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/features/auth/data/models/auth_model.dart';

abstract class IAuthRemoteDatasource {
  Future<ApiResponseModel<LoginResponse>> login({
    required String identifier,
    required String password,
  });

  Future<ApiResponseModel<void>> register(RegisterRequest request);
}

class AuthRemoteDatasourceImpl implements IAuthRemoteDatasource {
  final Dio _dio;

  AuthRemoteDatasourceImpl(this._dio);

  @override
  Future<ApiResponseModel<LoginResponse>> login({
    required String identifier,
    required String password,
  }) async {
    final response = await _dio.post(
      "${AppUrl.LOGIN}",
      data: {"identifier": identifier, "password": password},
    );

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

  @override
  Future<ApiResponseModel<void>> register(RegisterRequest request) async {
    final response = await _dio.post(
      "${AppUrl.REGISTER}",
      data: request.toJson(),
    );

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