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

import Foundation
import LeStorage
import SwiftUI

@Observable
class BaseTournament: SyncedModelObject, SyncedStorable {

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

    var id: String = Store.randomId()
    var event: String? = nil
    var name: String? = nil
    var startDate: Date = Date()
    var endDate: Date? = nil
    var creationDate: Date = Date()
    var isPrivate: Bool = false
    var groupStageFormat: MatchFormat? = nil
    var roundFormat: MatchFormat? = nil
    var loserRoundFormat: MatchFormat? = nil
    var groupStageSortMode: GroupStageOrderingMode = GroupStageOrderingMode.snake
    var groupStageCount: Int = 0
    var rankSourceDate: Date? = nil
    var dayDuration: Int = 0
    var teamCount: Int = 0
    var teamSorting: TeamSortingType = TeamSortingType.inscriptionDate
    var federalCategory: TournamentCategory = TournamentCategory.men
    var federalLevelCategory: TournamentLevel = TournamentLevel.unlisted
    var federalAgeCategory: FederalTournamentAge = FederalTournamentAge.unlisted
    var closedRegistrationDate: Date? = nil
    var groupStageAdditionalQualified: Int = 0
    var courtCount: Int = 2
    var prioritizeClubMembers: Bool = false
    var qualifiedPerGroupStage: Int = 0
    var teamsPerGroupStage: Int = 0
    var entryFee: Double? = nil
    var payment: TournamentPayment? = nil
    var additionalEstimationDuration: Int = 0
    var isDeleted: Bool = false
    var isCanceled: Bool = false
    var publishTeams: Bool = false
    var publishSummons: Bool = false
    var publishGroupStages: Bool = false
    var publishBrackets: Bool = false
    var shouldVerifyGroupStage: Bool = false
    var shouldVerifyBracket: Bool = false
    var hideTeamsWeight: Bool = false
    var publishTournament: Bool = false
    var hidePointsEarned: Bool = false
    var publishRankings: Bool = false
    var loserBracketMode: LoserBracketMode = .automatic
    var initialSeedRound: Int = 0
    var initialSeedCount: Int = 0
    var enableOnlineRegistration: Bool = false
    var registrationDateLimit: Date? = nil
    var openingRegistrationDate: Date? = nil
    var waitingListLimit: Int? = nil
    var accountIsRequired: Bool = true
    var licenseIsRequired: Bool = true
    var minimumPlayerPerTeam: Int = 2
    var maximumPlayerPerTeam: Int = 2
    var information: String? = nil
    var umpireCustomMail: String? = nil
    var umpireCustomContact: String? = nil
    var umpireCustomPhone: String? = nil
    var hideUmpireMail: Bool = false
    var hideUmpirePhone: Bool = true
    var disableRankingFederalRuling: Bool = false
    var teamCountLimit: Bool = true

