package com.controllers;

import com.common.service.PartitionTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

@RestController
@RequestMapping("/api/partition")
public class PartitionTableController {

    @Autowired
    private PartitionTableService partitionTableService;

    /**
     * 检查第二天的分区表状态
     */
    @GetMapping("/check-tomorrow")
    public Map<String, Object> checkTomorrowPartition() {
        Map<String, Object> result = new HashMap<>();

        try {
            PartitionTableService.PartitionTableStatus status =
                    partitionTableService.checkTomorrowPartitionTable();

            result.put("success", true);
            result.put("data", status);
            result.put("message", "检查完成");

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "检查失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 检查未来多天的分区表状态
     */
    @GetMapping("/check-future")
    public Map<String, Object> checkFuturePartitions(@RequestParam(defaultValue = "7") int days) {
        Map<String, Object> result = new HashMap<>();

        try {
            List<PartitionTableService.PartitionTableStatus>   statusList = partitionTableService.checkFuturePartitionTables(days);

            result.put("success", true);
            result.put("data", statusList);
            result.put("count", statusList.size());
            result.put("message", String.format("检查了未来 %d 天的分区表状态", days));

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "检查失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取分区表统计信息
     */
    @GetMapping("/stats")
    public Map<String, Object> getPartitionStats() {
        return partitionTableService.getPartitionTableStats();
    }

    /**
     * 手动创建指定日期的分区表
     */
    @PostMapping("/create")
    public Map<String, Object> createPartition(@RequestParam String date) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 日期格式: yyyy-MM-dd
            java.time.LocalDate localDate = java.time.LocalDate.parse(date);
            boolean success = partitionTableService.createDailyPartitionTable(localDate);

            result.put("success", success);
            result.put("message", success ? "分区表创建成功" : "分区表创建失败");

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "创建失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 手动触发创建下个月所有分区表
     */
    @PostMapping("/create-next-month")
    public Map<String, Object> createNextMonthPartition() {
        Map<String, Object> result = new HashMap<>();

        try {
            partitionTableService.createNextMonthPartitionTable();
            result.put("success", true);
            result.put("message", "分区表创建任务已触发");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "分区表创建失败: " + e.getMessage());
        }

        return result;
    }
}