import Foundation
import Observation
import VNCCore

enum AppState: Equatable, Sendable {
    case launching
    case list
    case error(AppError)
}

enum AppError: Equatable, Sendable {
    case storageUnavailable(String)
}

@Observable
@MainActor
final class AppStateController {
    private(set) var state: AppState = .launching

    func initialize() async {
        // Placeholder for Phase 1: warm storage, load recent connections, etc.
        try? await Task.sleep(for: .milliseconds(150))
        state = .list
    }

    func transition(to newState: AppState) {
        guard isValidTransition(from: state, to: newState) else {
            assertionFailure("Invalid transition: \(state) → \(newState)")
            return
        }
        state = newState
    }

    private func isValidTransition(from: AppState, to: AppState) -> Bool {
        switch (from, to) {
        case (.launching, .list), (.launching, .error): true
        case (.list, .error), (.error, .list): true
        default: false
        }
    }
}
