package com.caliverse.admin.domain.api;

import java.util.Map;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.caliverse.admin.domain.request.MailRequest;
import com.caliverse.admin.domain.response.MailResponse;
import com.caliverse.admin.domain.service.MailService;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "우편", description = "우편 조회 및 발송 관리 메뉴 api")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/mail")
public class MailController {

    private final MailService mailService;

    // 우편 조회
    @GetMapping("/list")
    public ResponseEntity<MailResponse> getList(
            @RequestParam Map<String, String> requestParam){

        return ResponseEntity.ok().body(mailService.getList(requestParam));
    }
    // 우편 상세 조회
    @GetMapping("/detail/{id}")
    public ResponseEntity<MailResponse> getList(
            @PathVariable("id") Long id){

        return ResponseEntity.ok().body(mailService.getdetail(id));
    }
    @PostMapping("/excel-upload")
    public ResponseEntity<MailResponse> excelUpload(
            @RequestParam("file") MultipartFile file){
        return ResponseEntity.ok().body( mailService.excelUpload(file));
    }
    @GetMapping("/excel-down")
    public ResponseEntity<Resource> excelDown(
            @RequestParam("file") String fileName){
        Resource resource = mailService.excelDown(fileName);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);

        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
    @PostMapping
    public ResponseEntity<MailResponse> postMail(
            @RequestBody MailRequest mailRequest){

        return ResponseEntity.ok().body(mailService.postMail(mailRequest));
    }
    @PutMapping("/{id}")
    public ResponseEntity<MailResponse> updateMail(
            @PathVariable("id")Long id, @RequestBody MailRequest mailRequest){

        return ResponseEntity.ok().body(mailService.updateMail(id, mailRequest));
    }
    @DeleteMapping("/delete")
    public ResponseEntity<MailResponse> deleteMail(
            @RequestBody MailRequest mailRequest){

        return ResponseEntity.ok().body(mailService.deleteMail(mailRequest));
    }
    @PostMapping("/item")
    public ResponseEntity<MailResponse> getItem(
            @RequestBody Map<String, String> item) {
        return ResponseEntity.ok().body(mailService.getMetaItem(item.get("item")));
    }

    // 칼리움 수량 조회
    @GetMapping("/calium")
    public ResponseEntity<MailResponse> getCalium(){
        return ResponseEntity.ok().body(mailService.getStockCalium());
    }
}
