package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.MailSentHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/mail-sent-history")
@RequiredArgsConstructor
@Slf4j
public class MailSentHistoryController {
    private final MailSentHistoryService mailSentHistoryService;

    // GET /statistics — /{id} 보다 먼저 정의해야 매핑 우선순위 확보
    @GetMapping("/statistics")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMailSentHistoryStatistics(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailSentHistoryService.getMailSentHistoryStatistics(params)));
    }

    // GET / — 목록 조회 (페이지네이션)
    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMailSentHistoryList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailSentHistoryService.getMailSentHistoryList(params)));
    }

    // POST /draft — 임시 저장
    @PostMapping("/draft")
    public ResponseEntity<ApiResponse<Map<String, Object>>> saveMailSentHistoryDraft(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(
                mailSentHistoryService.saveMailSentHistoryDraft(body), "임시 저장되었습니다."));
    }

    // PUT /draft/{id} — 임시 저장 업데이트
    @PutMapping("/draft/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateMailSentHistoryDraft(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(
                mailSentHistoryService.updateMailSentHistoryDraft(body), "임시 저장이 업데이트되었습니다."));
    }

    // POST /bulk/delete — 일괄 소프트 삭제
    @PostMapping("/bulk/delete")
    public ResponseEntity<ApiResponse<Map<String, Object>>> bulkDeleteMailSentHistory(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailSentHistoryService.bulkDeleteMailSentHistory(body)));
    }

    // POST /bulk/permanent-delete — 일괄 영구 삭제
    @PostMapping("/bulk/permanent-delete")
    public ResponseEntity<ApiResponse<Map<String, Object>>> bulkPermanentDeleteMailSentHistory(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailSentHistoryService.bulkPermanentDeleteMailSentHistory(body)));
    }

    // POST /bulk/restore — 일괄 복구
    @PostMapping("/bulk/restore")
    public ResponseEntity<ApiResponse<Map<String, Object>>> bulkRestoreMailSentHistory(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailSentHistoryService.bulkRestoreMailSentHistory(body)));
    }

    // POST /{id}/restore — 메일 복구
    @PostMapping("/{id}/restore")
    public ResponseEntity<ApiResponse<Map<String, Object>>> restoreMailSentHistory(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(
                mailSentHistoryService.restoreMailSentHistory(params), "메일이 복구되었습니다."));
    }

    // DELETE /{id}/permanent — 영구 삭제
    @DeleteMapping("/{id}/permanent")
    public ResponseEntity<ApiResponse<Map<String, Object>>> permanentlyDeleteMailSentHistory(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(
                mailSentHistoryService.permanentlyDeleteMailSentHistory(params), "메일이 영구 삭제되었습니다."));
    }

    // GET /{id} — 단건 조회
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMailSentHistoryInfo(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(mailSentHistoryService.getMailSentHistoryInfo(params)));
    }

    // DELETE /{id} — 소프트 삭제
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteMailSentHistory(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(
                mailSentHistoryService.deleteMailSentHistory(params), "발송 이력이 삭제되었습니다."));
    }
}
