import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BaseRepository } from '@edr/api-common';
import { Repository, FindOptionsWhere } from 'typeorm';
import { Warranty } from './entities/warranty.entity';

@Injectable()
export class WarrantyRepository extends BaseRepository<Warranty> {
  constructor(
    @InjectRepository(Warranty)
    private readonly warrantyRepository: Repository<Warranty>,
  ) {
    super(warrantyRepository);
  }

  async findFiltered(filters: { vehicleId?: string }) {
    const where: FindOptionsWhere<Warranty> = {};
    if (filters.vehicleId) where.vehicleId = filters.vehicleId;
    return this.warrantyRepository.find({
      where,
      order: { expiryDate: 'ASC' },
    });
  }
}
