package com.zioinfo.signage.admin;

import com.zioinfo.signage.admin.dto.AuditLog;
import com.zioinfo.signage.admin.dto.SignageSetting;
import com.zioinfo.signage.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * GUARDiA SIGNAGE 관리자 API — 감사 로그 조회 + 시스템 설정.
 *
 * <p>RBAC (SecurityConfig requestMatchers 로 통제):
 * <ul>
 *   <li>GET  /api/admin/audit       — ADMIN, MANAGER</li>
 *   <li>GET  /api/admin/settings    — ADMIN, MANAGER</li>
 *   <li>PUT  /api/admin/settings/** — ADMIN</li>
 * </ul>
 *
 * <p>사용자/테넌트/펌웨어 CRUD 는 기존 도메인 컨트롤러(UserController/TenantController/FirmwareController)
 * 가 담당한다. 이 컨트롤러는 admin 보조 영역(감사·설정)만 추가한다.
 */
@RestController
@RequestMapping("/api/admin")
@RequiredArgsConstructor
public class AdminController {

    private final AuditService auditService;
    private final SettingService settingService;

    // ===================== 감사 로그 (ADMIN/MANAGER) =====================

    @GetMapping("/audit")
    public ApiResponse<List<AuditLog>> audit(
            @RequestParam(required = false) String tenantCode,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String action,
            @RequestParam(required = false) String resource,
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime from,
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime to,
            @RequestParam(defaultValue = "100") int limit) {
        return ApiResponse.ok(auditService.find(tenantCode, username, action, resource, from, to, limit));
    }

    @GetMapping("/audit/count")
    public ApiResponse<Long> auditCount(
            @RequestParam(required = false) String tenantCode,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String action,
            @RequestParam(required = false) String resource,
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime from,
            @RequestParam(required = false)
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime to) {
        return ApiResponse.ok(auditService.count(tenantCode, username, action, resource, from, to));
    }

    // ===================== 시스템 설정 =====================

    @GetMapping("/settings")
    public ApiResponse<List<SignageSetting>> settings(@RequestParam(required = false) String category) {
        return ApiResponse.ok(settingService.list(category));
    }

    @PutMapping("/settings/{key}")
    public ApiResponse<SignageSetting> updateSetting(@PathVariable String key,
                                                 @RequestBody Map<String, String> body) {
        return ApiResponse.ok("설정 변경 완료", settingService.update(key, body.get("value")));
    }
}
