import 'dart:developer';

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

import '../../../domain/auth/auth.dart';
import '../datasources/local_data_provider.dart';
import '../datasources/remote_data_provider.dart';

@Injectable(as: IAuthRepository)
class AuthRepository implements IAuthRepository {
  final AuthLocalDataProvider _localDataProvider;
  final AuthRemoteDataProvider _remoteDataProvider;

  final String _logName = 'AuthRepository';

  AuthRepository(this._remoteDataProvider, this._localDataProvider);

  @override
  Future<Either<AuthFailure, CheckPhone>> checkPhone({
    required String phoneNumber,
  }) async {
    try {
      final result = await _remoteDataProvider.checkPhone(
        phoneNumber: phoneNumber,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final auth = result.data!.toDomain();

      return right(auth);
    } catch (e, s) {
      log('checkPhoneError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AuthFailure, Register>> register({
    required String phoneNumber,
    required String name,
    required DateTime birthDate,
  }) async {
    try {
      final result = await _remoteDataProvider.register(
        phoneNumber: phoneNumber,
        name: name,
        birthDate: birthDate,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final auth = result.data!.toDomain();

      return right(auth);
    } catch (e, s) {
      log('registerError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AuthFailure, Verify>> verify({
    required String registrationToken,
    required String otpCode,
  }) async {
    try {
      final result = await _remoteDataProvider.verify(
        registrationToken: registrationToken,
        otpCode: otpCode,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final auth = result.data!.toDomain();

      return right(auth);
    } catch (e, s) {
      log('verifyError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AuthFailure, Login>> setPassword({
    required String registrationToken,
    required String password,
    required String confirmPassword,
  }) async {
    try {
      final result = await _remoteDataProvider.setPassword(
        registrationToken: registrationToken,
        password: password,
        confirmPassword: confirmPassword,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final auth = result.data!.toDomain();

      await _localDataProvider.saveToken(auth.accessToken);
      await _localDataProvider.saveCurrentUser(result.data!.data!.user!);

      return right(auth);
    } catch (e, s) {
      log('setPasswordError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AuthFailure, Login>> login({
    required String phoneNumber,
    required String password,
  }) async {
    try {
      final result = await _remoteDataProvider.login(
        phoneNumber: phoneNumber,
        password: password,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final auth = result.data!.toDomain();

      await _localDataProvider.saveToken(auth.accessToken);
      await _localDataProvider.saveCurrentUser(result.data!.data!.user!);

      return right(auth);
    } catch (e, s) {
      log('loginError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AuthFailure, Resend>> resend({
    required String phoneNumber,
    required String purpose,
  }) async {
    try {
      final result = await _remoteDataProvider.resend(
        phoneNumber: phoneNumber,
        purpose: purpose,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final auth = result.data!.toDomain();

      return right(auth);
    } catch (e, s) {
      log('resendError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<Either<AuthFailure, User>> currentUser() async {
    try {
      User user = await _localDataProvider.currentUser();
      return right(user);
    } catch (e, s) {
      log('currentUserError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }

  @override
  Future<bool> hasToken() async {
    return await _localDataProvider.hasToken();
  }

  @override
  Future<Either<AuthFailure, Unit>> logout() async {
    try {
      await _localDataProvider.deleteAllAuth();
      return right(unit);
    } catch (e, s) {
      log('logoutError', name: _logName, error: e, stackTrace: s);
      return left(const AuthFailure.unexpectedError());
    }
  }
}
