package gc.mda.kcg.domain.enforcement;

import gc.mda.kcg.domain.enforcement.dto.CreatePlanRequest;
import gc.mda.kcg.domain.enforcement.dto.CreateRecordRequest;
import gc.mda.kcg.domain.enforcement.dto.UpdateRecordRequest;
import gc.mda.kcg.permission.annotation.RequirePermission;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * 단속 이력/계획 CRUD API.
 * enforcement_records, enforcement_plans 테이블 기반.
 */
@RestController
@RequestMapping("/api/enforcement")
@RequiredArgsConstructor
public class EnforcementController {

    private final EnforcementService service;

    // ========================================================================
    // 단속 이력 (Records)
    // ========================================================================

    /**
     * 단속 이력 목록 조회 (violationType 필터, 페이징)
     */
    @GetMapping("/records")
    @RequirePermission(resource = "enforcement:enforcement-history", operation = "READ")
    public Page<EnforcementRecord> listRecords(
            @RequestParam(required = false) String violationType,
            @RequestParam(required = false) String vesselMmsi,
            Pageable pageable
    ) {
        return service.listRecords(violationType, vesselMmsi, pageable);
    }

    /**
     * 단속 이력 상세 조회
     */
    @GetMapping("/records/{id}")
    @RequirePermission(resource = "enforcement:enforcement-history", operation = "READ")
    public EnforcementRecord getRecord(@PathVariable Long id) {
        return service.getRecord(id);
    }

    /**
     * 단속 이력 신규 등록. UID 자동 생성 (ENF-yyyyMMdd-NNNN).
     * event_id가 있으면 해당 prediction_events.status를 RESOLVED로 갱신.
     */
    @PostMapping("/records")
    @ResponseStatus(HttpStatus.CREATED)
    @RequirePermission(resource = "enforcement:enforcement-history", operation = "CREATE")
    public EnforcementRecord createRecord(@RequestBody CreateRecordRequest req) {
        return service.createRecord(req);
    }

    /**
     * 단속 이력 결과 수정 (result, ai_match_status, remarks)
     */
    @PatchMapping("/records/{id}")
    @RequirePermission(resource = "enforcement:enforcement-history", operation = "UPDATE")
    public EnforcementRecord updateRecord(
            @PathVariable Long id,
            @RequestBody UpdateRecordRequest req
    ) {
        return service.updateRecord(id, req);
    }

    // ========================================================================
    // 단속 계획 (Plans)
    // ========================================================================

    /**
     * 단속 계획 목록 조회 (status 필터, 페이징)
     */
    @GetMapping("/plans")
    @RequirePermission(resource = "enforcement:enforcement-history", operation = "READ")
    public Page<EnforcementPlan> listPlans(
            @RequestParam(required = false) String status,
            Pageable pageable
    ) {
        return service.listPlans(status, pageable);
    }

    /**
     * 단속 계획 생성
     */
    @PostMapping("/plans")
    @ResponseStatus(HttpStatus.CREATED)
    @RequirePermission(resource = "enforcement:enforcement-history", operation = "CREATE")
    public EnforcementPlan createPlan(@RequestBody CreatePlanRequest req) {
        return service.createPlan(req);
    }
}
