package com.erp.controller;

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

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/templates")
@RequiredArgsConstructor
@Slf4j
public class TemplateController {

    private final TemplateService templateService;

    /**
     * GET /api/templates — 템플릿 목록
     */
    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTemplateList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.putIfAbsent("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(templateService.getTemplateList(params)));
    }

    /**
     * GET /api/templates/{templateId} — 템플릿 상세
     */
    @GetMapping("/{templateId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTemplateInfo(
            @PathVariable String templateId,
            @RequestAttribute("company_code") String companyCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("template_id", templateId);
        params.put("company_code", companyCode);
        Map<String, Object> result = templateService.getTemplateInfo(params);
        if (result == null) {
            return ResponseEntity.ok(ApiResponse.error("템플릿을 찾을 수 없습니다"));
        }
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    /**
     * POST /api/templates — 템플릿 생성
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertTemplate(
            @RequestBody Map<String, Object> params,
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute("user_id") String userId) {
        params.put("company_code", companyCode);
        params.put("user_id", userId);
        Map<String, Object> result = templateService.insertTemplate(params);
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    /**
     * PUT /api/templates/{templateId} — 템플릿 수정
     */
    @PutMapping("/{templateId}")
    public ResponseEntity<ApiResponse<Void>> updateTemplate(
            @PathVariable String templateId,
            @RequestBody Map<String, Object> params,
            @RequestAttribute("user_id") String userId) {
        params.put("template_id", templateId);
        params.put("user_id", userId);
        templateService.updateTemplate(params);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    /**
     * PUT /api/templates/{templateId}/publish — 템플릿 게시
     */
    @PutMapping("/{templateId}/publish")
    public ResponseEntity<ApiResponse<Void>> publishTemplate(
            @PathVariable String templateId,
            @RequestAttribute("user_id") String userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("template_id", templateId);
        params.put("user_id", userId);
        templateService.publishTemplate(params);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    /**
     * DELETE /api/templates/{templateId} — 템플릿 삭제 (소프트)
     */
    @DeleteMapping("/{templateId}")
    public ResponseEntity<ApiResponse<Void>> deleteTemplate(
            @PathVariable String templateId,
            @RequestAttribute("user_id") String userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("template_id", templateId);
        params.put("user_id", userId);
        templateService.deleteTemplate(params);
        return ResponseEntity.ok(ApiResponse.success(null));
    }
}