    init(
        id: String = Store.randomId(),
        event: String? = nil,
        name: String? = nil,
        startDate: Date = Date(),
        endDate: Date? = nil,
        creationDate: Date = Date(),
        isPrivate: Bool = false,
        groupStageFormat: MatchFormat? = nil,
        roundFormat: MatchFormat? = nil,
        loserRoundFormat: MatchFormat? = nil,
        groupStageSortMode: GroupStageOrderingMode = GroupStageOrderingMode.snake,
        groupStageCount: Int = 0,
        rankSourceDate: Date? = nil,
        dayDuration: Int = 0,
        teamCount: Int = 0,
        teamSorting: TeamSortingType = TeamSortingType.inscriptionDate,
        federalCategory: TournamentCategory = TournamentCategory.men,
        federalLevelCategory: TournamentLevel = TournamentLevel.unlisted,
        federalAgeCategory: FederalTournamentAge = FederalTournamentAge.unlisted,
        closedRegistrationDate: Date? = nil,
        groupStageAdditionalQualified: Int = 0,
        courtCount: Int = 2,
        prioritizeClubMembers: Bool = false,
        qualifiedPerGroupStage: Int = 0,
        teamsPerGroupStage: Int = 0,
        entryFee: Double? = nil,
        payment: TournamentPayment? = nil,
        additionalEstimationDuration: Int = 0,
        isDeleted: Bool = false,
        isCanceled: Bool = false,
        publishTeams: Bool = false,
        publishSummons: Bool = false,
        publishGroupStages: Bool = false,
        publishBrackets: Bool = false,
        shouldVerifyGroupStage: Bool = false,
        shouldVerifyBracket: Bool = false,
        hideTeamsWeight: Bool = false,
        publishTournament: Bool = false,
        hidePointsEarned: Bool = false,
        publishRankings: Bool = false,
        loserBracketMode: LoserBracketMode = .automatic,
        initialSeedRound: Int = 0,
        initialSeedCount: Int = 0,
        enableOnlineRegistration: Bool = false,
        registrationDateLimit: Date? = nil,
        openingRegistrationDate: Date? = nil,
        waitingListLimit: Int? = nil,
        accountIsRequired: Bool = true,
        licenseIsRequired: Bool = true,
        minimumPlayerPerTeam: Int = 2,
        maximumPlayerPerTeam: Int = 2,
        information: String? = nil,
        umpireCustomMail: String? = nil,
        umpireCustomContact: String? = nil,
        umpireCustomPhone: String? = nil,
        hideUmpireMail: Bool = false,
        hideUmpirePhone: Bool = true,
        disableRankingFederalRuling: Bool = false,
        teamCountLimit: Bool = true
    ) {
        super.init()
        self.id = id
        self.event = event
        self.name = name
        self.startDate = startDate
        self.endDate = endDate
        self.creationDate = creationDate
        self.isPrivate = isPrivate
        self.groupStageFormat = groupStageFormat
        self.roundFormat = roundFormat
        self.loserRoundFormat = loserRoundFormat
        self.groupStageSortMode = groupStageSortMode
        self.groupStageCount = groupStageCount
        self.rankSourceDate = rankSourceDate
        self.dayDuration = dayDuration
        self.teamCount = teamCount
        self.teamSorting = teamSorting
        self.federalCategory = federalCategory
        self.federalLevelCategory = federalLevelCategory
        self.federalAgeCategory = federalAgeCategory
        self.closedRegistrationDate = closedRegistrationDate
        self.groupStageAdditionalQualified = groupStageAdditionalQualified
        self.courtCount = courtCount
        self.prioritizeClubMembers = prioritizeClubMembers
        self.qualifiedPerGroupStage = qualifiedPerGroupStage
        self.teamsPerGroupStage = teamsPerGroupStage
        self.entryFee = entryFee
        self.payment = payment
        self.additionalEstimationDuration = additionalEstimationDuration
        self.isDeleted = isDeleted
        self.isCanceled = isCanceled
        self.publishTeams = publishTeams
        self.publishSummons = publishSummons
        self.publishGroupStages = publishGroupStages
        self.publishBrackets = publishBrackets
        self.shouldVerifyGroupStage = shouldVerifyGroupStage
        self.shouldVerifyBracket = shouldVerifyBracket
        self.hideTeamsWeight = hideTeamsWeight
        self.publishTournament = publishTournament
        self.hidePointsEarned = hidePointsEarned
        self.publishRankings = publishRankings
        self.loserBracketMode = loserBracketMode
        self.initialSeedRound = initialSeedRound
        self.initialSeedCount = initialSeedCount
        self.enableOnlineRegistration = enableOnlineRegistration
        self.registrationDateLimit = registrationDateLimit
        self.openingRegistrationDate = openingRegistrationDate
        self.waitingListLimit = waitingListLimit
        self.accountIsRequired = accountIsRequired
        self.licenseIsRequired = licenseIsRequired
        self.minimumPlayerPerTeam = minimumPlayerPerTeam
        self.maximumPlayerPerTeam = maximumPlayerPerTeam
        self.information = information
        self.umpireCustomMail = umpireCustomMail
        self.umpireCustomContact = umpireCustomContact
        self.umpireCustomPhone = umpireCustomPhone
        self.hideUmpireMail = hideUmpireMail
        self.hideUmpirePhone = hideUmpirePhone
        self.disableRankingFederalRuling = disableRankingFederalRuling
        self.teamCountLimit = teamCountLimit
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case isCanceled = "isCanceled"
        case payment = "payment"
        case _id = "id"
        case _event = "event"
        case _name = "name"
        case _startDate = "startDate"
        case _endDate = "endDate"
        case _creationDate = "creationDate"
        case _isPrivate = "isPrivate"
        case _groupStageFormat = "groupStageFormat"
        case _roundFormat = "roundFormat"
        case _loserRoundFormat = "loserRoundFormat"
        case _groupStageSortMode = "groupStageSortMode"
        case _groupStageCount = "groupStageCount"
        case _rankSourceDate = "rankSourceDate"
        case _dayDuration = "dayDuration"
        case _teamCount = "teamCount"
        case _teamSorting = "teamSorting"
        case _federalCategory = "federalCategory"
        case _federalLevelCategory = "federalLevelCategory"
        case _federalAgeCategory = "federalAgeCategory"
        case _closedRegistrationDate = "closedRegistrationDate"
        case _groupStageAdditionalQualified = "groupStageAdditionalQualified"
        case _courtCount = "courtCount"
        case _prioritizeClubMembers = "prioritizeClubMembers"
        case _qualifiedPerGroupStage = "qualifiedPerGroupStage"
        case _teamsPerGroupStage = "teamsPerGroupStage"
        case _entryFee = "entryFee"
        case _payment = "globalId"
        case _additionalEstimationDuration = "additionalEstimationDuration"
        case _isDeleted = "isDeleted"
        case _isCanceled = "localId"
        case _publishTeams = "publishTeams"
        case _publishSummons = "publishSummons"
        case _publishGroupStages = "publishGroupStages"
        case _publishBrackets = "publishBrackets"
        case _shouldVerifyGroupStage = "shouldVerifyGroupStage"
        case _shouldVerifyBracket = "shouldVerifyBracket"
        case _hideTeamsWeight = "hideTeamsWeight"
        case _publishTournament = "publishTournament"
        case _hidePointsEarned = "hidePointsEarned"
        case _publishRankings = "publishRankings"
        case _loserBracketMode = "loserBracketMode"
        case _initialSeedRound = "initialSeedRound"
        case _initialSeedCount = "initialSeedCount"
        case _enableOnlineRegistration = "enableOnlineRegistration"
        case _registrationDateLimit = "registrationDateLimit"
        case _openingRegistrationDate = "openingRegistrationDate"
        case _waitingListLimit = "waitingListLimit"
        case _accountIsRequired = "accountIsRequired"
        case _licenseIsRequired = "licenseIsRequired"
        case _minimumPlayerPerTeam = "minimumPlayerPerTeam"
        case _maximumPlayerPerTeam = "maximumPlayerPerTeam"
        case _information = "information"
        case _umpireCustomMail = "umpireCustomMail"
        case _umpireCustomContact = "umpireCustomContact"
        case _umpireCustomPhone = "umpireCustomPhone"
        case _hideUmpireMail = "hideUmpireMail"
        case _hideUmpirePhone = "hideUmpirePhone"
        case _disableRankingFederalRuling = "disableRankingFederalRuling"
        case _teamCountLimit = "teamCountLimit"
    }

