/// Order Repository Provider
///
/// Provides the order repository instance and related providers.
library;

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/orders/data/datasources/order_remote_datasource.dart';
import 'package:worker/features/orders/data/datasources/order_status_local_datasource.dart';
import 'package:worker/features/orders/data/repositories/order_repository_impl.dart';
import 'package:worker/features/orders/domain/repositories/order_repository.dart';

part 'order_repository_provider.g.dart';

/// Order Repository Provider
@riverpod
Future<OrderRepository> orderRepository(Ref ref) async {
  final dioClient = await ref.watch(dioClientProvider.future);
  final remoteDataSource = OrderRemoteDataSource(dioClient);
  final statusLocalDataSource = OrderStatusLocalDataSource();
  return OrderRepositoryImpl(remoteDataSource, statusLocalDataSource);
}

/// Create Order Provider
///
/// Creates a new order with the given parameters.
@riverpod
Future<Map<String, dynamic>> createOrder(
  Ref ref, {
  required List<Map<String, dynamic>> items,
  required Map<String, dynamic> deliveryAddress,
  required String paymentMethod,
  bool needsInvoice = false,
  bool ignorePricingRule = false,
  bool contractRequest = false,
  String? notes,
}) async {
  final repository = await ref.watch(orderRepositoryProvider.future);
  return await repository.createOrder(
    items: items,
    deliveryAddress: deliveryAddress,
    paymentMethod: paymentMethod,
    needsInvoice: needsInvoice,
    ignorePricingRule: ignorePricingRule,
    contractRequest: contractRequest,
    notes: notes,
  );
}
