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

import Foundation
import LeStorage

@Observable
class TeamScore: ModelObject, Storable {

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

    var id: String = Store.randomId()
    var match: String
    var teamRegistration: String?
    //var playerRegistrations: [String] = []
    var score: String?
    var walkOut: Int?
    var luckyLoser: Int?

    init(match: String, teamRegistration: String? = nil, score: String? = nil, walkOut: Int? = nil, luckyLoser: Int? = nil) {
        self.match = match
        self.teamRegistration = teamRegistration
//        self.playerRegistrations = playerRegistrations
        self.score = score
        self.walkOut = walkOut
        self.luckyLoser = luckyLoser
    }
    
    init(match: String, team: TeamRegistration?) {
        self.match = match
        if let team {
            self.teamRegistration = team.id
            //self.playerRegistrations = team.players().map { $0.id }
        }
        self.score = nil
        self.walkOut = nil
        self.luckyLoser = nil
    }
    
    // MARK: - Computed dependencies
    
    func matchObject() -> Match? {
        return DataStore.shared.matches.findById(self.match)
    }
    
    var team: TeamRegistration? {
        guard let teamRegistration else {
            return nil
        }
        return DataStore.shared.teamRegistrations.findById(teamRegistration)
    }
    
    // MARK: -
    
    func isWalkOut() -> Bool {
        return walkOut != nil
    }
    
    enum CodingKeys: String, CodingKey {
        case _id = "id"
        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(match, forKey: ._match)
        
        if let teamRegistration = teamRegistration {
            try container.encode(teamRegistration, forKey: ._teamRegistration)
        } else {
            try container.encodeNil(forKey: ._teamRegistration)
        }
        
        //try container.encode(playerRegistrations, forKey: ._playerRegistrations)
        
        if let score = score {
            try container.encode(score, forKey: ._score)
        } else {
            try container.encodeNil(forKey: ._score)
        }
        
        if let walkOut = walkOut {
            try container.encode(walkOut, forKey: ._walkOut)
        } else {
            try container.encodeNil(forKey: ._walkOut)
        }
        
        if let luckyLoser = luckyLoser {
            try container.encode(luckyLoser, forKey: ._luckyLoser)
        } else {
            try container.encodeNil(forKey: ._luckyLoser)
        }
    }
    
    func insertOnServer() throws {
        try DataStore.shared.teamScores.writeChangeAndInsertOnServer(instance: self)
    }
    
}