    private static func _decodePayment(container: KeyedDecodingContainer<CodingKeys>) throws -> TournamentPayment? {
        var data = try container.decodeIfPresent(Data.self, forKey: ._payment)
        if data == nil {
            data = try container.decodeIfPresent(Data.self, forKey: .payment)
        }
        
        if let data {
            do {
                let decoded: String = try data.decryptData(pass: CryptoKey.pass.rawValue)
                let sequence = decoded.compactMap { NumberFormatter.standard.number(from: String($0))?.intValue }
                return TournamentPayment(rawValue: sequence[18])
            } catch {
                Logger.error(error)
            }
        }
        return nil
    }

    private func _encodePayment(container: inout KeyedEncodingContainer<CodingKeys>) throws {
        guard let payment else {
            try container.encodeNil(forKey: ._payment)
            return
        }
        
        let max: Int = TournamentPayment.allCases.count
        var sequence = (1...18).map { _ in Int.random(in: (0..<max)) }
        sequence.append(payment.rawValue)
        sequence.append(contentsOf: (1...13).map { _ in Int.random(in: (0..<max ))} )

        let stringCombo: [String] = sequence.map { $0.formatted() }
        let joined: String = stringCombo.joined(separator: "")
        if let data = joined.data(using: .utf8) {
            let encryped: Data = try data.encrypt(pass: CryptoKey.pass.rawValue)
            try container.encodeIfPresent(encryped, forKey: ._payment)
        }
    }
    private static func _decodeIscanceled(container: KeyedDecodingContainer<CodingKeys>) throws -> Bool {
        var data = try container.decodeIfPresent(Data.self, forKey: ._isCanceled)
        if data == nil {
            data = try container.decodeIfPresent(Data.self, forKey: .isCanceled)
        }
        if let data {
            do {
                let decoded: String = try data.decryptData(pass: CryptoKey.pass.rawValue)
                let sequence = decoded.compactMap { NumberFormatter.standard.number(from: String($0))?.intValue }
                return Bool.decodeInt(sequence[18])
            } catch {
                Logger.error(error)
            }
        }
        return false
    }

