import Foundation
import SwiftData

@Model
final class ForgejoInstance {
    var serverURL: String
    var username: String
    var name: String
    var isDefault: Bool
    var lastUsed: Date
    var allowSelfSignedCertificates: Bool
    var useTokenAuth: Bool = false

    init(
        serverURL: String, username: String, name: String = "",
        isDefault: Bool = false, allowSelfSignedCertificates: Bool = false,
        useTokenAuth: Bool = false,
    ) {
        self.serverURL = serverURL
        self.username = username
        self.name = name
        self.isDefault = isDefault
        lastUsed = Date()
        self.allowSelfSignedCertificates = allowSelfSignedCertificates
        self.useTokenAuth = useTokenAuth
    }
}

extension ForgejoInstance {
    static func clearDefaults(in instances: [ForgejoInstance]) {
        for instance in instances where instance.isDefault {
            instance.isDefault = false
        }
    }
}
