import 'dart:developer';

import 'package:data_channel/data_channel.dart';
import 'package:injectable/injectable.dart';

import '../../../common/api/api_client.dart';
import '../../../common/api/api_failure.dart';
import '../../../common/function/app_function.dart';
import '../../../common/url/api_path.dart';
import '../../../domain/customer/customer.dart';
import '../customer_dtos.dart';

@injectable
class CustomerRemoteDataProvider {
  final ApiClient _apiClient;
  final String _logName = "CustomerRemoteDataProvider";

  CustomerRemoteDataProvider(this._apiClient);

  Future<DC<CustomerFailure, CustomerPointDto>> fetchCustomerPoint() async {
    try {
      final response = await _apiClient.get(
        ApiPath.customerPoint,
        headers: getAuthorizationHeader(),
      );

      if (response.data['code'] == 401) {
        return DC.error(
          CustomerFailure.serverError(
            ApiFailure.unauthorized('Session Expired'),
          ),
        );
      }

      if (response.data['status'] == false) {
        return DC.error(
          CustomerFailure.dynamicErrorMessage(
            'Terjadi kesalahan coba lagi nanti',
          ),
        );
      }

      final dto = CustomerPointDto.fromJson(response.data['data']);
      return DC.data(dto);
    } on ApiFailure catch (e, s) {
      log('fetchCustomerPoint', name: _logName, error: e, stackTrace: s);
      return DC.error(CustomerFailure.serverError(e));
    }
  }
}
