// ABOUTME: This file defines security states for the security BLoC pattern
// ABOUTME: Includes authentication status, security configuration, and operational states

import 'package:equatable/equatable.dart';
import '../../models/security/auth_method.dart';
import '../../models/security/security_settings.dart';
import '../../services/auth_service.dart';

/// Base class for all security states
abstract class SecurityState extends Equatable {
  const SecurityState();

  @override
  List<Object?> get props => [];
}

/// Initial state when security BLoC is created
class SecurityInitialState extends SecurityState {
  const SecurityInitialState();
}

/// State when security system is being initialized
class SecurityInitializingState extends SecurityState {
  const SecurityInitializingState();
}

/// State when security system is ready and operational
class SecurityReadyState extends SecurityState {
  final SecuritySettings settings;
  final SecurityStatus status;
  final bool hasKeyPair;
  final BiometricAvailability biometricAvailability;

  const SecurityReadyState({
    required this.settings,
    required this.status,
    required this.hasKeyPair,
    required this.biometricAvailability,
  });

  @override
  List<Object?> get props => [settings, status, hasKeyPair, biometricAvailability];

  SecurityReadyState copyWith({
    SecuritySettings? settings,
    SecurityStatus? status,
    bool? hasKeyPair,
    BiometricAvailability? biometricAvailability,
  }) {
    return SecurityReadyState(
      settings: settings ?? this.settings,
      status: status ?? this.status,
      hasKeyPair: hasKeyPair ?? this.hasKeyPair,
      biometricAvailability: biometricAvailability ?? this.biometricAvailability,
    );
  }
}

/// State when authentication is being performed
class SecurityAuthenticatingState extends SecurityState {
  final AuthMethod method;

  const SecurityAuthenticatingState({
    required this.method,
  });

  @override
  List<Object?> get props => [method];
}

/// State when user is successfully authenticated
class SecurityAuthenticatedState extends SecurityState {
  final SecuritySettings settings;
  final SecurityStatus status;
  final SessionInfo? sessionInfo;
  final DateTime authenticatedAt;

  const SecurityAuthenticatedState({
    required this.settings,
    required this.status,
    this.sessionInfo,
    DateTime? authenticatedAt,
  }) : authenticatedAt = authenticatedAt ?? const DateTime.fromMillisecondsSinceEpoch(0);

  @override
  List<Object?> get props => [settings, status, sessionInfo, authenticatedAt];

  SecurityAuthenticatedState copyWith({
    SecuritySettings? settings,
    SecurityStatus? status,
    SessionInfo? sessionInfo,
    DateTime? authenticatedAt,
  }) {
    return SecurityAuthenticatedState(
      settings: settings ?? this.settings,
      status: status ?? this.status,
      sessionInfo: sessionInfo ?? this.sessionInfo,
      authenticatedAt: authenticatedAt ?? this.authenticatedAt,
    );
  }
}

/// State when authentication is required
class SecurityUnauthenticatedState extends SecurityState {
  final SecuritySettings settings;
  final AuthMethod requiredMethod;
  final bool isLocked;
  final Duration? lockTimeRemaining;
  final int failedAttempts;

  const SecurityUnauthenticatedState({
    required this.settings,
    required this.requiredMethod,
    required this.isLocked,
    this.lockTimeRemaining,
    required this.failedAttempts,
  });

  @override
  List<Object?> get props => [settings, requiredMethod, isLocked, lockTimeRemaining, failedAttempts];

  SecurityUnauthenticatedState copyWith({
    SecuritySettings? settings,
    AuthMethod? requiredMethod,
    bool? isLocked,
    Duration? lockTimeRemaining,
    int? failedAttempts,
  }) {
    return SecurityUnauthenticatedState(
      settings: settings ?? this.settings,
      requiredMethod: requiredMethod ?? this.requiredMethod,
      isLocked: isLocked ?? this.isLocked,
      lockTimeRemaining: lockTimeRemaining ?? this.lockTimeRemaining,
      failedAttempts: failedAttempts ?? this.failedAttempts,
    );
  }
}

/// State when security setup is needed (first time)
class SecuritySetupRequiredState extends SecurityState {
  final List<BiometricType> availableBiometrics;
  final bool canUseBiometric;

