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

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

  /** All active mappings for a destination, joined with vehicle_type for label. */
  async findByDestination(destinationId: string): Promise<DestinationVehicleTypeEntity[]> {
    return this.find({
      where: { destination_id: destinationId },
      relations: {
        vehicle_type: true,
      },
    });
  }

  /** All active mappings across the tenant, joined with both sides for the list page. */
  async findAllWithRelations(): Promise<DestinationVehicleTypeEntity[]> {
    return this.find({
      relations: {
        destination: true,
        vehicle_type: true,
      },
      order: { destination_id: 'ASC' },
    });
  }
}
