import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { TenantAwareRepository } from '@it0/database';
import { EscalationPolicy } from '../../domain/entities/escalation-policy.entity';

@Injectable()
export class EscalationPolicyRepository extends TenantAwareRepository<EscalationPolicy> {
  constructor(dataSource: DataSource) {
    super(dataSource, EscalationPolicy);
  }

  async findBySeverity(severity: string): Promise<EscalationPolicy[]> {
    return this.withRepository((repo) =>
      repo.find({ where: { severity } as any }),
    );
  }

  async findDefault(): Promise<EscalationPolicy | null> {
    return this.withRepository((repo) =>
      repo.findOne({ where: { isDefault: true } as any }),
    );
  }

  async delete(id: string): Promise<void> {
    await this.withRepository((repo) => repo.delete(id));
  }
}
