package com.ga.api.controller.dict;

import com.ga.api.service.dict.DataDomainService;
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.dict.DataDomainOptionResp;
import com.ga.core.vo.dict.DataDomainResp;
import com.ga.core.vo.dict.DataDomainSaveReq;
import com.ga.core.vo.dict.DataDomainSearchParam;
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/data-domains")
@RequiredArgsConstructor
public class DataDomainController {

    private final DataDomainService dataDomainService;

    @GetMapping
    @RequirePermission(menu = "SYSTEM_DATA_DOMAIN", perm = "READ")
    public ApiResponse<PageResponse<DataDomainResp>> list(@ModelAttribute DataDomainSearchParam param) {
        return ApiResponse.ok(dataDomainService.list(param));
    }

    @GetMapping("/options")
    @RequirePermission(menu = "SYSTEM_DATA_DOMAIN", perm = "READ")
    public ApiResponse<List<DataDomainOptionResp>> options() {
        return ApiResponse.ok(dataDomainService.options());
    }

    @GetMapping("/{domainCode}")
    @RequirePermission(menu = "SYSTEM_DATA_DOMAIN", perm = "READ")
    public ApiResponse<DataDomainResp> detail(@PathVariable String domainCode) {
        return ApiResponse.ok(dataDomainService.detail(domainCode));
    }

    @PostMapping
    @RequirePermission(menu = "SYSTEM_DATA_DOMAIN", perm = "CREATE")
    @DataChangeLog(menu = "SYSTEM_DATA_DOMAIN", table = "data_domain")
    public ApiResponse<Void> create(@Valid @RequestBody DataDomainSaveReq req) {
        dataDomainService.create(req);
        return ApiResponse.ok();
    }

    @PutMapping("/{domainCode}")
    @RequirePermission(menu = "SYSTEM_DATA_DOMAIN", perm = "UPDATE")
    @DataChangeLog(menu = "SYSTEM_DATA_DOMAIN", table = "data_domain")
    public ApiResponse<Void> update(@PathVariable String domainCode,
                                    @Valid @RequestBody DataDomainSaveReq req) {
        dataDomainService.update(domainCode, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/{domainCode}")
    @RequirePermission(menu = "SYSTEM_DATA_DOMAIN", perm = "DELETE")
    @DataChangeLog(menu = "SYSTEM_DATA_DOMAIN", table = "data_domain")
    public ApiResponse<Void> delete(@PathVariable String domainCode) {
        dataDomainService.delete(domainCode);
        return ApiResponse.ok();
    }
}
