package com.njcn.relational.controller;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.njcn.relational.service.SyncTwoDmExportService;
import com.njcn.relational.service.SyncThreeDmParseService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: cdf
 * @CreateTime: 2026-05-26
 * @Description:
 */
@RestController
@RequestMapping("/source")
@RequiredArgsConstructor
@Slf4j
public class DataTransportController {

    private final SyncTwoDmExportService syncTwoDmExportService;

    private final SyncThreeDmParseService syncThreeDmParseService;

    @Value("${spring.profiles.active:query}")
    private String serverType;

    @GetMapping("export")
    public void export(@RequestParam(required = false)String date){
        if(!serverType.contains("query")){
            return;
        }

        if(StrUtil.isBlank(date)){
            date = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATE_PATTERN));
        }else {
            if(!isValidDate(date)){
                log.error("参数时间格式错误"+date);
                return;
            }
        }
        syncTwoDmExportService.syncAllFullTables(date);
    }

    @GetMapping("import")
    public void importData(@RequestParam(required = false)String date){
        if(!serverType.contains("insert")){
            return;
        }
        if(StrUtil.isBlank(date)){
            date = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATE_PATTERN));
        }else {
            if(!isValidDate(date)){
                log.error("参数时间格式错误"+date);
                return;
            }
        }
        try {
            syncThreeDmParseService.syncFullTables(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("test")
    public void test(){
        if(!serverType.contains("query")){
            return;
        }
        syncTwoDmExportService.test();
    }

    @GetMapping("testR")
    public void testR(){
        if(!serverType.contains("insert")){
            return;
        }
        syncThreeDmParseService.testR();
    }

    @GetMapping("getRemoteTwoAll")
    public List<String> getRemoteTwoAll(){
        try {
            return syncTwoDmExportService.getPendingFiles();
        } catch (Exception e) {
            log.error("最终捕获二区异常",e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping("getRemoteAll")
    public List<String> getAll(){
        try {
            if("query_up".equals(serverType)){
                return syncTwoDmExportService.getPendingFiles();
            }else if("insert_up".equals(serverType)){
                return syncThreeDmParseService.getPendingFiles();
            }else {
                return new ArrayList<>();

            }
        } catch (Exception e) {
            log.error("最终捕获异常",e);
            return new ArrayList<>();
        }
    }

    @GetMapping("removeTwo")
    public String removeTwo(@RequestParam Integer a){
        try {
            syncTwoDmExportService.cleanLocalFiles(a);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "成功";
    }


    @GetMapping("removeThree")
    public String removeThree(@RequestParam Integer a){
        try {
            syncThreeDmParseService.cleanLocalFiles(a);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "成功";
    }


    /**
     * 删除横向隔离设备公共摆渡区数据
     * @author cdf
     * @date 2026/6/10
     */
    @GetMapping("removeRemote")
    public String removeRemote(@RequestParam Integer a){

        return "成功";
    }

    @GetMapping("getConfig")
    public Map<String, Object> getConfig(){
        Map<String, Object> config = new HashMap<>();
        if(serverType.contains("query")){
            config.put("serverType", "source");
        }else {
            config.put("serverType", "target");
        }
        config.put("serverTypeName", serverType.contains("query") ? "二区源服务器" : "三区目标服务器");
        return config;
    }

    public boolean isValidDate(String dateStr) {
        if (dateStr == null || dateStr.length() != 8) {
            return false;
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        try {
            LocalDate.parse(dateStr, formatter);
            return true;
        } catch (DateTimeParseException e) {
            return false;
        }
    }

}
