import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TaskSchedulerService } from '../scheduler/task-scheduler.service';
import { CreateTaskDto, UpdateTaskDto } from './tasks.dto';
import { InspectionTaskEntity } from './inspection-task.entity';

@Injectable()
export class TasksService {
  constructor(
    @InjectRepository(InspectionTaskEntity)
    private readonly taskRepository: Repository<InspectionTaskEntity>,
    private readonly taskSchedulerService: TaskSchedulerService,
  ) {}

  async getTasks(filters: {
    keyword?: string;
    status?: 'enabled' | 'disabled';
  }) {
    const query = this.taskRepository.createQueryBuilder('task');

    if (filters.keyword) {
      query.andWhere(
        '(task.name LIKE :keyword OR task.siteUrl LIKE :keyword OR task.serviceKey LIKE :keyword)',
        {
          keyword: `%${filters.keyword}%`,
        },
      );
    }

    if (filters.status) {
      query.andWhere('task.status = :status', { status: filters.status });
    }

    const [list, total] = await query
      .orderBy('task.createdAt', 'DESC')
      .getManyAndCount();

    return { list, total };
  }

  async createTask(dto: CreateTaskDto) {
    this.taskSchedulerService.validateCron(dto.cronExpr);

    const task = this.taskRepository.create({
      ...dto,
      status: 'enabled',
      lastRunAt: null,
    });

    await this.taskRepository.save(task);
    await this.taskSchedulerService.syncTask(task.id);

    return {
      taskId: task.id,
      status: task.status,
    };
  }

  async updateTask(id: string, dto: UpdateTaskDto) {
    this.taskSchedulerService.validateCron(dto.cronExpr);

    const task = await this.taskRepository.findOneBy({ id });
    if (!task) {
      throw new NotFoundException('巡检任务不存在');
    }

    task.name = dto.name;
    task.siteUrl = dto.siteUrl;
    task.cronExpr = dto.cronExpr;
    task.serviceKey = dto.serviceKey;
    task.preferredExecutor = dto.preferredExecutor;
    task.allowAgentFallback = dto.allowAgentFallback;
    task.stepsJson = dto.stepsJson;

    await this.taskRepository.save(task);
    await this.taskSchedulerService.syncTask(task.id);

    return {
      taskId: task.id,
      status: task.status,
    };
  }

  async updateStatus(id: string, status: 'enabled' | 'disabled') {
    const task = await this.taskRepository.findOneBy({ id });
    if (!task) {
      throw new NotFoundException('巡检任务不存在');
    }

    task.status = status;
    await this.taskRepository.save(task);
    await this.taskSchedulerService.syncTask(task.id);

    return this.taskRepository.findOneByOrFail({ id });
  }
}
