package com.sino.mci.channel.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sino.mci.channel.entity.SyncTask;
import com.sino.mci.channel.repository.SyncTaskMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SyncTaskService {

    private final SyncTaskMapper syncTaskMapper;

    @Transactional
    public SyncTask createTask(String tenantCode, String taskType, String channelType, Long targetId, String targetCode) {
        SyncTask task = new SyncTask();
        task.setTenantCode(tenantCode);
        task.setTaskType(taskType);
        task.setChannelType(channelType);
        task.setTargetId(targetId);
        task.setTargetCode(targetCode);
        task.setTaskStatus(SyncTask.STATUS_PENDING);
        task.setSyncedCount(0);
        task.setFailedCount(0);
        task.setRetryCount(0);
        task.setDeleted(0);
        syncTaskMapper.insert(task);
        return task;
    }

    @Transactional
    public void markRunning(Long taskId) {
        SyncTask task = syncTaskMapper.selectById(taskId);
        if (task == null) {
            return;
        }
        task.setTaskStatus(SyncTask.STATUS_RUNNING);
        task.setUpdateTime(LocalDateTime.now());
        syncTaskMapper.updateById(task);
    }

    @Transactional
    public void markSuccess(Long taskId, int syncedCount) {
        SyncTask task = syncTaskMapper.selectById(taskId);
        if (task == null) {
            return;
        }
        task.setTaskStatus(SyncTask.STATUS_SUCCESS);
        task.setSyncedCount(syncedCount);
        task.setFinishTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());
        syncTaskMapper.updateById(task);
    }

    @Transactional
    public void markFailed(Long taskId, String errorMessage) {
        SyncTask task = syncTaskMapper.selectById(taskId);
        if (task == null) {
            return;
        }
        task.setTaskStatus(SyncTask.STATUS_FAILED);
        task.setErrorMessage(errorMessage);
        task.setFinishTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());
        syncTaskMapper.updateById(task);
    }

    public SyncTask getTask(Long taskId, String tenantCode) {
        SyncTask task = syncTaskMapper.selectById(taskId);
        if (task == null) {
            return null;
        }
        if (tenantCode != null && !tenantCode.equals(task.getTenantCode())) {
            return null;
        }
        return task;
    }

    public SyncTask getTaskById(Long taskId) {
        return syncTaskMapper.selectById(taskId);
    }

    public List<SyncTask> listRecentTasks(String tenantCode, String taskType, Long targetId, int limit) {
        return syncTaskMapper.selectList(
                new LambdaQueryWrapper<SyncTask>()
                        .eq(SyncTask::getTenantCode, tenantCode)
                        .eq(SyncTask::getTaskType, taskType)
                        .eq(targetId != null, SyncTask::getTargetId, targetId)
                        .orderByDesc(SyncTask::getId)
                        .last("LIMIT " + limit));
    }
}
