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

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

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

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

  async findByDestination(destinationId: string): Promise<SeasonEntity[]> {
    const tenantId = this.getTenantId();
    return this.repo.find({
      where: { destination_id: destinationId, tenant_id: tenantId, is_deleted: false } as any,
      order: { sort_order: 'ASC' },
    });
  }
}
