package com.erp.systemtool.controller;

import com.erp.systemtool.entity.AuditLog;
import com.erp.systemtool.service.AuditLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Audit Log Controller
 * 
 * Provides REST API for audit log management.
 * Migrations from PHP AuditLogController.
 */
@RestController
@RequestMapping("/api/system/audit-logs")
@RequiredArgsConstructor
public class AuditLogController {

    private final AuditLogService auditLogService;

    /**
     * Query audit logs with filters
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> queryLogs(
            @RequestParam Map<String, Object> params,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        Map<String, Object> result = auditLogService.queryLogs(tenantId, params);
        result.put("code", 200);
        result.put("message", "success");
        return ResponseEntity.ok(result);
    }

    /**
     * Get audit log by ID
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getLogById(
            @PathVariable Long id,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        return auditLogService.getLogById(id, tenantId)
            .map(log -> ResponseEntity.ok(Map.<String, Object>of(
                "code", 200,
                "data", log,
                "message", "success"
            )))
            .orElse(ResponseEntity.status(404).body(Map.of(
                "code", 404,
                "message", "日志不存在"
            )));
    }

    /**
     * Get audit log statistics
     */
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        Map<String, Object> stats = auditLogService.getStats(tenantId, startDate, endDate);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", stats,
            "message", "success"
        ));
    }

    /**
     * Clear old audit logs
     */
    @DeleteMapping("/clear")
    public ResponseEntity<Map<String, Object>> clearLogs(
            @RequestParam(required = false) Integer days,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        long deletedCount = auditLogService.clearOldLogs(tenantId, days);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", Map.of("deleted_count", deletedCount),
            "message", String.format("成功清空 %d 条审计日志", deletedCount)
        ));
    }

    /**
     * Create audit log (internal use)
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createLog(
            @RequestBody AuditLog auditLog,
            @RequestHeader(value = "X-Tenant-Id", defaultValue = "1") Integer tenantId) {
        
        auditLog.setTenantId(tenantId);
        AuditLog created = auditLogService.createLog(auditLog);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", created,
            "message", "success"
        ));
    }
}
