package com.sino.mci.policy.controller;

import com.sino.mci.policy.dto.CreatePolicyGroupRequest;
import com.sino.mci.policy.dto.UpdatePolicyGroupRequest;
import com.sino.mci.policy.dto.UpdatePolicyGroupStatusRequest;
import com.sino.mci.policy.entity.PolicyGroup;
import com.sino.mci.policy.service.PolicyGroupService;
import com.sino.mci.shared.web.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/policy-group")
@RequiredArgsConstructor
public class PolicyGroupController {

    private final PolicyGroupService policyGroupService;

    @PostMapping
    public ApiResponse<PolicyGroup> createPolicyGroup(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody CreatePolicyGroupRequest request) {
        return ApiResponse.ok(policyGroupService.createPolicyGroup(tenantCode, request));
    }

    @GetMapping("/list")
    public ApiResponse<List<PolicyGroup>> listPolicyGroups(
            @RequestHeader("X-Tenant-Code") String tenantCode) {
        return ApiResponse.ok(policyGroupService.listPolicyGroups(tenantCode));
    }

    @GetMapping("/{groupCode}")
    public ApiResponse<PolicyGroup> getPolicyGroup(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("groupCode") String groupCode) {
        return ApiResponse.ok(policyGroupService.getPolicyGroup(tenantCode, groupCode));
    }

    @PostMapping("/{groupCode}")
    public ApiResponse<PolicyGroup> updatePolicyGroup(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("groupCode") String groupCode,
            @Valid @RequestBody UpdatePolicyGroupRequest request) {
        return ApiResponse.ok(policyGroupService.updatePolicyGroup(tenantCode, groupCode, request));
    }

    @PostMapping("/{groupCode}/status")
    public ApiResponse<PolicyGroup> updatePolicyGroupStatus(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("groupCode") String groupCode,
            @Valid @RequestBody UpdatePolicyGroupStatusRequest request) {
        return ApiResponse.ok(policyGroupService.updatePolicyGroupStatus(tenantCode, groupCode, request.getStatus()));
    }

    @PostMapping("/{groupCode}/delete")
    public ApiResponse<?> deletePolicyGroup(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("groupCode") String groupCode) {
        policyGroupService.deletePolicyGroup(tenantCode, groupCode);
        return ApiResponse.ok();
    }
}
