import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BaseRepository } from '@edr/api-common';
import { Repository, FindOptionsWhere } from 'typeorm';
import { WorkOrder, WorkOrderStatus } from './entities/work-order.entity';

@Injectable()
export class WorkOrderRepository extends BaseRepository<WorkOrder> {
  constructor(
    @InjectRepository(WorkOrder)
    private readonly workOrderRepository: Repository<WorkOrder>,
  ) {
    super(workOrderRepository);
  }

  async findFiltered(filters: { vehicleId?: string; status?: WorkOrderStatus }) {
    const where: FindOptionsWhere<WorkOrder> = {};
    if (filters.vehicleId) where.vehicleId = filters.vehicleId;
    if (filters.status) where.status = filters.status;
    return this.workOrderRepository.find({
      where,
      order: { openedAt: 'DESC' },
    });
  }
}
