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 { HotelEntity } from '../../../entities/hotel.entity';

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

  async findAllWithDestination(): Promise<HotelEntity[]> {
    const tenantId = this.getTenantId();
    return this.repo.find({
      where: { tenant_id: tenantId, is_deleted: false },
      relations: {
        destination: true,
      },
      order: { name: 'ASC' },
    });
  }

  async findByIdWithDestination(id: string): Promise<HotelEntity | null> {
    const tenantId = this.getTenantId();
    return this.repo.findOne({
      where: { id, tenant_id: tenantId, is_deleted: false },
      relations: {
        destination: true,
      },
    });
  }

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

  async findByShortCode(code: string): Promise<HotelEntity | null> {
    const tenantId = this.getTenantId();
    return this.repo.findOne({
      where: { short_code: code, tenant_id: tenantId, is_deleted: false },
    });
  }
}
