package com.ga.api.controller.training;

import com.ga.api.service.training.AgentTrainingService;
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.training.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Tag(name = "보수교육 관리")
@RestController
@RequiredArgsConstructor
public class AgentTrainingController {

    private final AgentTrainingService service;

    // ── Training ──────────────────────────────────────────────────────────────

    @GetMapping("/api/agent-trainings")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "READ")
    public ApiResponse<PageResponse<AgentTrainingResp>> list(@ModelAttribute AgentTrainingSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/api/agent-trainings/{trainingId}")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "READ")
    public ApiResponse<AgentTrainingResp> detail(@PathVariable Long trainingId) {
        return ApiResponse.ok(service.detail(trainingId));
    }

    @GetMapping("/api/agent-trainings/annual-hours")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "READ")
    public ApiResponse<BigDecimal> annualHours(@RequestParam Long agentId,
                                               @RequestParam int year) {
        return ApiResponse.ok(service.annualHours(agentId, year));
    }

    @PostMapping("/api/agent-trainings")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "CREATE")
    @DataChangeLog(menu = "AGENT_TRAINING", table = "agent_training")
    public ApiResponse<Long> create(@Valid @RequestBody AgentTrainingSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PutMapping("/api/agent-trainings/{trainingId}")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "UPDATE")
    @DataChangeLog(menu = "AGENT_TRAINING", table = "agent_training")
    public ApiResponse<Void> update(@PathVariable Long trainingId,
                                    @Valid @RequestBody AgentTrainingSaveReq req) {
        service.update(trainingId, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/api/agent-trainings/{trainingId}")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "DELETE")
    @DataChangeLog(menu = "AGENT_TRAINING", table = "agent_training")
    public ApiResponse<Void> delete(@PathVariable Long trainingId) {
        service.delete(trainingId);
        return ApiResponse.ok();
    }

    // ── Requirement ───────────────────────────────────────────────────────────

    @GetMapping("/api/training-requirements")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "READ")
    public ApiResponse<List<TrainingRequirementResp>> listRequirements(
            @ModelAttribute TrainingRequirementSearchParam param) {
        return ApiResponse.ok(service.listRequirements(param));
    }

    @GetMapping("/api/training-requirements/{year}")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "READ")
    public ApiResponse<TrainingRequirementResp> requirementByYear(@PathVariable int year) {
        return ApiResponse.ok(service.requirementByYear(year));
    }

    @PostMapping("/api/training-requirements")
    @RequirePermission(menu = "AGENT_TRAINING", perm = "UPDATE")
    @DataChangeLog(menu = "AGENT_TRAINING", table = "training_requirement")
    public ApiResponse<Void> saveRequirement(@Valid @RequestBody TrainingRequirementSaveReq req) {
        service.saveRequirement(req);
        return ApiResponse.ok();
    }
}
