//
//  TeamScore.swift
//  Padel Tournament
//
//  Created by razmig on 10/03/2024.
//

import Foundation
import LeStorage

@Observable
final class TeamScore: BaseTeamScore, SideStorable {
   
    
//    static func resourceName() -> String { "team-scores" }
//    static func tokenExemptedMethods() -> [HTTPMethod] { return [] }
//    static func filterByStoreIdentifier() -> Bool { return true }
//    static var relationshipNames: [String] = ["match"]
//
//    var id: String = Store.randomId()
//    var lastUpdate: Date
//    var match: String
//    var teamRegistration: String?
//    //var playerRegistrations: [String] = []
//    var score: String?
//    var walkOut: Int?
//    var luckyLoser: Int?
//    
//    var storeId: String? = nil

    init(match: String, teamRegistration: String? = nil, score: String? = nil, walkOut: Int? = nil, luckyLoser: Int? = nil) {
        super.init(match: match, teamRegistration: teamRegistration, score: score, walkOut: walkOut, luckyLoser: luckyLoser)
        
//        self.match = match
//        self.teamRegistration = teamRegistration
////        self.playerRegistrations = playerRegistrations
//        self.score = score
//        self.walkOut = walkOut
//        self.luckyLoser = luckyLoser
    }
    
    init(match: String, team: TeamRegistration?) {
        super.init(match: match)
        if let team {
            self.teamRegistration = team.id
        }
    }
    
    required init(from decoder: Decoder) throws {
        try super.init(from: decoder)
    }
    
    required public init() {
        super.init()
    }
    
    var tournamentStore: TournamentStore? {
        guard let storeId else {
            fatalError("missing store id for \(String(describing: type(of: self)))")
        }
        return TournamentLibrary.shared.store(tournamentId: storeId)
//
//        if let store = self.store as? TournamentStore {
//            return store
//        }
//        fatalError("missing store for \(String(describing: type(of: self)))")
    }

    // MARK: - Computed dependencies
    
    func matchObject() -> Match? {
        return self.tournamentStore?.matches.findById(self.match)
    }
    
    var team: TeamRegistration? {
        guard let teamRegistration else {
            return nil
        }
        return self.tournamentStore?.teamRegistrations.findById(teamRegistration)
    }
    
    // MARK: -
    
    func isWalkOut() -> Bool {
        return walkOut != nil
    }
    
//    enum CodingKeys: String, CodingKey {
//        case _id = "id"
//        case _storeId = "storeId"
//        case _lastUpdate = "lastUpdate"
//        case _match = "match"
//        case _teamRegistration = "teamRegistration"
//        //case _playerRegistrations = "playerRegistrations"
//        case _score = "score"
//        case _walkOut = "walkOut"
//        case _luckyLoser = "luckyLoser"
//    }
//
//    func encode(to encoder: Encoder) throws {
//        var container = encoder.container(keyedBy: CodingKeys.self)
//        
//        try container.encode(id, forKey: ._id)
//        try container.encode(storeId, forKey: ._storeId)
//        try container.encode(lastUpdate, forKey: ._lastUpdate)
//        try container.encode(match, forKey: ._match)
//        try container.encode(teamRegistration, forKey: ._teamRegistration)
//        try container.encode(score, forKey: ._score)
//        try container.encode(walkOut, forKey: ._walkOut)
//        try container.encode(luckyLoser, forKey: ._luckyLoser)
//    }

    func insertOnServer() {
        self.tournamentStore?.teamScores.writeChangeAndInsertOnServer(instance: self)
    }
    
}
