// SPDX-License-Identifier: LicenseRef-Hive-Proprietary OR GPL-3.0-only

import Foundation

enum MobileVpnStatus: String, Codable {
    case claimed
    case awaitingPermission = "awaiting-permission"
    case waitingRuntime = "waiting-runtime"
    case connecting, connected, reconnecting, stopped, failed, expired
}

struct DeviceSummary: Codable {
    let id: Int
    let name: String
    let version: String
}

struct MobileSession: Codable {
    let id: String
    let status: MobileVpnStatus
    let device: DeviceSummary?
    let connectionId: Int?
    let transport: String?
    let quality: String?
    let latencyMs: Int?
    let errorCode: String?
    let errorMessage: String?
}

struct SessionCredentials: Codable {
    let sessionId: String
    let token: String
    let websocketUrl: String
    let deviceName: String
    let portalReturnUrl: String?
}

struct RedeemResponse: Decodable {
    let session: MobileSession
    let sessionToken: String
    let websocketUrl: String
    let portalReturnUrl: String?
}

struct SessionEnvelope: Codable { let session: MobileSession }

struct VpnConfiguration: Codable {
    struct N3n: Codable {
        let version: String
        let supernodeUrls: [String]
        let community: String
        let key: String
        let preferredTransport: String
        let fallbackTransport: String
        let transportOrder: [String]?
    }
    struct Overlay: Codable {
        let address: String
        let serverAddress: String
        let prefixLength: Int
        let mtu: Int
    }
    struct Route: Codable {
        let ip: String
        let mask: String
        let source: String
        let interfaceName: String?
    }
    struct RuntimeTarget: Codable {
        struct TLSSettings: Codable {
            let caCertificatePem: String?
            let certificateSha256: String?
        }
        let host: String
        let port: Int
        let secure: Bool
        let path: String
        let tls: TLSSettings
    }
    let configVersion: Int
    let sessionId: String
    let connectionId: Int
    let edgeSessionId: String?
    let edgeGeneration: Int?
    let n3n: N3n
    let overlay: Overlay
    let routes: [Route]
    let runtime: RuntimeTarget
}

struct TunnelDiagnostics: Codable, Equatable {
    let phase: String
    let transport: String
    let assignedAddress: String?
    let runtimeAddress: String?
    let includedRoutes: [String]
    let skippedRoutes: [String]
    let packetSummary: String
    let runtimeConnected: Bool
    let lastError: String?
}
