import 'dart:developer';

import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:enaklo_pos/core/constants/variables.dart';
import 'package:enaklo_pos/core/network/dio_client.dart';
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
import 'package:enaklo_pos/data/models/response/payment_methods_response_model.dart';

class PaymentMethodsRemoteDatasource {
  final Dio dio = DioClient.instance;
  Future<Either<String, PaymentMethodsResponseModel>>
      getPaymentMethods() async {
    try {
      final authData = await AuthLocalDataSource().getAuthData();
      final response = await dio.get(
        '${Variables.baseUrl}/api/v1/payment-methods',
        options: Options(
          headers: {
            'Authorization': 'Bearer ${authData.token}',
            'Accept': 'application/json',
          },
        ),
      );

      if (response.statusCode == 200) {
        return Right(PaymentMethodsResponseModel.fromMap(response.data));
      } else {
        return const Left('Failed to get payment methods');
      }
    } on DioException catch (e) {
      log("Dio error: ${e.message}");
      return Left(
          e.response?.data['message'] ?? 'Failed to get payment methods');
    } catch (e) {
      log("Error getting payment methods: $e");
      return Left('Unexpected error');
    }
  }
}
