package com.zioinfo.kintex.tenant;

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 com.zioinfo.kintex.tenant.dto.TenantAdminDto;
import com.zioinfo.kintex.tenant.dto.TenantCreateRequest;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SCR-A9 테넌트(전시관) 관리·온보딩 API (M18 · §1A). 플랫폼 슈퍼관리자 전용.
 * <p>1단계: 목록 조회 + 등록(온보딩 위저드 1단계 = 마스터 생성). 브랜딩·마스터데이터 시드·격리 스모크는 2단계.
 * 접근 가드는 {@link SystemAccessGuard#requireAdmin}(홀매니저/전역 ADMIN). 등록은 @Audited.
 */
@RestController
@RequestMapping("/api/admin/tenants")
public class TenantAdminController {

    private final TenantAdminService service;
    private final SystemAccessGuard guard;

    public TenantAdminController(TenantAdminService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** GET — 전 테넌트 목록(코드·명·도메인·상태·행사수·사용자수). */
    @GetMapping
    public ApiResponse<List<TenantAdminDto>> list(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.list());
    }

    /** POST — 테넌트 등록(온보딩 1단계). */
    @Audited(action = "TENANT_CREATE", targetType = "tenant")
    @PostMapping
    public ApiResponse<TenantAdminDto> create(@AuthenticationPrincipal KintexPrincipal principal,
                                              @RequestBody TenantCreateRequest req) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.create(req));
    }
}
