// Generated by SwiftModelGenerator
// Do not modify this file manually

import Foundation
import LeStorage
import SwiftUI

@Observable
class BaseRound: SyncedModelObject, SyncedStorable {

    static func resourceName() -> String { return "rounds" }
    static func tokenExemptedMethods() -> [HTTPMethod] { return [] }

    var id: String = Store.randomId()
    var tournament: String = ""
    var index: Int = 0
    var parent: String? = nil
    var format: MatchFormat? = nil
    var startDate: Date? = nil
    var groupStageLoserBracket: Bool = false
    var loserBracketMode: LoserBracketMode = .automatic

    init(
        id: String = Store.randomId(),
        tournament: String = "",
        index: Int = 0,
        parent: String? = nil,
        format: MatchFormat? = nil,
        startDate: Date? = nil,
        groupStageLoserBracket: Bool = false,
        loserBracketMode: LoserBracketMode = .automatic
    ) {
        super.init()
        self.id = id
        self.tournament = tournament
        self.index = index
        self.parent = parent
        self.format = format
        self.startDate = startDate
        self.groupStageLoserBracket = groupStageLoserBracket
        self.loserBracketMode = loserBracketMode
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case _id = "id"
        case _tournament = "tournament"
        case _index = "index"
        case _parent = "parent"
        case _format = "format"
        case _startDate = "startDate"
        case _groupStageLoserBracket = "groupStageLoserBracket"
        case _loserBracketMode = "loserBracketMode"
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decodeIfPresent(String.self, forKey: ._id) ?? Store.randomId()
        self.tournament = try container.decodeIfPresent(String.self, forKey: ._tournament) ?? ""
        self.index = try container.decodeIfPresent(Int.self, forKey: ._index) ?? 0
        self.parent = try container.decodeIfPresent(String.self, forKey: ._parent) ?? nil
        self.format = try container.decodeIfPresent(MatchFormat.self, forKey: ._format) ?? nil
        self.startDate = try container.decodeIfPresent(Date.self, forKey: ._startDate) ?? nil
        self.groupStageLoserBracket = try container.decodeIfPresent(Bool.self, forKey: ._groupStageLoserBracket) ?? false
        self.loserBracketMode = try container.decodeIfPresent(LoserBracketMode.self, forKey: ._loserBracketMode) ?? .automatic
        try super.init(from: decoder)
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.id, forKey: ._id)
        try container.encode(self.tournament, forKey: ._tournament)
        try container.encode(self.index, forKey: ._index)
        try container.encode(self.parent, forKey: ._parent)
        try container.encode(self.format, forKey: ._format)
        try container.encode(self.startDate, forKey: ._startDate)
        try container.encode(self.groupStageLoserBracket, forKey: ._groupStageLoserBracket)
        try container.encode(self.loserBracketMode, forKey: ._loserBracketMode)
        try super.encode(to: encoder)
    }

    func tournamentValue() -> Tournament? {
        return Store.main.findById(tournament)
    }

    func copy(from other: any Storable) {
        guard let round = other as? BaseRound else { return }
        self.id = round.id
        self.tournament = round.tournament
        self.index = round.index
        self.parent = round.parent
        self.format = round.format
        self.startDate = round.startDate
        self.groupStageLoserBracket = round.groupStageLoserBracket
        self.loserBracketMode = round.loserBracketMode
    }

    static func relationships() -> [Relationship] {
        return [
            Relationship(type: Tournament.self, keyPath: \BaseRound.tournament),
        ]
    }

}