import 'package:baseproject/core/common/index.dart';
import 'package:baseproject/features/repositories/hra_repository.dart';
import 'package:baseproject/features/repositories/hra_repository_models.dart';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';

@lazySingleton
class OrderUseCases {
  OrderUseCases(this._hraRepository);

  final HraRepository _hraRepository;

  Future<Either<String, OrderDtoFilterResult>> getMyOrders({
    required int pageIndex,
    required int pageSize,
  }) async {
    try {
      final query = OrderGetListQuery(
        pageIndex: pageIndex,
        pageSize: pageSize,
      );

      final result = await _hraRepository.orderListMy(query);

      if (result.data == null || result.success != true) {
        return Left<String, OrderDtoFilterResult>(
          result.message ?? 'Không lấy được danh sách khóa học đã mua',
        );
      }

      return Right<String, OrderDtoFilterResult>(result.data!);
    } catch (ex) {
      showErrorMessage(ex.toString());
      return Left<String, OrderDtoFilterResult>(ex.toString());
    }
  }

  Future<Either<String, OrderDto>> getOrderDetail(int id) async {
    try {
      final OrderDtoApiResponse result = await _hraRepository.orderId(id);

      if (result.data == null || result.success != true) {
        return Left<String, OrderDto>(
          result.message ?? 'Không lấy được thông tin đơn hàng',
        );
      }

      return Right<String, OrderDto>(result.data!);
    } catch (ex) {
      return Left<String, OrderDto>(ex.toString());
    }
  }
}

