using Quartz;

namespace WebApi.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class JobController(ISchedulerFactory schedulerFactory, ILogger<JobController> logger) : ControllerBase
{
    [HttpGet]
    public async Task<BaseResponse<List<JobStatus>>> GetJobsAsync()
    {
        try
        {
            var scheduler = await schedulerFactory.GetScheduler();
            var jobKeys = await scheduler.GetJobKeys(Quartz.Impl.Matchers.GroupMatcher<JobKey>.AnyGroup());
            var jobStatuses = new List<JobStatus>();

            foreach (var jobKey in jobKeys)
            {
                var jobDetail = await scheduler.GetJobDetail(jobKey);
                var triggers = await scheduler.GetTriggersOfJob(jobKey);
                var trigger = triggers.FirstOrDefault();
                
                var status = "Unknown";
                DateTime? nextFireTime = null;

                if (trigger != null)
                {
                    var triggerState = await scheduler.GetTriggerState(trigger.Key);
                    status = triggerState.ToString();
                    nextFireTime = trigger.GetNextFireTimeUtc()?.ToLocalTime().DateTime;
                }

                jobStatuses.Add(new JobStatus
                {
                    Name = jobKey.Name,
                    JobDescription = jobDetail?.Description ?? jobKey.Name,
                    TriggerDescription = trigger?.Description ?? string.Empty,
                    Status = status,
                    NextRunTime = nextFireTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "无"
                });
            }

            return jobStatuses.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "获取任务列表失败");
            return $"获取任务列表失败: {ex.Message}".Fail<List<JobStatus>>();
        }
    }

    [HttpPost]
    public async Task<BaseResponse<bool>> ExecuteAsync([FromBody] JobRequest request)
    {
        try
        {
            var scheduler = await schedulerFactory.GetScheduler();
            await scheduler.TriggerJob(new JobKey(request.JobName));
            return true.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "执行任务失败: {JobName}", request.JobName);
            return $"执行任务失败: {ex.Message}".Fail<bool>();
        }
    }

    [HttpPost]
    public async Task<BaseResponse<bool>> PauseAsync([FromBody] JobRequest request)
    {
        try
        {
            var scheduler = await schedulerFactory.GetScheduler();
            await scheduler.PauseJob(new JobKey(request.JobName));
            return true.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "暂停任务失败: {JobName}", request.JobName);
            return $"暂停任务失败: {ex.Message}".Fail<bool>();
        }
    }

    [HttpPost]
    public async Task<BaseResponse<bool>> ResumeAsync([FromBody] JobRequest request)
    {
        try
        {
            var scheduler = await schedulerFactory.GetScheduler();
            await scheduler.ResumeJob(new JobKey(request.JobName));
            return true.Ok();
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "恢复任务失败: {JobName}", request.JobName);
            return $"恢复任务失败: {ex.Message}".Fail<bool>();
        }
    }

    public class JobStatus
    {
        public string Name { get; set; } = string.Empty;
        public string JobDescription { get; set; } = string.Empty;
        public string TriggerDescription { get; set; } = string.Empty;
        public string Status { get; set; } = string.Empty;
        public string NextRunTime { get; set; } = string.Empty;
    }

    public class JobRequest
    {
        public string JobName { get; set; } = string.Empty;
    }
}
