package com.ga.api.controller.license;

import com.ga.api.service.license.AgentLicenseService;
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.license.AgentLicenseResp;
import com.ga.core.vo.license.AgentLicenseSaveReq;
import com.ga.core.vo.license.AgentLicenseSearchParam;
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/agent-licenses")
@RequiredArgsConstructor
public class AgentLicenseController {

    private final AgentLicenseService service;

    @GetMapping
    @RequirePermission(menu = "AGENT_LICENSE", perm = "READ")
    public ApiResponse<PageResponse<AgentLicenseResp>> list(@ModelAttribute AgentLicenseSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/{licenseId}")
    @RequirePermission(menu = "AGENT_LICENSE", perm = "READ")
    public ApiResponse<AgentLicenseResp> detail(@PathVariable Long licenseId) {
        return ApiResponse.ok(service.detail(licenseId));
    }

    @GetMapping("/expiring")
    @RequirePermission(menu = "AGENT_LICENSE", perm = "READ")
    public ApiResponse<List<AgentLicenseResp>> expiring(
            @RequestParam(defaultValue = "30") int days) {
        return ApiResponse.ok(service.expiring(days));
    }

    @PostMapping
    @RequirePermission(menu = "AGENT_LICENSE", perm = "CREATE")
    @DataChangeLog(menu = "AGENT_LICENSE", table = "agent_license")
    public ApiResponse<Long> create(@Valid @RequestBody AgentLicenseSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PutMapping("/{licenseId}")
    @RequirePermission(menu = "AGENT_LICENSE", perm = "UPDATE")
    @DataChangeLog(menu = "AGENT_LICENSE", table = "agent_license")
    public ApiResponse<Void> update(@PathVariable Long licenseId,
                                    @Valid @RequestBody AgentLicenseSaveReq req) {
        service.update(licenseId, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/{licenseId}")
    @RequirePermission(menu = "AGENT_LICENSE", perm = "DELETE")
    @DataChangeLog(menu = "AGENT_LICENSE", table = "agent_license")
    public ApiResponse<Void> delete(@PathVariable Long licenseId) {
        service.delete(licenseId);
        return ApiResponse.ok();
    }
}
