/// Invoice Repository Implementation
///
/// Implements the invoice repository interface.
library;

import 'package:worker/features/invoices/data/datasources/invoice_remote_datasource.dart';
import 'package:worker/features/invoices/domain/entities/invoice.dart';
import 'package:worker/features/invoices/domain/repositories/invoice_repository.dart';

/// Invoice Repository Implementation
class InvoiceRepositoryImpl implements InvoiceRepository {
  const InvoiceRepositoryImpl(this._remoteDataSource);

  final InvoiceRemoteDataSource _remoteDataSource;

  @override
  Future<List<Invoice>> getInvoicesList({
    int limitStart = 0,
    int limitPageLength = 0,
  }) async {
    try {
      final invoicesData = await _remoteDataSource.getInvoicesList(
        limitStart: limitStart,
        limitPageLength: limitPageLength,
      );
      // Convert Model → Entity
      return invoicesData.map((model) => model.toEntity()).toList();
    } catch (e) {
      throw Exception('Failed to get invoices list: $e');
    }
  }

  @override
  Future<Invoice> getInvoiceDetail(String name) async {
    try {
      final invoiceData = await _remoteDataSource.getInvoiceDetail(name);
      // Convert Model → Entity
      return invoiceData.toEntity();
    } catch (e) {
      throw Exception('Failed to get invoice detail: $e');
    }
  }
}
