import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BaseRepository } from '@edr/api-common';
import { Repository, FindOptionsWhere } from 'typeorm';
import { ComplianceRecord, ComplianceType } from './entities/compliance-record.entity';

@Injectable()
export class ComplianceRepository extends BaseRepository<ComplianceRecord> {
  constructor(
    @InjectRepository(ComplianceRecord)
    private readonly complianceRepository: Repository<ComplianceRecord>,
  ) {
    super(complianceRepository);
  }

  async findWithFilters(filter: { vehicleId?: string; type?: ComplianceType } = {}) {
    const where: FindOptionsWhere<ComplianceRecord> = {};
    if (filter.vehicleId) where.vehicleId = filter.vehicleId;
    if (filter.type) where.type = filter.type;

    return this.complianceRepository.find({
      where,
      order: { expiryDate: 'ASC' },
    });
  }
}
