package com.erp.audit.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.erp.audit.dto.*;
import com.erp.audit.entity.AuditLog;
import com.erp.audit.entity.ComplianceReport;
import com.erp.audit.service.AuditLogService;
import com.erp.audit.service.ComplianceService;
import com.erp.audit.service.ExportService;
import com.erp.audit.service.SensitiveOperationService;
import com.erp.audit.entity.SensitiveOperationRule;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 审计日志控制器
 */
@RestController
@RequestMapping("/api/audit-log")
@RequiredArgsConstructor
public class AuditLogController {

    private final AuditLogService auditLogService;
    private final ExportService exportService;
    private final ComplianceService complianceService;
    private final SensitiveOperationService sensitiveOperationService;

    // ================== CRUD接口 ==================

    @PostMapping
    public AuditLog create(@RequestBody AuditLogCreateRequest request) {
        return auditLogService.create(request);
    }

    @PutMapping("/{id}")
    public AuditLog update(@PathVariable Long id, @RequestBody AuditLogDTO dto) {
        return auditLogService.update(id, dto);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        auditLogService.delete(id);
    }

    @GetMapping("/{id}")
    public AuditLogDTO getById(@PathVariable Long id) {
        return auditLogService.getById(id);
    }

    @PostMapping("/page")
    public IPage<AuditLogDTO> pageQuery(@RequestBody AuditLogQueryRequest request) {
        return auditLogService.pageQuery(request);
    }

    // ================== 统计接口 ==================

    @GetMapping("/statistics")
    public AuditStatisticsDTO getStatistics(
            @RequestParam(required = false) Long tenantId,
            @RequestParam String startDate,
            @RequestParam String endDate) {
        return auditLogService.getStatistics(tenantId, startDate, endDate);
    }

    @GetMapping("/trend")
    public List<AuditStatisticsDTO.DailyTrend> getTrend(
            @RequestParam(required = false) Long tenantId,
            @RequestParam String startDate,
            @RequestParam String endDate) {
        return auditLogService.getTrend(tenantId, startDate, endDate);
    }

    // ================== 导出接口 ==================

    @PostMapping("/export/csv")
    public void exportCsv(@RequestBody ExportRequest request, HttpServletResponse response) throws Exception {
        exportService.exportCsv(request, response);
    }

    @PostMapping("/export/excel")
    public void exportExcel(@RequestBody ExportRequest request, HttpServletResponse response) throws Exception {
        exportService.exportExcel(request, response);
    }

    // ================== 敏感操作规则接口 ==================

    @GetMapping("/sensitive/rules")
    public List<SensitiveOperationRule> getSensitiveRules() {
        return sensitiveOperationService.getEnabledRules();
    }

    @PostMapping("/sensitive/rules")
    public SensitiveOperationRule saveRule(@RequestBody SensitiveOperationRule rule) {
        return sensitiveOperationService.saveRule(rule);
    }

    @DeleteMapping("/sensitive/rules/{id}")
    public void deleteRule(@PathVariable Long id) {
        sensitiveOperationService.deleteRule(id);
    }

    // ================== 合规报告接口 ==================

    @PostMapping("/compliance/report")
    public ComplianceReport generateReport(@RequestBody ComplianceReportRequest request) {
        return complianceService.generateReport(request);
    }

    @GetMapping("/compliance/report/{id}")
    public ComplianceReport getReportById(@PathVariable Long id) {
        return complianceService.getReportById(id);
    }

    @GetMapping("/compliance/report/no/{reportNo}")
    public ComplianceReport getReportByNo(@PathVariable String reportNo) {
        return complianceService.getReportByNo(reportNo);
    }

    @PostMapping("/compliance/reports")
    public IPage<ComplianceReport> listReports(
            @RequestParam(required = false) Long tenantId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize) {
        return complianceService.listReports(tenantId, pageNum, pageSize);
    }

    @PutMapping("/compliance/report/{id}/review")
    public void reviewReport(
            @PathVariable Long id,
            @RequestParam String reviewer,
            @RequestParam String conclusion) {
        complianceService.reviewReport(id, reviewer, conclusion);
    }

    @GetMapping("/compliance/summary")
    public Object getComplianceSummary(
            @RequestParam(required = false) Long tenantId,
            @RequestParam String startTime,
            @RequestParam String endTime) {
        return complianceService.getComplianceSummary(tenantId, 
                java.time.LocalDateTime.parse(startTime), 
                java.time.LocalDateTime.parse(endTime));
    }

    @GetMapping("/compliance/risk-assessment")
    public Object riskAssessment(
            @RequestParam(required = false) Long tenantId,
            @RequestParam String startTime,
            @RequestParam String endTime) {
        return complianceService.riskAssessment(tenantId,
                java.time.LocalDateTime.parse(startTime),
                java.time.LocalDateTime.parse(endTime));
    }

    // ================== 清理接口 ==================

    @DeleteMapping("/cleanup")
    public int cleanExpiredLogs(@RequestParam(defaultValue = "90") int retentionDays) {
        return auditLogService.cleanExpiredLogs(retentionDays);
    }
}
