import Foundation

// MARK: - Generic Action State

/// A generic state machine for tracking async action states.
/// Replaces multiple boolean flags (isCreating, isUpdating, isDeleting, etc.)
/// with a single, type-safe state property.
enum ActionState<ActionType: Equatable>: Equatable {
    case idle
    case loading(ActionType)
    case success(ActionType)
    case error(ActionType, String)

    // MARK: - Convenience Properties

    var isLoading: Bool {
        if case .loading = self { return true }
        return false
    }

    var isSuccess: Bool {
        if case .success = self { return true }
        return false
    }

    var isError: Bool {
        if case .error = self { return true }
        return false
    }

    var errorMessage: String? {
        if case .error(_, let message) = self { return message }
        return nil
    }

    var currentAction: ActionType? {
        switch self {
        case .idle:
            return nil
        case .loading(let action), .success(let action), .error(let action, _):
            return action
        }
    }

    // MARK: - Action-Specific Checks

    /// Check if a specific action is currently loading
    func isLoading(_ action: ActionType) -> Bool {
        if case .loading(let currentAction) = self {
            return currentAction == action
        }
        return false
    }

    /// Check if a specific action completed successfully
    func isSuccess(_ action: ActionType) -> Bool {
        if case .success(let currentAction) = self {
            return currentAction == action
        }
        return false
    }

    /// Check if a specific action failed
    func isError(_ action: ActionType) -> Bool {
        if case .error(let currentAction, _) = self {
            return currentAction == action
        }
        return false
    }
}

// MARK: - Task Action Types

/// Action types for TaskViewModel
enum TaskActionType: Equatable {
    case create
    case update
    case cancel
    case uncancel
    case markInProgress
    case archive
    case unarchive
}

// MARK: - Contractor Action Types

/// Action types for ContractorViewModel
enum ContractorActionType: Equatable {
    case create
    case update
    case delete
    case toggleFavorite
}

// MARK: - Document Action Types

/// Action types for DocumentViewModel
enum DocumentActionType: Equatable {
    case create
    case update
    case delete
    case download
}

// MARK: - Residence Action Types

/// Action types for ResidenceViewModel
enum ResidenceActionType: Equatable {
    case create
    case update
    case delete
    case join
    case leave
    case generateReport
}

// MARK: - Auth Action Types

/// Action types for authentication operations
enum AuthActionType: Equatable {
    case login
    case logout
    case register
    case verifyEmail
    case resendVerification
    case forgotPassword
    case resetPassword
    case updateProfile
}
