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

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

  async findAllSorted(): Promise<DestinationEntity[]> {
    const tenantId = this.getTenantId();
    return this.repo.find({
      where: { tenant_id: tenantId, is_deleted: false } as any,
      relations: ['currency'],
      order: { name: 'ASC' },
    });
  }

  async findByShortCode(shortCode: string): Promise<DestinationEntity | null> {
    return this.findOne({
      where: { short_code: shortCode } as FindOptionsWhere<DestinationEntity>,
    });
  }
}
