package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.MailTemplateFileService;
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-template-file")
@RequiredArgsConstructor
@Slf4j
public class MailTemplateFileController {
    private final MailTemplateFileService mailTemplateFileService;

    /** GET /list → 템플릿 목록 조회 (alias) */
    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMailTemplateFileListAlias(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailTemplateFileService.getMailTemplateFileList(params)));
    }

    /** GET / → 템플릿 목록 조회 */
    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMailTemplateFileList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailTemplateFileService.getMailTemplateFileList(params)));
    }

    /** GET /{id} → 템플릿 상세 조회 */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getMailTemplateFileInfo(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(mailTemplateFileService.getMailTemplateFileInfo(params)));
    }

    /** POST / → 템플릿 생성 */
    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertMailTemplateFile(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(mailTemplateFileService.insertMailTemplateFile(body)));
    }

    /** PUT /{id} → 템플릿 수정 */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateMailTemplateFile(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(mailTemplateFileService.updateMailTemplateFile(body)));
    }

    /** DELETE /{id} → 템플릿 삭제 */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteMailTemplateFile(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(mailTemplateFileService.deleteMailTemplateFile(params)));
    }

    /** POST /{id}/preview → 템플릿 HTML 미리보기 */
    @PostMapping("/{id}/preview")
    public ResponseEntity<ApiResponse<Map<String, Object>>> previewMailTemplateFile(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        Map<String, Object> result = mailTemplateFileService.previewMailTemplateFile(body);
        if (result.isEmpty()) {
            return ResponseEntity.status(404).body(ApiResponse.error("템플릿을 찾을 수 없습니다."));
        }
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    /** POST /{id}/preview-with-query → 쿼리 연동 미리보기 (미지원) */
    @PostMapping("/{id}/preview-with-query")
    public ResponseEntity<ApiResponse<Map<String, Object>>> previewMailTemplateFileWithQuery(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        return ResponseEntity.status(501).body(ApiResponse.error("SQL 쿼리 연동 기능은 현재 지원하지 않습니다."));
    }
}
