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

/// <summary>
/// Core quest data - follows same pattern as WeaponItem and ArmorItem for easy creation
/// </summary>
[CreateAssetMenu(fileName = "New Quest", menuName = "RPG/Quests/Quest")]
[System.Serializable]
public class Quest : ScriptableObject
{
    [Header("Basic Information")]
    public string questTitle;
    [TextArea(3, 6)]
    public string questDescription;
    public QuestType questType;
    public QuestDifficulty difficulty;

    [Header("Quest Goals")]
    public List<QuestGoal> goals = new List<QuestGoal>();

    [Header("Time Limits")]
    public int timeLimitDays = 7;
    [Tooltip("Additional hours beyond the days")]
    public int timeLimitHours = 0;

    [Header("Location")]
    public string targetAreaName = "Forest Clearing";
    public Vector2Int targetMapPosition = Vector2Int.zero;
    [Tooltip("How far from target position is acceptable (in map tiles)")]
    public float completionRadius = 2f;

    [Header("Rewards")]
    public int goldReward = 50;
    public int silverReward = 0;
    public int copperReward = 0;
    public int renownReward = 10;
    public List<string> itemRewards = new List<string>();

    [Header("Failure Penalties")]
    public int renownPenalty = 5;
    public bool canFail = true;

    [Header("Requirements")]
    public int minimumLevel = 1;
    public int minimumRenown = 0;
    public List<string> prerequisiteQuests = new List<string>();

    // Compatibility properties for UI
    public string description => questDescription;
    public string targetLocationName => targetAreaName;
    public float targetLocationX => targetMapPosition.x;
    public float targetLocationY => targetMapPosition.y;
    public float timeLimit => GetTotalTimeLimitHours();
    public List<QuestGoal> objectives => goals;

    [Header("Quest Generation")]
    [Tooltip("Tags used for random quest generation")]
    public string[] questTags = new string[] { "combat", "rescue", "retrieval" };

    /// <summary>
    /// Calculate travel time from current position to quest location
    /// </summary>
    public float CalculateTravelTime(Vector2Int currentPosition, float travelSpeed = 1f)
    {
        float distance = Vector2Int.Distance(currentPosition, targetMapPosition);
        return distance / travelSpeed; // Returns time in game hours
    }

    /// <summary>
    /// Check if quest can be completed at given position
    /// </summary>
    public bool CanCompleteAtPosition(Vector2Int position)
    {
        float distance = Vector2Int.Distance(position, targetMapPosition);
        return distance <= completionRadius;
    }

    /// <summary>
    /// Get total time limit in hours
    /// </summary>
    public float GetTotalTimeLimitHours()
    {
        return (timeLimitDays * 24f) + timeLimitHours;
    }

    /// <summary>
    /// Create a runtime instance of this quest
    /// </summary>
    public ActiveQuest CreateActiveQuest()
    {
        return new ActiveQuest(this);
    }
}

[System.Serializable]
public class QuestGoal
{
    public string description;
    public QuestGoalType goalType;
    public string targetName; // Enemy type, item name, NPC name, etc.
    public int targetCount = 1;
    public int currentProgress = 0;
    public bool isHidden = false; // For surprise objectives
    public bool isOptional = false;

    public bool IsCompleted => currentProgress >= targetCount;
    public float ProgressPercent => targetCount > 0 ? (float)currentProgress / targetCount : 0f;

    // Compatibility property for UI
    public int targetAmount => targetCount;
}

public enum QuestType
{
    Combat,      // Kill enemies
    Rescue,      // Save someone
    Retrieval,   // Get items/artifacts
    Delivery,    // Transport items
    Exploration, // Discover locations
    Escort       // Protect someone
}

public enum QuestDifficulty
{
    Easy,
    Normal,
    Hard,
    Legendary
}

public enum QuestGoalType
{
    KillEnemies,
    CollectItems,
    RescuePerson,
    ReachLocation,
    SurviveTime,
    DefeatBoss,
    DeliverItem
}

[System.Serializable]
public class QuestReward
{
    public QuestRewardType type;
    public int amount;
    public string itemName;
    public UnityEngine.Object item; // For actual item references
}

public enum QuestRewardType
{
    Gold,
    Silver,
    Copper,
    Renown,
    Item,
    Experience
}
