using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// GAME 1303 Tier 7 — Feed, play, clean, pet. One button (or one Interactable) per action.
/// Cozy Critters.
///
/// SETUP
///  1. Empty GameObject "FeedAction" + this script, one per action.
///  2. Effects: add an entry per stat this changes — the NeedStat and how much to add.
///     Feeding: Hunger +40, Happiness +5. Playing: Happiness +25, Energy -15.
///  3. Cooldown stops spamming; OnCooldownChanged(0-1) can grey out a UI button.
///  4. Trigger it from either:
///       - a UI Button: OnClick -> CareAction.Perform
///       - a Tier 6 Interactable on a food bowl: OnInteract -> CareAction.Perform
///  5. Pet Brain: drag the pet in so it perks up (React state) when cared for.
///  6. Personality Trait: name the trait this action feeds ("well_fed", "playful",
///     "cuddled"). PersonalityTracker uses these to decide the pet's nature — that's the
///     pitch's "changing the nature of the pet."
///  7. Effect Prefab: hearts, crumbs, bubbles — spawned at the pet.
///
/// Requires Item: for a game where food is a limited resource, set an ItemType and the
/// action spends one from the Tier 1 CollectionTracker. Leave empty for unlimited care.
/// </summary>
public class CareAction : MonoBehaviour
{
    [System.Serializable]
    public class StatEffect
    {
        public NeedStat stat;
        public float amount = 25f;
    }

    [Header("Identity")]
    public string actionName = "Feed";
    public Sprite icon;

    [Header("Effects")]
    public List<StatEffect> effects = new List<StatEffect>();

    [Header("Cost (optional)")]
    public ItemType requiresItem;
    public int itemCost = 1;

    [Header("Timing")]
    public float cooldown = 5f;

    [Header("Pet")]
    public PetBrain pet;
    [Tooltip("Trait this action feeds — PersonalityTracker counts these.")]
    public string personalityTrait = "well_fed";

    [Header("Feedback")]
    public GameObject effectPrefab;
    public string sfxName = "care";

    [Header("Events")]
    public UnityEvent OnPerformed;
    public UnityEvent OnRefused;
    public UnityEvent<float> OnCooldownChanged;   // 1 = ready, 0 = just used

    public bool IsReady => cooldownTimer <= 0f;

    private float cooldownTimer;

    void Update()
    {
        if (cooldownTimer <= 0f) return;
        cooldownTimer -= Time.deltaTime;
        OnCooldownChanged?.Invoke(cooldown > 0f ? 1f - Mathf.Clamp01(cooldownTimer / cooldown) : 1f);
    }

    /// <summary>Wire a UI Button or an Interactable here.</summary>
    public void Perform()
    {
        if (!IsReady) { OnRefused?.Invoke(); return; }

        if (requiresItem != null && itemCost > 0)
        {
            if (CollectionTracker.Instance == null || !CollectionTracker.Instance.Spend(requiresItem, itemCost))
            {
                OnRefused?.Invoke();
                return;
            }
        }

        foreach (var e in effects)
            if (e.stat != null) e.stat.Add(e.amount);

        if (pet != null)
        {
            pet.React();
            if (effectPrefab != null) Instantiate(effectPrefab, pet.transform.position, Quaternion.identity);
        }

        if (!string.IsNullOrEmpty(personalityTrait))
            ChoiceFlagManager.Instance?.AddCount(personalityTrait, 1);

        if (!string.IsNullOrEmpty(sfxName)) AudioManager.Instance?.PlaySFX(sfxName);

        cooldownTimer = cooldown;
        OnCooldownChanged?.Invoke(0f);
        OnPerformed?.Invoke();
    }
}