package com.zioinfo.kintex.admin;

import com.fasterxml.jackson.databind.JsonNode;
import com.zioinfo.kintex.admin.dto.RulesetSummaryDto;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.system.SystemAccessGuard;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SCR-A8 규정 룰셋 버전 조회 API (M18). 시스템관리자(홀매니저/ADMIN)만 접근. 읽기 전용.
 * 룰셋은 classpath 리소스로 버전 관리되며 편집 엔드포인트는 제공하지 않는다(규정 개정 = 리소스 교체).
 */
@RestController
@RequestMapping("/api/admin/rulesets")
public class AdminRulesetController {

    private final AdminRulesetService service;
    private final SystemAccessGuard guard;

    public AdminRulesetController(AdminRulesetService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** GET — 룰셋 파일 요약 목록(파일명·버전·규칙 수·모듈 분포). */
    @GetMapping
    public ApiResponse<List<RulesetSummaryDto>> list(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.list());
    }

    /** GET — 지정 룰셋의 규칙 전문(JSON 원문). */
    @GetMapping("/{name}")
    public ApiResponse<JsonNode> detail(@AuthenticationPrincipal KintexPrincipal principal,
                                        @PathVariable String name) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.detail(name));
    }
}