    private func _encodeIscanceled(container: inout KeyedEncodingContainer<CodingKeys>) throws {
        let max: Int = 9
        var sequence = (1...18).map { _ in Int.random(in: (0...max)) }
        sequence.append(self.isCanceled.encodedValue)
        sequence.append(contentsOf: (1...13).map { _ in Int.random(in: (0...max ))} )
        
        let stringCombo: [String] = sequence.map { $0.formatted() }
        let joined: String = stringCombo.joined(separator: "")
        if let data = joined.data(using: .utf8) {
            let encryped: Data = try data.encrypt(pass: CryptoKey.pass.rawValue)
            try container.encode(encryped, forKey: ._isCanceled)
        }
    }

    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.event = try container.decodeIfPresent(String.self, forKey: ._event) ?? nil
        self.name = try container.decodeIfPresent(String.self, forKey: ._name) ?? nil
        self.startDate = try container.decodeIfPresent(Date.self, forKey: ._startDate) ?? Date()
        self.endDate = try container.decodeIfPresent(Date.self, forKey: ._endDate) ?? nil
        self.creationDate = try container.decodeIfPresent(Date.self, forKey: ._creationDate) ?? Date()
        self.isPrivate = try container.decodeIfPresent(Bool.self, forKey: ._isPrivate) ?? false
        self.groupStageFormat = try container.decodeIfPresent(MatchFormat.self, forKey: ._groupStageFormat) ?? nil
        self.roundFormat = try container.decodeIfPresent(MatchFormat.self, forKey: ._roundFormat) ?? nil
        self.loserRoundFormat = try container.decodeIfPresent(MatchFormat.self, forKey: ._loserRoundFormat) ?? nil
        self.groupStageSortMode = try container.decodeIfPresent(GroupStageOrderingMode.self, forKey: ._groupStageSortMode) ?? GroupStageOrderingMode.snake
        self.groupStageCount = try container.decodeIfPresent(Int.self, forKey: ._groupStageCount) ?? 0
        self.rankSourceDate = try container.decodeIfPresent(Date.self, forKey: ._rankSourceDate) ?? nil
        self.dayDuration = try container.decodeIfPresent(Int.self, forKey: ._dayDuration) ?? 0
        self.teamCount = try container.decodeIfPresent(Int.self, forKey: ._teamCount) ?? 0
        self.teamSorting = try container.decodeIfPresent(TeamSortingType.self, forKey: ._teamSorting) ?? TeamSortingType.inscriptionDate
        self.federalCategory = try container.decodeIfPresent(TournamentCategory.self, forKey: ._federalCategory) ?? TournamentCategory.men
        self.federalLevelCategory = try container.decodeIfPresent(TournamentLevel.self, forKey: ._federalLevelCategory) ?? TournamentLevel.unlisted
        self.federalAgeCategory = try container.decodeIfPresent(FederalTournamentAge.self, forKey: ._federalAgeCategory) ?? FederalTournamentAge.unlisted
        self.closedRegistrationDate = try container.decodeIfPresent(Date.self, forKey: ._closedRegistrationDate) ?? nil
        self.groupStageAdditionalQualified = try container.decodeIfPresent(Int.self, forKey: ._groupStageAdditionalQualified) ?? 0
        self.courtCount = try container.decodeIfPresent(Int.self, forKey: ._courtCount) ?? 2
        self.prioritizeClubMembers = try container.decodeIfPresent(Bool.self, forKey: ._prioritizeClubMembers) ?? false
        self.qualifiedPerGroupStage = try container.decodeIfPresent(Int.self, forKey: ._qualifiedPerGroupStage) ?? 0
        self.teamsPerGroupStage = try container.decodeIfPresent(Int.self, forKey: ._teamsPerGroupStage) ?? 0
        self.entryFee = try container.decodeIfPresent(Double.self, forKey: ._entryFee) ?? nil
        self.payment = try Self._decodePayment(container: container)
        self.additionalEstimationDuration = try container.decodeIfPresent(Int.self, forKey: ._additionalEstimationDuration) ?? 0
        self.isDeleted = try container.decodeIfPresent(Bool.self, forKey: ._isDeleted) ?? false
        self.isCanceled = try Self._decodeIscanceled(container: container)
        self.publishTeams = try container.decodeIfPresent(Bool.self, forKey: ._publishTeams) ?? false
        self.publishSummons = try container.decodeIfPresent(Bool.self, forKey: ._publishSummons) ?? false
        self.publishGroupStages = try container.decodeIfPresent(Bool.self, forKey: ._publishGroupStages) ?? false
        self.publishBrackets = try container.decodeIfPresent(Bool.self, forKey: ._publishBrackets) ?? false
        self.shouldVerifyGroupStage = try container.decodeIfPresent(Bool.self, forKey: ._shouldVerifyGroupStage) ?? false
        self.shouldVerifyBracket = try container.decodeIfPresent(Bool.self, forKey: ._shouldVerifyBracket) ?? false
        self.hideTeamsWeight = try container.decodeIfPresent(Bool.self, forKey: ._hideTeamsWeight) ?? false
        self.publishTournament = try container.decodeIfPresent(Bool.self, forKey: ._publishTournament) ?? false
        self.hidePointsEarned = try container.decodeIfPresent(Bool.self, forKey: ._hidePointsEarned) ?? false
        self.publishRankings = try container.decodeIfPresent(Bool.self, forKey: ._publishRankings) ?? false
        self.loserBracketMode = try container.decodeIfPresent(LoserBracketMode.self, forKey: ._loserBracketMode) ?? .automatic
        self.initialSeedRound = try container.decodeIfPresent(Int.self, forKey: ._initialSeedRound) ?? 0
        self.initialSeedCount = try container.decodeIfPresent(Int.self, forKey: ._initialSeedCount) ?? 0
        self.enableOnlineRegistration = try container.decodeIfPresent(Bool.self, forKey: ._enableOnlineRegistration) ?? false
        self.registrationDateLimit = try container.decodeIfPresent(Date.self, forKey: ._registrationDateLimit) ?? nil
        self.openingRegistrationDate = try container.decodeIfPresent(Date.self, forKey: ._openingRegistrationDate) ?? nil
        self.waitingListLimit = try container.decodeIfPresent(Int.self, forKey: ._waitingListLimit) ?? nil
        self.accountIsRequired = try container.decodeIfPresent(Bool.self, forKey: ._accountIsRequired) ?? true
        self.licenseIsRequired = try container.decodeIfPresent(Bool.self, forKey: ._licenseIsRequired) ?? true
        self.minimumPlayerPerTeam = try container.decodeIfPresent(Int.self, forKey: ._minimumPlayerPerTeam) ?? 2
        self.maximumPlayerPerTeam = try container.decodeIfPresent(Int.self, forKey: ._maximumPlayerPerTeam) ?? 2
        self.information = try container.decodeIfPresent(String.self, forKey: ._information) ?? nil
        self.umpireCustomMail = try container.decodeIfPresent(String.self, forKey: ._umpireCustomMail) ?? nil
        self.umpireCustomContact = try container.decodeIfPresent(String.self, forKey: ._umpireCustomContact) ?? nil
        self.umpireCustomPhone = try container.decodeIfPresent(String.self, forKey: ._umpireCustomPhone) ?? nil
        self.hideUmpireMail = try container.decodeIfPresent(Bool.self, forKey: ._hideUmpireMail) ?? false
        self.hideUmpirePhone = try container.decodeIfPresent(Bool.self, forKey: ._hideUmpirePhone) ?? true
        self.disableRankingFederalRuling = try container.decodeIfPresent(Bool.self, forKey: ._disableRankingFederalRuling) ?? false
        self.teamCountLimit = try container.decodeIfPresent(Bool.self, forKey: ._teamCountLimit) ?? true
        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.event, forKey: ._event)
        try container.encode(self.name, forKey: ._name)
        try container.encode(self.startDate, forKey: ._startDate)
        try container.encode(self.endDate, forKey: ._endDate)
        try container.encode(self.creationDate, forKey: ._creationDate)
        try container.encode(self.isPrivate, forKey: ._isPrivate)
        try container.encode(self.groupStageFormat, forKey: ._groupStageFormat)
        try container.encode(self.roundFormat, forKey: ._roundFormat)
        try container.encode(self.loserRoundFormat, forKey: ._loserRoundFormat)
        try container.encode(self.groupStageSortMode, forKey: ._groupStageSortMode)
        try container.encode(self.groupStageCount, forKey: ._groupStageCount)
        try container.encode(self.rankSourceDate, forKey: ._rankSourceDate)
        try container.encode(self.dayDuration, forKey: ._dayDuration)
        try container.encode(self.teamCount, forKey: ._teamCount)
        try container.encode(self.teamSorting, forKey: ._teamSorting)
        try container.encode(self.federalCategory, forKey: ._federalCategory)
        try container.encode(self.federalLevelCategory, forKey: ._federalLevelCategory)
        try container.encode(self.federalAgeCategory, forKey: ._federalAgeCategory)
        try container.encode(self.closedRegistrationDate, forKey: ._closedRegistrationDate)
        try container.encode(self.groupStageAdditionalQualified, forKey: ._groupStageAdditionalQualified)
        try container.encode(self.courtCount, forKey: ._courtCount)
        try container.encode(self.prioritizeClubMembers, forKey: ._prioritizeClubMembers)
        try container.encode(self.qualifiedPerGroupStage, forKey: ._qualifiedPerGroupStage)
        try container.encode(self.teamsPerGroupStage, forKey: ._teamsPerGroupStage)
        try container.encode(self.entryFee, forKey: ._entryFee)
        try _encodePayment(container: &container)
        try container.encode(self.additionalEstimationDuration, forKey: ._additionalEstimationDuration)
        try container.encode(self.isDeleted, forKey: ._isDeleted)
        try _encodeIscanceled(container: &container)
        try container.encode(self.publishTeams, forKey: ._publishTeams)
        try container.encode(self.publishSummons, forKey: ._publishSummons)
        try container.encode(self.publishGroupStages, forKey: ._publishGroupStages)
        try container.encode(self.publishBrackets, forKey: ._publishBrackets)
        try container.encode(self.shouldVerifyGroupStage, forKey: ._shouldVerifyGroupStage)
        try container.encode(self.shouldVerifyBracket, forKey: ._shouldVerifyBracket)
        try container.encode(self.hideTeamsWeight, forKey: ._hideTeamsWeight)
        try container.encode(self.publishTournament, forKey: ._publishTournament)
        try container.encode(self.hidePointsEarned, forKey: ._hidePointsEarned)
        try container.encode(self.publishRankings, forKey: ._publishRankings)
        try container.encode(self.loserBracketMode, forKey: ._loserBracketMode)
        try container.encode(self.initialSeedRound, forKey: ._initialSeedRound)
        try container.encode(self.initialSeedCount, forKey: ._initialSeedCount)
        try container.encode(self.enableOnlineRegistration, forKey: ._enableOnlineRegistration)
        try container.encode(self.registrationDateLimit, forKey: ._registrationDateLimit)
        try container.encode(self.openingRegistrationDate, forKey: ._openingRegistrationDate)
        try container.encode(self.waitingListLimit, forKey: ._waitingListLimit)
        try container.encode(self.accountIsRequired, forKey: ._accountIsRequired)
        try container.encode(self.licenseIsRequired, forKey: ._licenseIsRequired)
        try container.encode(self.minimumPlayerPerTeam, forKey: ._minimumPlayerPerTeam)
        try container.encode(self.maximumPlayerPerTeam, forKey: ._maximumPlayerPerTeam)
        try container.encode(self.information, forKey: ._information)
        try container.encode(self.umpireCustomMail, forKey: ._umpireCustomMail)
        try container.encode(self.umpireCustomContact, forKey: ._umpireCustomContact)
        try container.encode(self.umpireCustomPhone, forKey: ._umpireCustomPhone)
        try container.encode(self.hideUmpireMail, forKey: ._hideUmpireMail)
        try container.encode(self.hideUmpirePhone, forKey: ._hideUmpirePhone)
        try container.encode(self.disableRankingFederalRuling, forKey: ._disableRankingFederalRuling)
        try container.encode(self.teamCountLimit, forKey: ._teamCountLimit)
        try super.encode(to: encoder)
    }

    func eventValue() -> Event? {
        guard let event = self.event else { return nil }
        return Store.main.findById(event)
    }

    func copy(from other: any Storable) {
        guard let tournament = other as? BaseTournament else { return }
        self.id = tournament.id
        self.event = tournament.event
        self.name = tournament.name
        self.startDate = tournament.startDate
        self.endDate = tournament.endDate
        self.creationDate = tournament.creationDate
        self.isPrivate = tournament.isPrivate
        self.groupStageFormat = tournament.groupStageFormat
        self.roundFormat = tournament.roundFormat
        self.loserRoundFormat = tournament.loserRoundFormat
        self.groupStageSortMode = tournament.groupStageSortMode
        self.groupStageCount = tournament.groupStageCount
        self.rankSourceDate = tournament.rankSourceDate
        self.dayDuration = tournament.dayDuration
        self.teamCount = tournament.teamCount
        self.teamSorting = tournament.teamSorting
        self.federalCategory = tournament.federalCategory
        self.federalLevelCategory = tournament.federalLevelCategory
        self.federalAgeCategory = tournament.federalAgeCategory
        self.closedRegistrationDate = tournament.closedRegistrationDate
        self.groupStageAdditionalQualified = tournament.groupStageAdditionalQualified
        self.courtCount = tournament.courtCount
        self.prioritizeClubMembers = tournament.prioritizeClubMembers
        self.qualifiedPerGroupStage = tournament.qualifiedPerGroupStage
        self.teamsPerGroupStage = tournament.teamsPerGroupStage
        self.entryFee = tournament.entryFee
        self.payment = tournament.payment
        self.additionalEstimationDuration = tournament.additionalEstimationDuration
        self.isDeleted = tournament.isDeleted
        self.isCanceled = tournament.isCanceled
        self.publishTeams = tournament.publishTeams
        self.publishSummons = tournament.publishSummons
        self.publishGroupStages = tournament.publishGroupStages
        self.publishBrackets = tournament.publishBrackets
        self.shouldVerifyGroupStage = tournament.shouldVerifyGroupStage
        self.shouldVerifyBracket = tournament.shouldVerifyBracket
        self.hideTeamsWeight = tournament.hideTeamsWeight
        self.publishTournament = tournament.publishTournament
        self.hidePointsEarned = tournament.hidePointsEarned
        self.publishRankings = tournament.publishRankings
        self.loserBracketMode = tournament.loserBracketMode
        self.initialSeedRound = tournament.initialSeedRound
        self.initialSeedCount = tournament.initialSeedCount
        self.enableOnlineRegistration = tournament.enableOnlineRegistration
        self.registrationDateLimit = tournament.registrationDateLimit
        self.openingRegistrationDate = tournament.openingRegistrationDate
        self.waitingListLimit = tournament.waitingListLimit
        self.accountIsRequired = tournament.accountIsRequired
        self.licenseIsRequired = tournament.licenseIsRequired
        self.minimumPlayerPerTeam = tournament.minimumPlayerPerTeam
        self.maximumPlayerPerTeam = tournament.maximumPlayerPerTeam
        self.information = tournament.information
        self.umpireCustomMail = tournament.umpireCustomMail
        self.umpireCustomContact = tournament.umpireCustomContact
        self.umpireCustomPhone = tournament.umpireCustomPhone
        self.hideUmpireMail = tournament.hideUmpireMail
        self.hideUmpirePhone = tournament.hideUmpirePhone
        self.disableRankingFederalRuling = tournament.disableRankingFederalRuling
        self.teamCountLimit = tournament.teamCountLimit
    }

    static func relationships() -> [Relationship] {
        return [
            Relationship(type: Event.self, keyPath: \BaseTournament.event),
        ]
    }

}