import Foundation
import SwiftData

@Model
public final class SavedConnection {
    @Attribute(.unique) public var id: UUID
    public var displayName: String
    public var host: String
    public var port: Int
    public var username: String
    public var colorTagRaw: String
    public var lastConnectedAt: Date?
    public var preferredEncodings: [String]
    public var keychainTag: String
    public var qualityRaw: String
    public var inputModeRaw: String
    public var viewOnly: Bool
    public var curtainMode: Bool
    public var clipboardSyncEnabled: Bool
    public var notes: String

    public init(
        id: UUID = UUID(),
        displayName: String,
        host: String,
        port: Int = 5900,
        username: String = "",
        colorTag: ColorTag = .blue,
        preferredEncodings: [String] = ["7", "16", "5", "6"],
        keychainTag: String = UUID().uuidString,
        quality: QualityPreset = .adaptive,
        inputMode: InputModePreference = .touch,
        viewOnly: Bool = false,
        curtainMode: Bool = false,
        clipboardSyncEnabled: Bool = true,
        notes: String = ""
    ) {
        self.id = id
        self.displayName = displayName
        self.host = host
        self.port = port
        self.username = username
        self.colorTagRaw = colorTag.rawValue
        self.lastConnectedAt = nil
        self.preferredEncodings = preferredEncodings
        self.keychainTag = keychainTag
        self.qualityRaw = quality.rawValue
        self.inputModeRaw = inputMode.rawValue
        self.viewOnly = viewOnly
        self.curtainMode = curtainMode
        self.clipboardSyncEnabled = clipboardSyncEnabled
        self.notes = notes
    }

    public var colorTag: ColorTag {
        get { ColorTag(rawValue: colorTagRaw) ?? .blue }
        set { colorTagRaw = newValue.rawValue }
    }

    public var quality: QualityPreset {
        get { QualityPreset(rawValue: qualityRaw) ?? .adaptive }
        set { qualityRaw = newValue.rawValue }
    }

    public var inputMode: InputModePreference {
        get { InputModePreference(rawValue: inputModeRaw) ?? .touch }
        set { inputModeRaw = newValue.rawValue }
    }
}

public enum ColorTag: String, CaseIterable, Sendable {
    case red, orange, yellow, green, blue, purple, pink, gray
}

public enum QualityPreset: String, CaseIterable, Sendable {
    case adaptive, high, low
}

public enum InputModePreference: String, CaseIterable, Sendable {
    case touch
    case trackpad
}
