package com.zioinfo.kintex.system.authpolicy;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/** 시스템관리 — 인증 정책 API. ADMIN 게이트. */
@RestController
@RequestMapping("/api/admin/auth-policy")
public class AuthPolicyController {

    private final AuthPolicyService service;
    private final SystemAccessGuard guard;

    public AuthPolicyController(AuthPolicyService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<AuthPolicyDto> get(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.get());
    }

    @Audited(action = "AUTH_POLICY_SAVE", targetType = "sys_auth_policy")
    @PutMapping
    public ApiResponse<Void> save(@AuthenticationPrincipal KintexPrincipal principal,
                                  @RequestBody AuthPolicySaveRequest req) {
        guard.requireAdmin(principal);
        service.save(req, principal.userId());
        return ApiResponse.ok(null);
    }
}
