package com.erp.systemtool.controller;

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

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

/**
 * Data Cleanup Controller
 * 
 * Provides REST API for data cleanup operations.
 * Migrations from PHP CleanPullRecordsCommand.
 */
@RestController
@RequestMapping("/api/system/cleanup")
@RequiredArgsConstructor
public class DataCleanupController {

    private final DataCleanupService cleanupService;

    /**
     * Get cleanup status
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatus() {
        Map<String, Object> status = cleanupService.getCleanupStatus();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", status,
            "message", "success"
        ));
    }

    /**
     * Cleanup old pull records
     */
    @PostMapping("/pull-records")
    public ResponseEntity<Map<String, Object>> cleanupPullRecords(
            @RequestParam(required = false, defaultValue = "30") Integer days) {
        
        Map<String, Object> result = cleanupService.cleanupOldPullRecords(days);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", result,
            "message", String.format("已清理 %d 条旧拉取记录", result.get("deleted_count")))
        );
    }

    /**
     * Cleanup old operation logs
     */
    @PostMapping("/operation-logs")
    public ResponseEntity<Map<String, Object>> cleanupOperationLogs(
            @RequestParam(required = false, defaultValue = "90") Integer days) {
        
        Map<String, Object> result = cleanupService.cleanupOldOperationLogs(days);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", result,
            "message", String.format("已清理 %d 条旧操作日志", result.get("deleted_count")))
        );
    }

    /**
     * Cleanup old audit logs
     */
    @PostMapping("/audit-logs")
    public ResponseEntity<Map<String, Object>> cleanupAuditLogs(
            @RequestParam(required = false, defaultValue = "180") Integer days) {
        
        Map<String, Object> result = cleanupService.cleanupOldAuditLogs(days);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", result,
            "message", String.format("已清理 %d 条旧审计日志", result.get("deleted_count")))
        );
    }

    /**
     * Execute full cleanup
     */
    @PostMapping("/full")
    public ResponseEntity<Map<String, Object>> fullCleanup(
            @RequestBody(required = false) Map<String, Integer> retentionDays) {
        
        if (retentionDays == null) {
            retentionDays = new HashMap<>();
        }
        
        Map<String, Object> results = cleanupService.fullCleanup(retentionDays);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", results,
            "message", "全量清理完成"
        ));
    }
}
