import 'package:dio/dio.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_url.dart';
import 'package:niogu_ecommerce_v1/core/models/api_response_model.dart';
import 'package:niogu_ecommerce_v1/core/system/system_setting.dart';
import 'package:niogu_ecommerce_v1/features/account/data/models/account_model.dart';

abstract class IAccountRemoteDatasource {
  Future<ApiResponseModel<CustomerInfoResponse>> updateCustomer(
    CustomerInfoRequest request,
  );

  Future<ApiResponseModel<void>> updatePassword({
    required String oldPassword,
    required String newPassword,
  });

  Future<ApiResponseModel<List<CustomerAddressModel>>> fetchCustomerAddresses();

  Future<ApiResponseModel<void>> saveCustomerAddress(
    CustomerAddressModel request,
  );

  Future<ApiResponseModel<void>> deleteCustomerAddress(String id);
}

class AccountRemoteDatasourceImpl implements IAccountRemoteDatasource {
  final Dio _dio;

  AccountRemoteDatasourceImpl(this._dio);

  @override
  Future<ApiResponseModel<CustomerInfoResponse>> updateCustomer(
    CustomerInfoRequest request,
  ) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.patch(
      "${AppUrl.CUSTOMER}/$customerId",
      data: request.toJson(),
      options: Options(contentType: Headers.jsonContentType),
    );

    return ApiResponseModel.fromJson(
      response.data,
      (json) => CustomerInfoResponse.fromJson(json as Map<String, dynamic>),
    );
  }

  @override
  Future<ApiResponseModel<void>> updatePassword({
    required String oldPassword,
    required String newPassword,
  }) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.patch(
      "${AppUrl.CUSTOMER}/$customerId/password",
      data: {'old_password': oldPassword, 'new_password': newPassword},
    );

    return ApiResponseModel.fromJson(response.data, (_) => null);
  }

  @override
  Future<ApiResponseModel<List<CustomerAddressModel>>>
  fetchCustomerAddresses() async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.get("${AppUrl.CUSTOMER}/$customerId/addresses");

    return ApiResponseModel.fromJson(
      response.data,
      (json) => (json as List)
          .map((e) => CustomerAddressModel.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  @override
  Future<ApiResponseModel<void>> saveCustomerAddress(
    CustomerAddressModel request,
  ) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.post(
      "${AppUrl.CUSTOMER}/$customerId",
      data: request.toJson(),
      options: Options(contentType: Headers.jsonContentType),
    );

    return ApiResponseModel.fromJson(response.data, (_) => null);
  }

  @override
  Future<ApiResponseModel<void>> deleteCustomerAddress(String id) async {
    final customerId = await SystemSetting.getCurrentCustomerId();

    final response = await _dio.delete(
      "${AppUrl.CUSTOMER}/$customerId/addresses/$id",
    );

    return ApiResponseModel.fromJson(response.data, (_) => null);
  }
}
