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

import Foundation
import LeStorage
import SwiftUI

@Observable
class BaseEvent: SyncedModelObject, SyncedStorable {

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

    var id: String = Store.randomId()
    var creator: String? = nil
    var club: String? = nil
    var creationDate: Date = Date()
    var name: String? = nil
    var tenupId: String? = nil

    init(
        id: String = Store.randomId(),
        creator: String? = nil,
        club: String? = nil,
        creationDate: Date = Date(),
        name: String? = nil,
        tenupId: String? = nil
    ) {
        super.init()
        self.id = id
        self.creator = creator
        self.club = club
        self.creationDate = creationDate
        self.name = name
        self.tenupId = tenupId
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case _id = "id"
        case _creator = "creator"
        case _club = "club"
        case _creationDate = "creationDate"
        case _name = "name"
        case _tenupId = "tenupId"
    }

    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.creator = try container.decodeIfPresent(String.self, forKey: ._creator) ?? nil
        self.club = try container.decodeIfPresent(String.self, forKey: ._club) ?? nil
        self.creationDate = try container.decodeIfPresent(Date.self, forKey: ._creationDate) ?? Date()
        self.name = try container.decodeIfPresent(String.self, forKey: ._name) ?? nil
        self.tenupId = try container.decodeIfPresent(String.self, forKey: ._tenupId) ?? 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.creator, forKey: ._creator)
        try container.encode(self.club, forKey: ._club)
        try container.encode(self.creationDate, forKey: ._creationDate)
        try container.encode(self.name, forKey: ._name)
        try container.encode(self.tenupId, forKey: ._tenupId)
        try super.encode(to: encoder)
    }

    func creatorValue() -> CustomUser? {
        guard let creator = self.creator else { return nil }
        return Store.main.findById(creator)
    }

    func clubValue() -> Club? {
        guard let club = self.club else { return nil }
        return Store.main.findById(club)
    }

    func copy(from other: any Storable) {
        guard let event = other as? BaseEvent else { return }
        self.id = event.id
        self.creator = event.creator
        self.club = event.club
        self.creationDate = event.creationDate
        self.name = event.name
        self.tenupId = event.tenupId
    }

    static func relationships() -> [Relationship] {
        return [
            Relationship(type: CustomUser.self, keyPath: \BaseEvent.creator),
            Relationship(type: Club.self, keyPath: \BaseEvent.club),
        ]
    }

}