package cn.nianxx.thhotel.platform.audit.control;

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.audit.common.result.AdminAuditLogResult;
import cn.nianxx.thhotel.platform.audit.service.PlatformAdminAuditLogService;
import cn.nianxx.thhotel.platform.common.result.PlatformPageResult;
import cn.nianxx.thhotel.platform.security.service.AdminAuthorizationService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理后台审计 Controller。提供管理写操作审计列表。
 */
@RestController
@RequestMapping("/api/admin/audits")
public class AdminAuditLogController {

    private final AdminAuthorizationService authorizationService;
    private final PlatformAdminAuditLogService auditLogService;

    /**
     * 注入管理鉴权和审计服务。
     */
    public AdminAuditLogController(
            AdminAuthorizationService authorizationService,
            PlatformAdminAuditLogService auditLogService) {
        this.authorizationService = authorizationService;
        this.auditLogService = auditLogService;
    }

    /**
     * 分页查询管理审计，需要系统管理入口权限。
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public PlatformPageResult<AdminAuditLogResult> listAudits(
            @RequestParam(name = "target_type", required = false) String targetType,
            @RequestParam(name = "target_id", required = false) String targetId,
            @RequestParam(required = false) String action,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_ADMIN_CONSOLE_ACCESS.name());
        return auditLogService.query(targetType, targetId, action, pageNum, pageSize);
    }
}
