package com.ga.api.controller.settle;

import com.ga.api.service.settle.IncentiveProgramService;
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.settle.IncentiveProgramResp;
import com.ga.core.vo.settle.IncentiveProgramSaveReq;
import com.ga.core.vo.settle.IncentiveProgramSearchParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "시책 프로그램 관리")
@RestController
@RequestMapping("/api/incentive-programs")
@RequiredArgsConstructor
public class IncentiveProgramController {

    private final IncentiveProgramService service;

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

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

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

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

    @PutMapping("/{programId}/active")
    @RequirePermission(menu = "INCENTIVE_PROG", perm = "UPDATE")
    @DataChangeLog(menu = "INCENTIVE_PROG", table = "incentive_program")
    public ApiResponse<Void> toggleActive(@PathVariable Long programId, @RequestBody Map<String, Boolean> body) {
        service.toggleActive(programId, Boolean.TRUE.equals(body.get("isActive")));
        return ApiResponse.ok();
    }
}
