package com.njcn.gather.tool.adddata.service.impl;

import com.njcn.gather.tool.adddata.component.AddDataInfluxTaskExecutor;
import com.njcn.gather.tool.adddata.component.AddDataTaskStatusHolder;
import com.njcn.gather.tool.adddata.pojo.bo.AddDataTaskCommand;
import com.njcn.gather.tool.adddata.pojo.param.AddDataTaskRequestParam;
import com.njcn.gather.tool.adddata.pojo.vo.AddDataTaskCreateVO;
import com.njcn.gather.tool.adddata.pojo.vo.AddDataTaskStatusVO;
import com.njcn.gather.tool.adddata.service.AddDataInfluxTaskService;
import com.njcn.gather.tool.adddata.util.AddDataDateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * InfluxDB 数据补录任务服务实现。
 */
@Service
@RequiredArgsConstructor
public class AddDataInfluxTaskServiceImpl implements AddDataInfluxTaskService {

    /** 支持的用户步长。 */
    private static final Set<Integer> SUPPORTED_INTERVALS = new LinkedHashSet<Integer>(Arrays.asList(1, 3, 5, 10));

    /** 任务状态持有器。 */
    private final AddDataTaskStatusHolder addDataTaskStatusHolder;

    /** InfluxDB 后台执行器。 */
    private final AddDataInfluxTaskExecutor addDataInfluxTaskExecutor;

    @Override
    public AddDataTaskCreateVO create(AddDataTaskRequestParam param) {
        AddDataTaskCommand command = buildCommand(param);
        AddDataTaskStatusVO snapshot = addDataTaskStatusHolder.createWaitingTask(command);
        addDataInfluxTaskExecutor.submit(snapshot.getTaskId(), command);
        AddDataTaskCreateVO result = new AddDataTaskCreateVO();
        result.setTaskId(snapshot.getTaskId());
        result.setStatus(snapshot.getStatus());
        return result;
    }

    @Override
    public AddDataTaskStatusVO getStatus(String taskId) {
        if (taskId == null || taskId.trim().isEmpty()) {
            throw new IllegalArgumentException("任务编号不能为空");
        }
        return addDataTaskStatusHolder.getStatus(taskId.trim());
    }

    private AddDataTaskCommand buildCommand(AddDataTaskRequestParam param) {
        if (param == null) {
            throw new IllegalArgumentException("补数参数不能为空");
        }
        Integer intervalMinutes = param.getIntervalMinutes();
        if (intervalMinutes == null || !SUPPORTED_INTERVALS.contains(intervalMinutes)) {
            throw new IllegalArgumentException("时间步长仅支持 1、3、5、10 分钟");
        }
        List<String> lineIds = normalizeLineIds(param.getLineIds());
        LocalDateTime startTime = AddDataDateTimeUtil.parse(param.getStartTime());
        LocalDateTime endTime = AddDataDateTimeUtil.parse(param.getEndTime());
        if (startTime.isAfter(endTime)) {
            throw new IllegalArgumentException("开始时间不能大于结束时间");
        }
        return new AddDataTaskCommand(lineIds, startTime, endTime, intervalMinutes);
    }

    private List<String> normalizeLineIds(List<String> lineIds) {
        if (lineIds == null || lineIds.isEmpty()) {
            throw new IllegalArgumentException("监测点 ID 列表不能为空");
        }
        LinkedHashSet<String> normalized = new LinkedHashSet<String>();
        for (String lineId : lineIds) {
            if (lineId == null) {
                throw new IllegalArgumentException("监测点 ID 不能为空");
            }
            String normalizedLineId = lineId.trim();
            if (normalizedLineId.isEmpty()) {
                throw new IllegalArgumentException("监测点 ID 不能为空");
            }
            if (normalizedLineId.length() > 32) {
                throw new IllegalArgumentException("监测点 ID 长度不能超过 32 位");
            }
            normalized.add(normalizedLineId);
        }
        return new ArrayList<String>(normalized);
    }
}
