/**
 * Repository for HookScript.
 * Uses standard TypeORM repository (no schema-per-tenant — uses tenantId column filter).
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { HookScript } from '../../domain/entities/hook-script.entity';

@Injectable()
export class HookScriptRepository {
  constructor(
    @InjectRepository(HookScript)
    private readonly repo: Repository<HookScript>,
  ) {}

  async findByTenantId(tenantId: string): Promise<HookScript[]> {
    return this.repo.find({ where: { tenantId }, order: { createdAt: 'DESC' } });
  }

  async findOneByIdAndTenant(id: string, tenantId: string): Promise<HookScript | null> {
    return this.repo.findOneBy({ id, tenantId });
  }

  async save(entity: HookScript): Promise<HookScript> {
    return this.repo.save(entity);
  }

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