/// Order Repository Implementation
///
/// Implements the order repository interface.
library;

import 'package:worker/features/orders/data/datasources/order_remote_datasource.dart';
import 'package:worker/features/orders/domain/entities/order_status.dart';
import 'package:worker/features/orders/domain/entities/payment_term.dart';
import 'package:worker/features/orders/domain/repositories/order_repository.dart';

/// Order Repository Implementation
class OrderRepositoryImpl implements OrderRepository {
  const OrderRepositoryImpl(this._remoteDataSource);

  final OrderRemoteDataSource _remoteDataSource;

  @override
  Future<List<OrderStatus>> getOrderStatusList() async {
    try {
      final models = await _remoteDataSource.getOrderStatusList();
      return models.map((model) => model.toEntity()).toList();
    } catch (e) {
      throw Exception('Failed to get order status list: $e');
    }
  }

  @override
  Future<List<PaymentTerm>> getPaymentTermsList() async {
    try {
      final models = await _remoteDataSource.getPaymentTermsList();
      return models.map((model) => model.toEntity()).toList();
    } catch (e) {
      throw Exception('Failed to get payment terms list: $e');
    }
  }

  @override
  Future<Map<String, dynamic>> createOrder({
    required List<Map<String, dynamic>> items,
    required Map<String, dynamic> deliveryAddress,
    required String paymentMethod,
    bool needsInvoice = false,
    bool needsNegotiation = false,
    String? notes,
  }) async {
    try {
      return await _remoteDataSource.createOrder(
        items: items,
        deliveryAddress: deliveryAddress,
        paymentMethod: paymentMethod,
        needsInvoice: needsInvoice,
        needsNegotiation: needsNegotiation,
        notes: notes,
      );
    } catch (e) {
      throw Exception('Failed to create order: $e');
    }
  }

  @override
  Future<Map<String, dynamic>> generateQrCode(String orderId) async {
    try {
      return await _remoteDataSource.generateQrCode(orderId);
    } catch (e) {
      throw Exception('Failed to generate QR code: $e');
    }
  }
}
