export type ResourceType = 'wood' | 'stone' | 'food' | 'gold';

export class ResourceManager {
  private store: Record<ResourceType, number> = {
    wood: 20,
    stone: 10,
    food: 10,
    gold: 0,
  };

  /** Prüft, ob Ressourcen ausreichen */
  canAfford(cost: Partial<Record<ResourceType, number>>): boolean {
    return Object.entries(cost).every(([type, amount]) =>
      (this.store[type as ResourceType] ?? 0) >= (amount ?? 0)
    );
  }

  /** Zieht Ressourcen ab */
  pay(cost: Partial<Record<ResourceType, number>>): boolean {
    if (!this.canAfford(cost)) return false;
    for (const [type, amount] of Object.entries(cost)) {
      this.store[type as ResourceType] -= amount ?? 0;
    }
    return true;
  }

  /** Fügt Ressourcen hinzu (z. B. Produktion) */
  add(delta: Partial<Record<ResourceType, number>>): void {
    for (const [type, amount] of Object.entries(delta)) {
      this.store[type as ResourceType] += amount ?? 0;
    }
  }

  /** Gibt aktuellen Stand zurück */
  getAll(): Record<ResourceType, number> {
    return { ...this.store };
  }
}