package com.erp.systemtool.controller;

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

import java.util.Map;

/**
 * Data Consistency Controller
 * 
 * Provides REST API for data consistency checking and repair.
 * Migrations from PHP DataConsistencyService.
 */
@RestController
@RequestMapping("/api/system/consistency")
@RequiredArgsConstructor
public class DataConsistencyController {

    private final DataConsistencyService consistencyService;

    /**
     * Execute full consistency check
     */
    @GetMapping("/full-check")
    public ResponseEntity<Map<String, Object>> fullCheck() {
        Map<String, Object> results = consistencyService.fullCheck();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", results,
            "message", "全量一致性检查完成"
        ));
    }

    /**
     * Check stock consistency only
     */
    @GetMapping("/stock")
    public ResponseEntity<Map<String, Object>> checkStockConsistency() {
        Map<String, Object> results = consistencyService.checkAndFixStockConsistency();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", results,
            "message", String.format("库存一致性检查完成: 检查 %d 项, 修复 %d 项",
                results.get("checked"), results.get("fixed"))
        ));
    }

    /**
     * Check order-stock consistency only
     */
    @GetMapping("/order-stock")
    public ResponseEntity<Map<String, Object>> checkOrderStockConsistency() {
        Map<String, Object> results = consistencyService.checkOrderStockConsistency();
        
        @SuppressWarnings("unchecked")
        int issueCount = ((java.util.List<?>) results.get("issues")).size();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", results,
            "message", String.format("订单-库存一致性检查完成: 检查 %d 项, 发现 %d 个问题",
                results.get("checked"), issueCount)
        ));
    }

    /**
     * Check waybill consistency only
     */
    @GetMapping("/waybill")
    public ResponseEntity<Map<String, Object>> checkWaybillConsistency() {
        Map<String, Object> results = consistencyService.checkWaybillConsistency();
        
        @SuppressWarnings("unchecked")
        int issueCount = ((java.util.List<?>) results.get("issues")).size();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", results,
            "message", String.format("运单-物流一致性检查完成: 检查 %d 项, 发现 %d 个问题",
                results.get("checked"), issueCount)
        ));
    }

    /**
     * Check finance consistency only
     */
    @GetMapping("/finance")
    public ResponseEntity<Map<String, Object>> checkFinanceConsistency() {
        Map<String, Object> results = consistencyService.checkFinanceConsistency();
        
        @SuppressWarnings("unchecked")
        int issueCount = ((java.util.List<?>) results.get("issues")).size();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", results,
            "message", String.format("财务一致性检查完成: 检查 %d 项, 发现 %d 个问题",
                results.get("checked"), issueCount)
        ));
    }
}
