import 'package:logging/logging.dart';

import 'package:uuid/uuid.dart';

import 'package:pshared/api/requests/tokens/session_identifier.dart';
import 'package:pshared/api/requests/verification/login.dart';
import 'package:pshared/api/responses/login.dart';
import 'package:pshared/api/responses/verification/response.dart';
import 'package:pshared/data/mapper/account/account.dart';
import 'package:pshared/data/mapper/session_identifier.dart';
import 'package:pshared/data/mapper/verification/purpose.dart';
import 'package:pshared/models/account/account.dart';
import 'package:pshared/models/auth/pending_login.dart';
import 'package:pshared/models/verification/purpose.dart';
import 'package:pshared/service/authorization/service.dart';
import 'package:pshared/service/authorization/storage.dart';
import 'package:pshared/service/services.dart';
import 'package:pshared/utils/http/requests.dart';


class VerificationService {
  static final _logger = Logger('service.verification');
  static const String _objectType = Services.verification;

  static Future<VerificationResponse> requestLoginCode(
    PendingLogin pending, {
    String? target,
  }) async {
    _logger.fine('Requesting login confirmation code');
    final response = await getPOSTResponse(
      _objectType,
      '',
      LoginVerificationRequest(
        target: target,
        idempotencyKey: Uuid().v4(),
      ).toJson(),
      authToken: pending.pendingToken.token,
    );
    return VerificationResponse.fromJson(response);
  }

  static Future<VerificationResponse> resendLoginCode(
    PendingLogin pending, {
    String? target,
  }) async {
    _logger.fine('Resending login confirmation code');
    final response = await getPOSTResponse(
      _objectType,
      '/resend',
      LoginVerificationRequest(
        target: target,
        idempotencyKey: pending.idempotencyKey ?? Uuid().v4(),
      ).toJson(),
      authToken: pending.pendingToken.token,
    );
    return VerificationResponse.fromJson(response);
  }

  static Future<Account> confirmLoginCode({
    required PendingLogin pending,
    required String code,
    String? target,
  }) async {
    _logger.fine('Confirming login code');
    final response = await getPOSTResponse(
      _objectType,
      '/verify',
      LoginCodeVerifyicationRequest(
        code: code,
        target: target,
        sessionIdentifier: pending.session.toDTO(),
        idempotencyKey: pending.idempotencyKey ?? Uuid().v4(),
      ).toJson(),
      authToken: pending.pendingToken.token,
    );

    final loginResponse = LoginResponse.fromJson(response);
    await AuthorizationStorage.updateToken(loginResponse.accessToken);
    await AuthorizationStorage.updateRefreshToken(loginResponse.refreshToken);
    return loginResponse.account.toDomain();
  }

  static Future<VerificationResponse> requestPayoutCode({
    String? target,
    String? idempotencyKey,
  }) async {
    _logger.fine('Requesting payout confirmation code');
    final response = await AuthorizationService.getPOSTResponse(
      _objectType,
      '',
      LoginVerificationRequest(
        purpose: VerificationPurpose.payout.toDTO(),
        target: target,
        idempotencyKey: idempotencyKey ?? Uuid().v4(),
      ).toJson(),
    );
    return VerificationResponse.fromJson(response);
  }

  static Future<VerificationResponse> resendPayoutCode({
    required String idempotencyKey,
    String? target,
  }) async {
    _logger.fine('Resending payout confirmation code');
    final response = await AuthorizationService.getPOSTResponse(
      _objectType,
      '/resend',
      LoginVerificationRequest(
        purpose: VerificationPurpose.payout.toDTO(),
        target: target,
        idempotencyKey: idempotencyKey,
      ).toJson(),
    );
    return VerificationResponse.fromJson(response);
  }

  static Future<void> confirmPayoutCode({
    required String code,
    required String idempotencyKey,
    String? target,
  }) async {
    _logger.fine('Confirming payout code');
    await AuthorizationService.getPOSTResponse(
      _objectType,
      '/verify',
      LoginCodeVerifyicationRequest(
        purpose: VerificationPurpose.payout.toDTO(),
        target: target,
        idempotencyKey: idempotencyKey,
        code: code,
        sessionIdentifier: const SessionIdentifierDTO(
          clientId: '',
          deviceId: '',
        ),
      ).toJson(),
    );
  }
}
