package com.ga.api.controller.product;

import com.ga.api.service.product.ProductService;
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.product.ProductResp;
import com.ga.core.vo.product.ProductVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "상품 관리")
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @GetMapping
    @RequirePermission(menu = "PRODUCT", perm = "READ")
    public ApiResponse<PageResponse<ProductResp>> list(@RequestParam(defaultValue = "1") int pageNum,
                                                       @RequestParam(defaultValue = "10") int pageSize,
                                                       @RequestParam(required = false) Long companyId,
                                                       @RequestParam(required = false) String insuranceType,
                                                       @RequestParam(required = false) String keyword,
                                                       @RequestParam(required = false) String isActive) {
        return ApiResponse.ok(productService.list(pageNum, pageSize, companyId, insuranceType, keyword, isActive));
    }

    @GetMapping("/{productId}")
    @RequirePermission(menu = "PRODUCT", perm = "READ")
    public ApiResponse<ProductResp> detail(@PathVariable Long productId) {
        return ApiResponse.ok(productService.detail(productId));
    }

    @PostMapping
    @RequirePermission(menu = "PRODUCT", perm = "CREATE")
    @DataChangeLog(menu = "PRODUCT", table = "product")
    public ApiResponse<Long> create(@RequestBody ProductVO vo) {
        return ApiResponse.ok(productService.create(vo));
    }

    @PutMapping("/{productId}")
    @RequirePermission(menu = "PRODUCT", perm = "UPDATE")
    @DataChangeLog(menu = "PRODUCT", table = "product")
    public ApiResponse<Void> update(@PathVariable Long productId, @RequestBody ProductVO vo) {
        productService.update(productId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/{productId}")
    @RequirePermission(menu = "PRODUCT", perm = "DELETE")
    @DataChangeLog(menu = "PRODUCT", table = "product")
    public ApiResponse<Void> delete(@PathVariable Long productId) {
        productService.delete(productId);
        return ApiResponse.ok();
    }
}
