import 'package:logging/logging.dart';
import 'package:pshared/api/requests/verification/resend.dart';
import 'package:pshared/service/device_id.dart';

import 'package:share_plus/share_plus.dart';

import 'package:pshared/api/requests/signup.dart';
import 'package:pshared/api/responses/account.dart';
import 'package:pshared/api/requests/login_data.dart';
import 'package:pshared/api/requests/password/change.dart';
import 'package:pshared/api/requests/password/forgot.dart';
import 'package:pshared/api/requests/password/reset.dart';
import 'package:pshared/data/mapper/account/account.dart';
import 'package:pshared/models/account/account.dart';
import 'package:pshared/models/auth/login_outcome.dart';
import 'package:pshared/service/authorization/service.dart';
import 'package:pshared/service/files.dart';
import 'package:pshared/service/services.dart';
import 'package:pshared/utils/http/requests.dart';


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

  static Future<LoginOutcome> login(LoginData login) async {
    _logger.fine('Logging in');
    return AuthorizationService.login(_objectType, login);
  }

  static Future<Account> restore() async {
    return AuthorizationService.restore();
  }

  static Future<void> signup(SignupRequest request) async {
    // Use regular HTTP for public signup endpoint (no auth needed)
    await getPOSTResponse(_objectType, 'signup', request.toJson());
  }

  static Future<void> logout() async {
    _logger.fine('Logging out');
    return AuthorizationService.logout();
  }

  static Future<Account> _getAccount(Future<Map<String, dynamic>> future) async {
    final response = await future;
    return AccountResponse.fromJson(response).account.toDomain();
  }

  static Future<Account> update(Account account) async {
    _logger.fine('Patching account ${account.id}');
    // Use AuthorizationService for authenticated operations
    return _getAccount(AuthorizationService.getPUTResponse(_objectType, '', account.toDTO().toJson()));
  }

  static Future<void> forgotPassword(String email) async {
    _logger.fine('Requesting password reset for email: $email');
    await getPUTResponse(_objectType, 'password', ForgotPasswordRequest.build(login: email).toJson());
  }

  static Future<void> resendVerificationEmail(String email) async {
    _logger.fine('Resending verification email');
    await getPUTResponse(_objectType, 'email', ResendVerificationEmailRequest.build(login: email).toJson());
  }

  static Future<void> verifyEmail(String token) async {
    _logger.fine('Verifying email');
    await getGETResponse(_objectType, 'verify/$token');
  }

  static Future<void> resetPassword(String accountRef, String token, String newPassword) async {
    _logger.fine('Resetting password for account: $accountRef');
    await getPOSTResponse(_objectType, 'password/reset/$accountRef/$token', ResetPasswordRequest.build(password: newPassword).toJson());
  }

  static Future<Account> changePassword(String oldPassword, String newPassword) async {
    _logger.fine('Changing password');
    return _getAccount(AuthorizationService.getPATCHResponse(
      _objectType,
      'password',
      ChangePassword(
        oldPassword: oldPassword, 
        newPassword: newPassword, 
        deviceId: await DeviceIdManager.getDeviceId(),
      ).toJson(),
    ));
  }

  static Future<String> uploadAvatar(String id, XFile avatarFile) async {
    _logger.fine('Uploading avatar');
    return FilesService.uploadImage(_objectType, id, avatarFile);
  }
}
