import { Repository } from 'typeorm';
import { AppDataSource } from '../index';
import { Timezone } from '../entities/Timezone.entity';

export class TimezoneRepository {
  private repo: Repository<Timezone>;

  constructor() {
    this.repo = AppDataSource.getRepository(Timezone);
  }

  count(): Promise<number> {
    return this.repo.count();
  }

  findAllActive(): Promise<Timezone[]> {
    return this.repo.find({
      where: { isActive: true },
      order: { sortOrder: 'ASC', label: 'ASC' },
    });
  }

  findActiveByIanaId(ianaId: string): Promise<Timezone | null> {
    return this.repo.findOne({ where: { ianaId, isActive: true } });
  }

  async upsertMany(rows: Array<Partial<Timezone>>): Promise<void> {
    await this.repo.upsert(rows, ['ianaId']);
  }
}