  const SecuritySetupRequiredState({
    required this.availableBiometrics,
    required this.canUseBiometric,
  });

  @override
  List<Object?> get props => [availableBiometrics, canUseBiometric];
}

/// State when setting up authentication method
class SecuritySetupState extends SecurityState {
  final AuthMethod setupMethod;
  final bool isInProgress;
  final String? errorMessage;

  const SecuritySetupState({
    required this.setupMethod,
    required this.isInProgress,
    this.errorMessage,
  });

  @override
  List<Object?> get props => [setupMethod, isInProgress, errorMessage];

  SecuritySetupState copyWith({
    AuthMethod? setupMethod,
    bool? isInProgress,
    String? errorMessage,
  }) {
    return SecuritySetupState(
      setupMethod: setupMethod ?? this.setupMethod,
      isInProgress: isInProgress ?? this.isInProgress,
      errorMessage: errorMessage ?? this.errorMessage,
    );
  }
}

/// State when performing key operations
class SecurityKeyOperationState extends SecurityState {
  final String operation;
  final bool isInProgress;
  final double? progress;

  const SecurityKeyOperationState({
    required this.operation,
    required this.isInProgress,
    this.progress,
  });

  @override
  List<Object?> get props => [operation, isInProgress, progress];

  SecurityKeyOperationState copyWith({
    String? operation,
    bool? isInProgress,
    double? progress,
  }) {
    return SecurityKeyOperationState(
      operation: operation ?? this.operation,
      isInProgress: isInProgress ?? this.isInProgress,
      progress: progress ?? this.progress,
    );
  }
}

/// State when an error occurs
class SecurityErrorState extends SecurityState {
  final String error;
  final String? code;
  final dynamic cause;
  final bool canRetry;

  const SecurityErrorState({
    required this.error,
    this.code,
    this.cause,
    this.canRetry = true,
  });

  @override
  List<Object?> get props => [error, code, cause, canRetry];
}

/// State when loading/processing
class SecurityLoadingState extends SecurityState {
  final String operation;
  final double? progress;

  const SecurityLoadingState({
    required this.operation,
    this.progress,
  });

  @override
  List<Object?> get props => [operation, progress];
}

/// State when security settings are being updated
class SecurityUpdatingState extends SecurityState {
  final String updateType;

  const SecurityUpdatingState({
    required this.updateType,
  });

  @override
  List<Object?> get props => [updateType];
}

/// State when security maintenance is running
class SecurityMaintenanceState extends SecurityState {
  final List<String> tasks;
  final String? currentTask;
  final double? progress;

  const SecurityMaintenanceState({
    required this.tasks,
    this.currentTask,
    this.progress,
  });

  @override
  List<Object?> get props => [tasks, currentTask, progress];
}

/// State when session is about to expire
class SecuritySessionWarningState extends SecurityState {
  final Duration timeRemaining;
  final SecuritySettings settings;
  final SecurityStatus status;

  const SecuritySessionWarningState({
    required this.timeRemaining,
    required this.settings,
    required this.status,
  });

  @override
  List<Object?> get props => [timeRemaining, settings, status];
}

/// State when performing backup/restore operations
class SecurityBackupState extends SecurityState {
  final String operation;
  final bool isInProgress;
  final double? progress;
  final Map<String, dynamic>? backupData;

  const SecurityBackupState({
    required this.operation,
    required this.isInProgress,
    this.progress,
    this.backupData,
  });

  @override
  List<Object?> get props => [operation, isInProgress, progress, backupData];
}

/// State when security validation is being performed
class SecurityValidatingState extends SecurityState {
  final String validationType;
  final bool isValid;
  final List<String> errors;

  const SecurityValidatingState({
    required this.validationType,
    required this.isValid,
    required this.errors,
  });

  @override
  List<Object?> get props => [validationType, isValid, errors];
}

/// Mixin for states that include security settings
mixin SecuritySettingsStateMixin on SecurityState {
  SecuritySettings get settings;
}

/// Mixin for states that include authentication status
mixin SecurityStatusStateMixin on SecurityState {
  SecurityStatus get status;
}
