import SwiftUI
import Combine

class CreditsManager: ObservableObject {
    @Published var creditCount: Int = 1000
    @Published var showOutOfCreditsAlert: Bool = false
    
    static let shared = CreditsManager()
    
    private init() {}
    
    func deductCredits(_ amount: Int) {
        // Check if user has enough credits
        if creditCount < amount {
            // Not enough credits - show alert and don't deduct
            showOutOfCreditsAlert = true
            print("🚨 Insufficient credits: Need \(amount), have \(creditCount)")
            return
        }
        
        // Deduct credits normally
        creditCount -= amount
        print("💰 Deducted \(amount) credits - New balance: \(creditCount)")
        
        // Show alert if credits just reached 0
        if creditCount == 0 {
            showOutOfCreditsAlert = true
            print("🚨 Credits depleted - showing alert")
        }
    }
    
    func addCredits(_ amount: Int) {
        creditCount += amount
        print("💰 Added \(amount) credits - New balance: \(creditCount)")
    }
    
    func hasEnoughCredits(_ amount: Int) -> Bool {
        return creditCount >= amount
    }
}