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

import Foundation
import LeStorage
import SwiftUI

@Observable
class BaseGroupStage: SyncedModelObject, SyncedStorable {

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

    var id: String = Store.randomId()
    var tournament: String = ""
    var index: Int = 0
    var size: Int = 0
    var format: MatchFormat? = nil
    var startDate: Date? = nil
    var name: String? = nil
    var step: Int = 0

    init(
        id: String = Store.randomId(),
        tournament: String = "",
        index: Int = 0,
        size: Int = 0,
        format: MatchFormat? = nil,
        startDate: Date? = nil,
        name: String? = nil,
        step: Int = 0
    ) {
        super.init()
        self.id = id
        self.tournament = tournament
        self.index = index
        self.size = size
        self.format = format
        self.startDate = startDate
        self.name = name
        self.step = step
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case _id = "id"
        case _tournament = "tournament"
        case _index = "index"
        case _size = "size"
        case _format = "format"
        case _startDate = "startDate"
        case _name = "name"
        case _step = "step"
    }

    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.size = try container.decodeIfPresent(Int.self, forKey: ._size) ?? 0
        self.format = try container.decodeIfPresent(MatchFormat.self, forKey: ._format) ?? nil
        self.startDate = try container.decodeIfPresent(Date.self, forKey: ._startDate) ?? nil
        self.name = try container.decodeIfPresent(String.self, forKey: ._name) ?? nil
        self.step = try container.decodeIfPresent(Int.self, forKey: ._step) ?? 0
        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.size, forKey: ._size)
        try container.encode(self.format, forKey: ._format)
        try container.encode(self.startDate, forKey: ._startDate)
        try container.encode(self.name, forKey: ._name)
        try container.encode(self.step, forKey: ._step)
        try super.encode(to: encoder)
    }

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

    func copy(from other: any Storable) {
        guard let groupstage = other as? BaseGroupStage else { return }
        self.id = groupstage.id
        self.tournament = groupstage.tournament
        self.index = groupstage.index
        self.size = groupstage.size
        self.format = groupstage.format
        self.startDate = groupstage.startDate
        self.name = groupstage.name
        self.step = groupstage.step
    }

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

}