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

import Foundation
import LeStorage
import SwiftUI

@Observable
class BaseTeamScore: SyncedModelObject, SyncedStorable {

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

    var id: String = Store.randomId()
    var match: String = ""
    var teamRegistration: String? = nil
    var score: String? = nil
    var walkOut: Int? = nil
    var luckyLoser: Int? = nil

    init(
        id: String = Store.randomId(),
        match: String = "",
        teamRegistration: String? = nil,
        score: String? = nil,
        walkOut: Int? = nil,
        luckyLoser: Int? = nil
    ) {
        super.init()
        self.id = id
        self.match = match
        self.teamRegistration = teamRegistration
        self.score = score
        self.walkOut = walkOut
        self.luckyLoser = luckyLoser
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case _id = "id"
        case _match = "match"
        case _teamRegistration = "teamRegistration"
        case _score = "score"
        case _walkOut = "walkOut"
        case _luckyLoser = "luckyLoser"
    }

    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.match = try container.decodeIfPresent(String.self, forKey: ._match) ?? ""
        self.teamRegistration = try container.decodeIfPresent(String.self, forKey: ._teamRegistration) ?? nil
        self.score = try container.decodeIfPresent(String.self, forKey: ._score) ?? nil
        self.walkOut = try container.decodeIfPresent(Int.self, forKey: ._walkOut) ?? nil
        self.luckyLoser = try container.decodeIfPresent(Int.self, forKey: ._luckyLoser) ?? nil
        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.match, forKey: ._match)
        try container.encode(self.teamRegistration, forKey: ._teamRegistration)
        try container.encode(self.score, forKey: ._score)
        try container.encode(self.walkOut, forKey: ._walkOut)
        try container.encode(self.luckyLoser, forKey: ._luckyLoser)
        try super.encode(to: encoder)
    }

    func matchValue() -> Match? {
        return self.store?.findById(match)
    }

    func teamRegistrationValue() -> TeamRegistration? {
        guard let teamRegistration = self.teamRegistration else { return nil }
        return self.store?.findById(teamRegistration)
    }

    func copy(from other: any Storable) {
        guard let teamscore = other as? BaseTeamScore else { return }
        self.id = teamscore.id
        self.match = teamscore.match
        self.teamRegistration = teamscore.teamRegistration
        self.score = teamscore.score
        self.walkOut = teamscore.walkOut
        self.luckyLoser = teamscore.luckyLoser
    }

    static func relationships() -> [Relationship] {
        return [
            Relationship(type: Match.self, keyPath: \BaseTeamScore.match),
            Relationship(type: TeamRegistration.self, keyPath: \BaseTeamScore.teamRegistration),
        ]
    }

}