// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:firebase_auth/firebase_auth.dart';
import 'package:wyatt_authentication_bloc/src/core/exceptions/exceptions.dart';
import 'package:wyatt_authentication_bloc/src/domain/entities/account.dart';

/// {@template account_model}
/// Account Model to parse Firebase User data
/// {@endtemplate}
class AccountModel extends Account {
  /// {@macro account_model}
  factory AccountModel.fromFirebaseUserCredential(
    UserCredential? userCredential, {
    required String? accessToken,
  }) {
    final user = userCredential?.user;
    if (user != null) {
      final providerId =
          (user.providerData.isEmpty) ? '' : user.providerData.first.providerId;
      return AccountModel._(
        user: user,
        id: user.uid,
        emailVerified: user.emailVerified,
        isAnonymous: user.isAnonymous,
        providerId: providerId,
        creationTime: user.metadata.creationTime,
        lastSignInTime: user.metadata.lastSignInTime,
        isNewUser: userCredential?.additionalUserInfo?.isNewUser,
        email: user.email,
        phoneNumber: user.phoneNumber,
        photoURL: user.photoURL,
        accessToken: accessToken,
      );
    } else {
      throw ModelParsingFailureFirebase('null-user', 'User cannot be null');
    }
  }

  /// {@macro account_model}
  factory AccountModel.fromFirebaseUser(
    User? user, {
    required String? accessToken,
  }) {
    if (user != null) {
      final providerId =
          (user.providerData.isEmpty) ? '' : user.providerData.first.providerId;
      return AccountModel._(
        user: user,
        id: user.uid,
        emailVerified: user.emailVerified,
        isAnonymous: user.isAnonymous,
        providerId: providerId,
        creationTime: user.metadata.creationTime,
        lastSignInTime: user.metadata.lastSignInTime,
        isNewUser: false,
        email: user.email,
        phoneNumber: user.phoneNumber,
        photoURL: user.photoURL,
        accessToken: accessToken,
      );
    } else {
      throw ModelParsingFailureFirebase('null-user', 'User cannot be null');
    }
  }

  /// {@macro account_model}
  factory AccountModel.fromSecureStorage({
    required String? id,
    required String? email,
    required String? accessToken,
  }) {
    if (id != null && email != null && accessToken != null) {
      return AccountModel._(
        user: null,
        id: id,
        emailVerified: false,
        isAnonymous: false,
        providerId: '',
        email: email,
        accessToken: accessToken,
      );
    } else {
      throw Exception(
        'null-id-email-access-token'
        'id, email, and accessToken cannot be null',
      );
    }
  }

  const AccountModel._({
    required this.user,
    required super.id,
    required super.emailVerified,
    required super.isAnonymous,
    required super.providerId,
    super.lastSignInTime,
    super.creationTime,
    super.isNewUser,
    super.email,
    super.phoneNumber,
    super.photoURL,
    super.accessToken,
  });

  /// The Firebase User
  final User? user;

  @override
  String? get refreshToken => user?.refreshToken;
}
