import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BreakageRecord } from '../../domain/entities/breakage-record.entity';
import { IBreakageRepository } from '../../domain/repositories/breakage.repository.interface';

@Injectable()
export class BreakageRepository implements IBreakageRepository {
  constructor(
    @InjectRepository(BreakageRecord)
    private readonly repo: Repository<BreakageRecord>,
  ) {}

  async save(record: BreakageRecord): Promise<BreakageRecord> {
    return this.repo.save(record);
  }

  create(data: Partial<BreakageRecord>): BreakageRecord {
    return this.repo.create(data);
  }

  async findByIssuerId(issuerId: string): Promise<BreakageRecord[]> {
    return this.repo.find({
      where: { issuerId },
      order: { calculatedAt: 'DESC' },
    });
  }
}
