import 'package:taafee_mobile/core/apis/apis.dart';
import 'package:taafee_mobile/features/auth/data_layer/model/login.dart';
import 'package:taafee_mobile/features/auth/data_layer/model/login_response.dart';
import 'package:taafee_mobile/features/auth/data_layer/model/register.dart';
import 'package:taafee_mobile/features/auth/data_layer/model/reset_token.dart';
import 'package:taafee_mobile/features/auth/data_layer/model/verification_code.dart';

import '../../../../core/network/http.dart';
import '../model/user.dart';

class AuthService {
  Future<void> register(RegisterModel registerModel) async {
    Request request = Request(
      EndPoint.register,
      RequestMethod.post,
      body: registerModel.toJson(),
    );
    // no need to use variable (response)
    await request.sendRequest();
  }

  Future<void> verify(VerificationCodeModel verificationCodeModel) async {
    Request request = Request(
      EndPoint.verificationCode,
      RequestMethod.post,
      body: verificationCodeModel.toJson(),
    );

    // no need to use variable (response)

    await request.sendRequest();
  }

  Future<LoginResponseModel> login(LoginModel loginModel) async {
    Request request = Request(
      EndPoint.login,
      RequestMethod.post,
      body: loginModel.toJson(),
    );
    Map<String, dynamic> response = await request.sendRequest();
    return LoginResponseModel.fromJson(response["data"]);
  }

  Future<void> logout() async {
    Request request = Request(
      EndPoint.logout,
      RequestMethod.get,
      authorized: true,
    );

    // no need to use variable (response)

    await request.sendRequest();
  }

  Future<void> resendCode(String email) async {
    Request request = Request(EndPoint.resendCode, RequestMethod.post,
        body: {"email": email});

    // no need to use variable (response)

    await request.sendRequest();
  }

  Future<void> forgotPassword(String email) async {
    Request request = Request(
      EndPoint.forgotPassword,
      RequestMethod.post,
      body: {
        "email": email,
      },
    );

    // no need to use variable (response)

    await request.sendRequest();
  }

  Future<ResetToken> verifyForgotPassword(String email, String code) async {
    Request request = Request(
      EndPoint.verifyForgotPassword,
      RequestMethod.post,
      body: {
        "email": email,
        "reset_code": code,
      },
    );
    Map<String, dynamic> response = await request.sendRequest();

    return ResetToken.fromJson(response);
  }

  Future<void> changePassword(String resetToken, String newPassword) async {
    Request request = Request(
      EndPoint.changePassword,
      RequestMethod.post,
      body: {
        "reset_token": resetToken,
        "new_password": newPassword,
      },
    );

    // no need to use variable (response)

    await request.sendRequest();
  }

  Future<void> deleteAccount(String password) async {
    Request request = Request(EndPoint.deleteAccount, RequestMethod.delete,
        authorized: true, body: {"password": password});
    // no need to use variable (response)
    await request.sendRequest();
  }

  ///----------------show user profile-------------///
  Future<User> showUser({void Function(Object)? onConnectionError}) async {
    Request request = Request(EndPoint.editAccount, RequestMethod.get,
        authorized: true,
        cacheable: true,
        headers: {
          'Accept': 'application/json',
        });
    Map<String, dynamic> response =
        await request.sendRequest(onConnectionError: onConnectionError);
    return User.fromJson(response['data']);
  }

  ///---------------guest---------------------///
  Future<String> guest() async {
    Request request = Request(EndPoint.guest, RequestMethod.post);
    Map<String, dynamic> response = await request.sendRequest();
    return response['token'];
  }

  /// ------------- terminate other sessions --------///
  Future<void> terminateOtherSessions() async {
    Request request =
        Request(EndPoint.terminate, RequestMethod.delete, authorized: true);
    await request.sendRequest();
  }
}
