package com.ga.api.controller.upload;

import com.ga.api.service.upload.UploadTemplateService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.core.vo.upload.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "업로드 템플릿 관리")
@RestController
@RequestMapping("/api/upload-templates")
@RequiredArgsConstructor
public class UploadTemplateController {

    private final UploadTemplateService templateService;

    @Operation(summary = "템플릿 목록 조회")
    @GetMapping
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "READ")
    public ApiResponse<PageResponse<UploadTemplateResp>> list(@ModelAttribute UploadTemplateSearchParam param) {
        return ApiResponse.ok(templateService.list(param));
    }

    @Operation(summary = "템플릿 상세 조회 (컬럼 포함)")
    @GetMapping("/{templateId}")
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "READ")
    public ApiResponse<UploadTemplateResp> detail(@PathVariable Long templateId) {
        return ApiResponse.ok(templateService.detail(templateId));
    }

    @Operation(summary = "템플릿 등록")
    @PostMapping
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "CREATE")
    @DataChangeLog(menu = "SYSTEM_UPLOAD_TEMPLATE", table = "upload_template")
    public ApiResponse<Long> create(@Valid @RequestBody UploadTemplateSaveReq req) {
        return ApiResponse.ok(templateService.create(req));
    }

    @Operation(summary = "템플릿 수정")
    @PutMapping("/{templateId}")
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "UPDATE")
    @DataChangeLog(menu = "SYSTEM_UPLOAD_TEMPLATE", table = "upload_template")
    public ApiResponse<Void> update(@PathVariable Long templateId,
                                    @Valid @RequestBody UploadTemplateSaveReq req) {
        templateService.update(templateId, req);
        return ApiResponse.ok();
    }

    @Operation(summary = "템플릿 삭제")
    @DeleteMapping("/{templateId}")
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "DELETE")
    @DataChangeLog(menu = "SYSTEM_UPLOAD_TEMPLATE", table = "upload_template")
    public ApiResponse<Void> delete(@PathVariable Long templateId) {
        templateService.delete(templateId);
        return ApiResponse.ok();
    }

    @Operation(summary = "템플릿 컬럼 목록 조회")
    @GetMapping("/{templateId}/columns")
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "READ")
    public ApiResponse<List<UploadTemplateColumnVO>> columns(@PathVariable Long templateId) {
        return ApiResponse.ok(templateService.columns(templateId));
    }

    @Operation(summary = "템플릿 컬럼 전체 갱신 (기존 삭제 후 재등록)")
    @PutMapping("/{templateId}/columns")
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "UPDATE")
    @DataChangeLog(menu = "SYSTEM_UPLOAD_TEMPLATE", table = "upload_template_column")
    public ApiResponse<Void> updateColumns(@PathVariable Long templateId,
                                           @RequestBody List<UploadTemplateColumnVO> columns) {
        templateService.updateColumns(templateId, columns);
        return ApiResponse.ok();
    }

    @Operation(summary = "템플릿 업로드 이력 조회")
    @GetMapping("/{templateId}/history")
    @RequirePermission(menu = "SYSTEM_UPLOAD_TEMPLATE", perm = "READ")
    public ApiResponse<PageResponse<UploadHistoryResp>> history(@PathVariable Long templateId,
                                                                 @ModelAttribute UploadTemplateSearchParam param) {
        return ApiResponse.ok(templateService.history(templateId, param));
    }
}
