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

import Foundation
import LeStorage
import SwiftUI

@Observable
class BaseMatch: SyncedModelObject, SyncedStorable {

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

    var id: String = Store.randomId()
    var round: String? = nil
    var groupStage: String? = nil
    var startDate: Date? = nil
    var endDate: Date? = nil
    var index: Int = 0
    var format: MatchFormat? = nil
    var servingTeamId: String? = nil
    var winningTeamId: String? = nil
    var losingTeamId: String? = nil
    var name: String? = nil
    var disabled: Bool = false
    var courtIndex: Int? = nil
    var confirmed: Bool = false

    init(
        id: String = Store.randomId(),
        round: String? = nil,
        groupStage: String? = nil,
        startDate: Date? = nil,
        endDate: Date? = nil,
        index: Int = 0,
        format: MatchFormat? = nil,
        servingTeamId: String? = nil,
        winningTeamId: String? = nil,
        losingTeamId: String? = nil,
        name: String? = nil,
        disabled: Bool = false,
        courtIndex: Int? = nil,
        confirmed: Bool = false
    ) {
        super.init()
        self.id = id
        self.round = round
        self.groupStage = groupStage
        self.startDate = startDate
        self.endDate = endDate
        self.index = index
        self.format = format
        self.servingTeamId = servingTeamId
        self.winningTeamId = winningTeamId
        self.losingTeamId = losingTeamId
        self.name = name
        self.disabled = disabled
        self.courtIndex = courtIndex
        self.confirmed = confirmed
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case _id = "id"
        case _round = "round"
        case _groupStage = "groupStage"
        case _startDate = "startDate"
        case _endDate = "endDate"
        case _index = "index"
        case _format = "format"
        case _servingTeamId = "servingTeamId"
        case _winningTeamId = "winningTeamId"
        case _losingTeamId = "losingTeamId"
        case _name = "name"
        case _disabled = "disabled"
        case _courtIndex = "courtIndex"
        case _confirmed = "confirmed"
    }

    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.round = try container.decodeIfPresent(String.self, forKey: ._round) ?? nil
        self.groupStage = try container.decodeIfPresent(String.self, forKey: ._groupStage) ?? nil
        self.startDate = try container.decodeIfPresent(Date.self, forKey: ._startDate) ?? nil
        self.endDate = try container.decodeIfPresent(Date.self, forKey: ._endDate) ?? nil
        self.index = try container.decodeIfPresent(Int.self, forKey: ._index) ?? 0
        self.format = try container.decodeIfPresent(MatchFormat.self, forKey: ._format) ?? nil
        self.servingTeamId = try container.decodeIfPresent(String.self, forKey: ._servingTeamId) ?? nil
        self.winningTeamId = try container.decodeIfPresent(String.self, forKey: ._winningTeamId) ?? nil
        self.losingTeamId = try container.decodeIfPresent(String.self, forKey: ._losingTeamId) ?? nil
        self.name = try container.decodeIfPresent(String.self, forKey: ._name) ?? nil
        self.disabled = try container.decodeIfPresent(Bool.self, forKey: ._disabled) ?? false
        self.courtIndex = try container.decodeIfPresent(Int.self, forKey: ._courtIndex) ?? nil
        self.confirmed = try container.decodeIfPresent(Bool.self, forKey: ._confirmed) ?? false
        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.round, forKey: ._round)
        try container.encode(self.groupStage, forKey: ._groupStage)
        try container.encode(self.startDate, forKey: ._startDate)
        try container.encode(self.endDate, forKey: ._endDate)
        try container.encode(self.index, forKey: ._index)
        try container.encode(self.format, forKey: ._format)
        try container.encode(self.servingTeamId, forKey: ._servingTeamId)
        try container.encode(self.winningTeamId, forKey: ._winningTeamId)
        try container.encode(self.losingTeamId, forKey: ._losingTeamId)
        try container.encode(self.name, forKey: ._name)
        try container.encode(self.disabled, forKey: ._disabled)
        try container.encode(self.courtIndex, forKey: ._courtIndex)
        try container.encode(self.confirmed, forKey: ._confirmed)
        try super.encode(to: encoder)
    }

    func roundValue() -> Round? {
        guard let round = self.round else { return nil }
        return self.store?.findById(round)
    }

    func groupStageValue() -> GroupStage? {
        guard let groupStage = self.groupStage else { return nil }
        return self.store?.findById(groupStage)
    }

    func copy(from other: any Storable) {
        guard let match = other as? BaseMatch else { return }
        self.id = match.id
        self.round = match.round
        self.groupStage = match.groupStage
        self.startDate = match.startDate
        self.endDate = match.endDate
        self.index = match.index
        self.format = match.format
        self.servingTeamId = match.servingTeamId
        self.winningTeamId = match.winningTeamId
        self.losingTeamId = match.losingTeamId
        self.name = match.name
        self.disabled = match.disabled
        self.courtIndex = match.courtIndex
        self.confirmed = match.confirmed
    }

    static func relationships() -> [Relationship] {
        return [
            Relationship(type: Round.self, keyPath: \BaseMatch.round),
            Relationship(type: GroupStage.self, keyPath: \BaseMatch.groupStage),
        ]
    }

}