import 'package:food_delivery_client/core/services/request_handler_service.dart';
import 'package:food_delivery_client/feature/auth/data/models/response/login_response.dart';
import 'package:food_delivery_client/feature/auth/domain/usecases/register_usecase.dart';
import 'package:food_delivery_client/feature/auth/domain/usecases/reset_password_usecase.dart';
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_otp_code_login_usecase.dart';
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_phone_login_usecase.dart';
import 'package:food_delivery_client/food_delivery_client.dart';

abstract class AuthDatasource {
  Future<LoginResponseModel> login({
    required String phoneNumber,
    required String password,
  });

  Future<SuccessModel> verifyPhoneResetPassword({
    required VerifyPhoneNumberParams params,
  });

  Future<SuccessModel> verifyOtpCodeResetPassword({
    required VerifyOtpCodeParams params,
  });

  Future<SuccessModel> resetPassword({required ResetPasswordParams params});

  Future<SuccessModel> verifyPhoneRegister({
    required VerifyPhoneNumberParams params,
  });

  Future<SuccessModel> verifyOtpCodeRegister({
    required VerifyOtpCodeParams params,
  });

  Future<SuccessModel> register({required RegisterParams params});
}

@LazySingleton(as: AuthDatasource)
class AuthDatasourceImpl implements AuthDatasource {
  final RequestHandlerService _requestHandlerService;

  AuthDatasourceImpl(this._requestHandlerService);

  @override
  Future<LoginResponseModel> login({
    required String phoneNumber,
    required String password,
  }) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.login,
      method: RequestMethodEnum.post,
      data: {"password": password, "phoneNumber": phoneNumber},
      fromJson: (response) async => LoginResponseModel.fromJson(response.data),
    );
  }

  @override
  Future<SuccessModel> register({required RegisterParams params}) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.register,
      method: RequestMethodEnum.post,

      data: {
        "firstName": params.firstName,
        "lastName": params.lastName,
        "password": params.password,
        "phoneNumber": params.phoneNumber,
      },

      fromJson: (response) async {
        return SuccessModel.fromJson(response.data);
      },
    );
  }

  @override
  Future<SuccessModel> resetPassword({
    required ResetPasswordParams params,
  }) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.resetPassword,
      method: RequestMethodEnum.post,
      data: {
        "newPassword": params.newPassword,
        "phoneNumber": params.phoneNumber,
      },

      fromJson: (response) async => SuccessModel.fromJson(response.data),
    );
  }

  @override
  Future<SuccessModel> verifyOtpCodeRegister({
    required VerifyOtpCodeParams params,
  }) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.verifyCode,
      method: RequestMethodEnum.post,
      data: {"code": params.otpCode, "phoneNumber": params.phoneNumber},
      fromJson: (response) async => SuccessModel.fromJson(response.data),
    );
  }

  @override
  Future<SuccessModel> verifyOtpCodeResetPassword({
    required VerifyOtpCodeParams params,
  }) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.forgotPassword,
      method: RequestMethodEnum.post,
      data: {"code": params.otpCode, "phoneNumber": params.phoneNumber},
      fromJson: (response) async => SuccessModel.fromJson(response.data),
    );
  }

  @override
  Future<SuccessModel> verifyPhoneRegister({
    required VerifyPhoneNumberParams params,
  }) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.verifyPhone,
      method: RequestMethodEnum.post,

      data: {"phoneNumber": params.phoneNumber},
      fromJson: (response) async => SuccessModel.fromJson(response.data),
    );
  }

  @override
  Future<SuccessModel> verifyPhoneResetPassword({
    required VerifyPhoneNumberParams params,
  }) async {
    return _requestHandlerService.handleRequest(
      path: ApiConst.sendCode,
      method: RequestMethodEnum.post,
      data: {"phoneNumber": params.phoneNumber},
      fromJson: (response) async => SuccessModel.fromJson(response.data),
    );
  }
}
