import 'package:equatable/equatable.dart';
import 'package:krow_domain/krow_domain.dart';
import 'package:staff_authentication/src/domain/ui_entities/auth_mode.dart';

/// Enum representing the current status of the authentication process.
enum AuthStatus {
  /// Initial state, awaiting phone number entry.
  initial,

  /// Authentication operation in progress.
  loading,

  /// OTP has been sent, awaiting code verification.
  codeSent,

  /// User has been successfully authenticated.
  authenticated,

  /// An error occurred during the process.
  error,
}

/// A unified state class for the authentication process.
class AuthState extends Equatable {
  /// The current status of the authentication flow.
  final AuthStatus status;

  /// The ID received from the authentication service, used to verify the OTP.
  final String? verificationId;

  /// The authentication mode (login or signup).
  final AuthMode mode;

  /// The current draft OTP entered by the user.
  final String otp;

  /// The phone number entered by the user.
  final String phoneNumber;

  /// A descriptive message for any error that occurred.
  final String? errorMessage;

  /// The authenticated user's data (available when status is [AuthStatus.authenticated]).
  final User? user;

  const AuthState({
    this.status = AuthStatus.initial,
    this.verificationId,
    this.mode = AuthMode.login,
    this.otp = '',
    this.phoneNumber = '',
    this.errorMessage,
    this.user,
  });

  @override
  List<Object?> get props => <Object?>[
    status,
    verificationId,
    mode,
    otp,
    phoneNumber,
    errorMessage,
    user,
  ];

  /// Convenient helper to check if the status is [AuthStatus.loading].
  bool get isLoading => status == AuthStatus.loading;

  /// Convenient helper to check if the status is [AuthStatus.error].
  bool get hasError => status == AuthStatus.error;

  /// Copies the state with optional new values.
  AuthState copyWith({
    AuthStatus? status,
    String? verificationId,
    AuthMode? mode,
    String? otp,
    String? phoneNumber,
    String? errorMessage,
    User? user,
  }) {
    return AuthState(
      status: status ?? this.status,
      verificationId: verificationId ?? this.verificationId,
      mode: mode ?? this.mode,
      otp: otp ?? this.otp,
      phoneNumber: phoneNumber ?? this.phoneNumber,
      errorMessage: errorMessage ?? this.errorMessage,
      user: user ?? this.user,
    );
  }
}
