// 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:equatable/equatable.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';

/// {@template authentication_session}
/// The [AuthenticationSession] object is used to transport and propagate
/// the last event issued by an authentication state change, a user account
/// if connected, and the associated data.
/// {@endtemplate}
class AuthenticationSession<Data> extends Equatable {
  /// {@macro authentication_session}
  const AuthenticationSession({
    required this.latestEvent,
    this.account,
    this.data,
  });

  /// Creates a new [AuthenticationSession] from an [AuthenticationChangeEvent].
  factory AuthenticationSession.fromEvent(
    AuthenticationChangeEvent latestEvent, {
    Data? data,
  }) {
    if (latestEvent is AuthenticatedChangeEvent) {
      return AuthenticationSession(
        latestEvent: latestEvent,
        account: latestEvent.account,
        data: data,
      );
    }
    return AuthenticationSession(
      latestEvent: latestEvent,
      data: data,
    );
  }

  /// The last event issued by an authentication state change.
  final AuthenticationChangeEvent latestEvent;

  /// The user account if connected.
  final Account? account;

  /// The associated data.
  final Data? data;

  @override
  List<Object?> get props => [
        latestEvent,
        account,
        data,
      ];

  @override
  bool? get stringify => true;
}
