import type { PosCashSessionRepository } from '../domain/ports.js';
import type { PosCashSession } from '../domain/cash-session.js';
import { CashSessionNotOpenError } from '../domain/errors.js';

export interface CloseCashSessionInput {
  sessionId: string;
  closingCashCents: number;
  actualCashCents: number;
  notes?: string;
}

export class CloseCashSessionUseCase {
  constructor(private readonly sessionRepo: PosCashSessionRepository) {}

  async execute(input: CloseCashSessionInput): Promise<PosCashSession> {
    const existing = await this.sessionRepo.findById(input.sessionId);
    if (!existing) throw new CashSessionNotOpenError(input.sessionId);
    if (existing.status === 'CLOSED') throw new CashSessionNotOpenError(input.sessionId);

    return this.sessionRepo.close(input);
  }
}
