//
//  Player.swift
//  lichunWebsocket
//
//  Player model and related structures
//

import Foundation
import Combine

// MARK: - Player
class Player: ObservableObject {
    @Published var date: String = ""
    @Published var status: String = ""
    @Published var season: String = ""
    @Published var hourOfDay: Int = 0
    @Published var minuteOfHour: Int = 0
    @Published var gameSpeed: Int = 0
    @Published var activeConversations: [ConversationObj] = []
    @Published var focuses: [FocusOption] = []
    @Published var storeItems: [StoreItem] = []
    @Published var occupations: [OccupationClass] = []
    @Published var inAppPurchases: [InAppPurchaseItem] = []
    @Published var r: [Person] = []
    @Published var relData: [Relationship] = []

    // MARK: - Lifecycle "spine" (Tier 2)
    /// End-of-life summary, populated on death (lifeSummaryEvent) or persisted
    /// on the player and surfaced on reconnect after an offline death.
    @Published var lifeSummary: LifeSummary?
    /// Compounding cross-generational prestige (survives the per-life wipe).
    @Published var familyPrestige: Int = 0
    /// Persistent lineage history; one entry per ancestor death.
    @Published var familyTree: [FamilyTreeEntry] = []
    /// Inheritance stashed at death, applied to the heir's next life server-side.
    @Published var pendingInheritance: Int = 0
    /// Welcome-back digest from the offline loop (shown once on connect).
    @Published var offlineDigest: OfflineDigest?
    /// Forward-looking life goals snapshot (Wave 1: cached; UI is Wave 2).
    @Published var lifeGoals: LifeGoalsSnapshot?
    /// Deeper quest-loop state: full-clear streak + weekly challenge. Rides in
    /// playerObject (`questEngagement`). Chains are day-scoped and arrive via the
    /// quest-progress messages, not this snapshot.
    @Published var questEngagement: QuestEngagementSnapshot?

    /// Account-deletion grace-period marker. When set (an ISO-8601 timestamp),
    /// the account is scheduled for permanent deletion at that time (server
    /// schedules now + 30 days). The server sends this as the top-level
    /// `deletionScheduledAt` key inside `playerObject`. Cleared (nil) when the
    /// player cancels the deletion (explicitly or by signing back in).
    @Published var deletionScheduledAt: String?
}

// MARK: - Focus Option
struct FocusOption: Identifiable, Decodable, Encodable {
    var id: Int
    let focus_name: String
    let description: String
    let energyModifier: Int
}

// MARK: - Relationship
struct Relationship: Decodable {
    var id: String
    var person1: String
    var person2: String
    var startDate: String
    var anniversaryDate: String?
    var relationshipStatus: String
    var relationshipNotes: String
    var eventsLog: [String]
    var relationshipScore: Int
    var commonInterests: [String]
    var challenges: [String]
    var futurePlans: [String]

    init(id: String, person1: String, person2: String, startDate: String, anniversaryDate: String? = nil, relationshipStatus: String, relationshipNotes: String, eventsLog: [String], relationshipScore: Int, commonInterests: [String], challenges: [String], futurePlans: [String]) {
        self.id = id
        self.person1 = person1
        self.person2 = person2
        self.startDate = startDate
        self.anniversaryDate = anniversaryDate ?? startDate
        self.relationshipStatus = relationshipStatus
        self.relationshipNotes = relationshipNotes
        self.eventsLog = eventsLog
        self.relationshipScore = relationshipScore
        self.commonInterests = commonInterests
        self.challenges = challenges
        self.futurePlans = futurePlans
    }
}

// MARK: - Date Idea
class DateIdea: Identifiable, Decodable {
    let name: String
    let energy_cost: Int
    let money_cost: Int
    let image: String

    init(name: String, energy_cost: Int, money_cost: Int, image: String) {
        self.name = name
        self.energy_cost = energy_cost
        self.money_cost = money_cost
        self.image = image
    }
}
