import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, type FindOptionsWhere } from 'typeorm';
import { ClsService } from 'nestjs-cls';
import { TenantAwareRepository } from '../../../common/repositories/tenant-aware.repository';
import { VehicleTypeEntity } from '../../../entities/vehicle-type.entity';

@Injectable()
export class VehicleTypeRepository extends TenantAwareRepository<VehicleTypeEntity> {
  constructor(
    @InjectRepository(VehicleTypeEntity) repo: Repository<VehicleTypeEntity>,
    cls: ClsService,
  ) {
    super(repo, cls);
  }

  async findAllActive(): Promise<VehicleTypeEntity[]> {
    return this.find({
      where: { is_active: true } as FindOptionsWhere<VehicleTypeEntity>,
      order: { name: 'ASC' },
    });
  }

  async findByName(name: string): Promise<VehicleTypeEntity | null> {
    const tenantId = this.getTenantId();
    return this.repo.findOne({
      where: { name, tenant_id: tenantId, is_deleted: false },
    });
  }
}
