package com.ga.api.controller.receive;

import com.ga.api.service.receive.ReceiveService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.core.vo.receive.*;
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/receive")
@RequiredArgsConstructor
public class ReceiveController {

    private final ReceiveService receiveService;

    // 보험사 프로파일
    @GetMapping("/profiles")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "READ")
    public ApiResponse<List<CompanyProfileVO>> profiles() {
        return ApiResponse.ok(receiveService.listProfiles());
    }

    @PutMapping("/profiles/{companyCode}")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "UPDATE")
    @DataChangeLog(menu = "RECEIVE_MAPPING", table = "company_profile")
    public ApiResponse<Void> updateProfile(@PathVariable String companyCode, @RequestBody CompanyProfileVO vo) {
        receiveService.saveProfile(companyCode, vo);
        return ApiResponse.ok();
    }

    // 필드 매핑
    @GetMapping("/mapping/{companyCode}/fields")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "READ")
    public ApiResponse<List<CompanyFieldMappingVO>> fields(@PathVariable String companyCode,
                                                            @RequestParam(required = false) String targetTable) {
        return ApiResponse.ok(receiveService.listFields(companyCode, targetTable));
    }

    @PostMapping("/mapping/{companyCode}/fields")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "CREATE")
    @DataChangeLog(menu = "RECEIVE_MAPPING", table = "company_field_mapping")
    public ApiResponse<Long> createField(@PathVariable String companyCode, @RequestBody CompanyFieldMappingVO vo) {
        return ApiResponse.ok(receiveService.createField(companyCode, vo));
    }

    @PutMapping("/mapping/fields/{mappingId}")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "UPDATE")
    @DataChangeLog(menu = "RECEIVE_MAPPING", table = "company_field_mapping")
    public ApiResponse<Void> updateField(@PathVariable Long mappingId, @RequestBody CompanyFieldMappingVO vo) {
        receiveService.updateField(mappingId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/mapping/fields/{mappingId}")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "DELETE")
    @DataChangeLog(menu = "RECEIVE_MAPPING", table = "company_field_mapping")
    public ApiResponse<Void> deleteField(@PathVariable Long mappingId) {
        receiveService.deleteField(mappingId);
        return ApiResponse.ok();
    }

    // 코드 매핑
    @GetMapping("/mapping/{companyCode}/codes")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "READ")
    public ApiResponse<List<CodeMappingVO>> codes(@PathVariable String companyCode,
                                                   @RequestParam(required = false) String codeType) {
        return ApiResponse.ok(receiveService.listCodes(companyCode, codeType));
    }

    @PostMapping("/mapping/{companyCode}/codes")
    @RequirePermission(menu = "RECEIVE_MAPPING", perm = "CREATE")
    @DataChangeLog(menu = "RECEIVE_MAPPING", table = "code_mapping")
    public ApiResponse<Long> createCode(@PathVariable String companyCode, @RequestBody CodeMappingVO vo) {
        return ApiResponse.ok(receiveService.createCode(companyCode, vo));
    }

    // 원본 데이터 + 에러
    @GetMapping("/raw")
    @RequirePermission(menu = "RECEIVE_DATA", perm = "READ")
    public ApiResponse<List<RawCommissionDataVO>> rawList(@RequestParam(required = false) String companyCode,
                                                           @RequestParam(required = false) String settleMonth,
                                                           @RequestParam(required = false) String parseStatus) {
        return ApiResponse.ok(receiveService.listRaw(companyCode, settleMonth, parseStatus));
    }

    @GetMapping("/errors")
    @RequirePermission(menu = "RECEIVE_DATA", perm = "READ")
    public ApiResponse<List<ParseErrorLogVO>> errors(@RequestParam(required = false) String companyCode,
                                                      @RequestParam(required = false) String resolveStatus) {
        return ApiResponse.ok(receiveService.listErrors(companyCode, resolveStatus));
    }

    @PutMapping("/errors/{errorId}/resolve")
    @RequirePermission(menu = "RECEIVE_DATA", perm = "UPDATE")
    @DataChangeLog(menu = "RECEIVE_DATA", table = "parse_error_log")
    public ApiResponse<Void> resolveError(@PathVariable Long errorId, @Valid @RequestBody ResolveErrorReq req) {
        receiveService.resolveError(errorId, req.getStatus(), req.getNote());
        return ApiResponse.ok();
    }
}
