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

/// <summary>
/// Editor tool for creating sample quests - follows same pattern as CreateSampleItems
/// </summary>
public class QuestCreatorHelper
{
#if UNITY_EDITOR
    [MenuItem("RPG/Create Sample Quests")]
    public static void CreateSampleQuests()
    {
        CreateCombatQuests();
        CreateRescueQuests();
        CreateRetrievalQuests();
        CreateExplorationQuests();
        
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        
        Debug.Log("✅ Sample quests created in Resources/Quests folder!");
    }
    
    private static void CreateCombatQuests()
    {
        EnsureFolderExists("Assets/Resources/Quests/Combat");
        
        // Easy Combat Quest
        var banditCamp = ScriptableObject.CreateInstance<Quest>();
        banditCamp.questTitle = "Clear the Bandit Camp";
        banditCamp.questDescription = "Bandits have been attacking merchants on the main road. Their camp has been spotted in the nearby forest. Clear them out and make the roads safe for trade.";
        banditCamp.questType = QuestType.Combat;
        banditCamp.difficulty = QuestDifficulty.Easy;
        banditCamp.timeLimitDays = 5;
        banditCamp.targetAreaName = "Darkwood Forest";
        banditCamp.targetMapPosition = new Vector2Int(45, 60);
        banditCamp.goldReward = 75;
        banditCamp.renownReward = 10;
        banditCamp.renownPenalty = 5;
        banditCamp.questTags = new string[] { "combat", "bandits", "forest", "easy" };
        
        banditCamp.goals.Add(new QuestGoal
        {
            description = "Defeat 3 Bandits",
            goalType = QuestGoalType.KillEnemies,
            targetName = "Bandit",
            targetCount = 3
        });
        
        banditCamp.goals.Add(new QuestGoal
        {
            description = "Defeat the Bandit Leader",
            goalType = QuestGoalType.DefeatBoss,
            targetName = "Bandit Chief",
            targetCount = 1
        });
        
        AssetDatabase.CreateAsset(banditCamp, "Assets/Resources/Quests/Combat/ClearBanditCamp.asset");
        
        // Hard Combat Quest
        var orcRaid = ScriptableObject.CreateInstance<Quest>();
        orcRaid.questTitle = "Orc Warband Threat";
        orcRaid.questDescription = "A large orc warband has been spotted moving toward the village. They must be stopped before they reach the settlement and cause havoc.";
        orcRaid.questType = QuestType.Combat;
        orcRaid.difficulty = QuestDifficulty.Hard;
        orcRaid.timeLimitDays = 3;
        orcRaid.timeLimitHours = 12;
        orcRaid.targetAreaName = "Ironback Mountains";
        orcRaid.targetMapPosition = new Vector2Int(80, 30);
        orcRaid.goldReward = 200;
        orcRaid.renownReward = 25;
        orcRaid.renownPenalty = 15;
        orcRaid.minimumRenown = 15;
        orcRaid.questTags = new string[] { "combat", "orcs", "mountains", "hard" };
        
        orcRaid.goals.Add(new QuestGoal
        {
            description = "Defeat 8 Orc Warriors",
            goalType = QuestGoalType.KillEnemies,
            targetName = "Orc Warrior",
            targetCount = 8
        });
        
        orcRaid.goals.Add(new QuestGoal
        {
            description = "Defeat the Orc Warchief",
            goalType = QuestGoalType.DefeatBoss,
            targetName = "Orc Warchief",
            targetCount = 1
        });
        
        AssetDatabase.CreateAsset(orcRaid, "Assets/Resources/Quests/Combat/OrcWarbandThreat.asset");
    }
    
    private static void CreateRescueQuests()
    {
        EnsureFolderExists("Assets/Resources/Quests/Rescue");
        
        // Missing Merchant
        var missingMerchant = ScriptableObject.CreateInstance<Quest>();
        missingMerchant.questTitle = "Missing Merchant";
        missingMerchant.questDescription = "Merchant Harold departed for the next town three days ago but never arrived. His family fears the worst. Find him and bring him home safely.";
        missingMerchant.questType = QuestType.Rescue;
        missingMerchant.difficulty = QuestDifficulty.Normal;
        missingMerchant.timeLimitDays = 7;
        missingMerchant.targetAreaName = "Whispering Hills";
        missingMerchant.targetMapPosition = new Vector2Int(65, 45);
        missingMerchant.goldReward = 120;
        missingMerchant.renownReward = 15;
        missingMerchant.renownPenalty = 10;
        missingMerchant.questTags = new string[] { "rescue", "merchant", "hills", "normal" };
        
        missingMerchant.goals.Add(new QuestGoal
        {
            description = "Find Merchant Harold",
            goalType = QuestGoalType.RescuePerson,
            targetName = "Harold",
            targetCount = 1
        });
        
        missingMerchant.goals.Add(new QuestGoal
        {
            description = "Escort Harold to safety",
            goalType = QuestGoalType.ReachLocation,
            targetName = "Safe Location",
            targetCount = 1
        });
        
        AssetDatabase.CreateAsset(missingMerchant, "Assets/Resources/Quests/Rescue/MissingMerchant.asset");
        
        // Kidnapped Noble
        var kidnappedNoble = ScriptableObject.CreateInstance<Quest>();
        kidnappedNoble.questTitle = "The Baron's Daughter";
        kidnappedNoble.questDescription = "Lady Elara, the Baron's daughter, has been kidnapped by cultists. Time is running short - rescue her before the dark ritual begins at midnight.";
        kidnappedNoble.questType = QuestType.Rescue;
        kidnappedNoble.difficulty = QuestDifficulty.Hard;
        kidnappedNoble.timeLimitDays = 2;
        kidnappedNoble.targetAreaName = "Cursed Ruins";
        kidnappedNoble.targetMapPosition = new Vector2Int(25, 75);
        kidnappedNoble.goldReward = 300;
        kidnappedNoble.renownReward = 30;
        kidnappedNoble.renownPenalty = 20;
        kidnappedNoble.minimumRenown = 20;
        kidnappedNoble.questTags = new string[] { "rescue", "noble", "cultists", "urgent" };
        
        kidnappedNoble.goals.Add(new QuestGoal
        {
            description = "Infiltrate the cultist stronghold",
            goalType = QuestGoalType.ReachLocation,
            targetName = "Cultist Hideout",
            targetCount = 1
        });
        
        kidnappedNoble.goals.Add(new QuestGoal
        {
            description = "Defeat the Cult Leader",
            goalType = QuestGoalType.DefeatBoss,
            targetName = "Cult Leader",
            targetCount = 1
        });
        
        kidnappedNoble.goals.Add(new QuestGoal
        {
            description = "Rescue Lady Elara",
            goalType = QuestGoalType.RescuePerson,
            targetName = "Lady Elara",
            targetCount = 1
        });
        
        AssetDatabase.CreateAsset(kidnappedNoble, "Assets/Resources/Quests/Rescue/BaronsDaughter.asset");
    }
    
    private static void CreateRetrievalQuests()
    {
        EnsureFolderExists("Assets/Resources/Quests/Retrieval");
        
        // Stolen Goods
        var stolenGoods = ScriptableObject.CreateInstance<Quest>();
        stolenGoods.questTitle = "Recover the Stolen Shipment";
        stolenGoods.questDescription = "Thieves made off with a valuable shipment of goods bound for the capital. Track them down and recover the stolen merchandise before they can sell it off.";
        stolenGoods.questType = QuestType.Retrieval;
        stolenGoods.difficulty = QuestDifficulty.Normal;
        stolenGoods.timeLimitDays = 4;
        stolenGoods.targetAreaName = "Smuggler's Cove";
        stolenGoods.targetMapPosition = new Vector2Int(90, 20);
        stolenGoods.goldReward = 150;
        stolenGoods.renownReward = 12;
        stolenGoods.renownPenalty = 8;
        stolenGoods.questTags = new string[] { "retrieval", "thieves", "shipment", "normal" };
        
        stolenGoods.goals.Add(new QuestGoal
        {
            description = "Recover the stolen goods",
            goalType = QuestGoalType.CollectItems,
            targetName = "Stolen Shipment",
            targetCount = 3
        });
        
        stolenGoods.goals.Add(new QuestGoal
        {
            description = "Defeat the Thief Leader (Optional)",
            goalType = QuestGoalType.DefeatBoss,
            targetName = "Thief Captain",
            targetCount = 1,
            isOptional = true
        });
        
        AssetDatabase.CreateAsset(stolenGoods, "Assets/Resources/Quests/Retrieval/StolenShipment.asset");
        
        // Ancient Artifact
        var ancientArtifact = ScriptableObject.CreateInstance<Quest>();
        ancientArtifact.questTitle = "The Lost Grimoire";
        ancientArtifact.questDescription = "An ancient grimoire containing powerful spells has been lost in the depths of the old ruins. Retrieve it before it falls into the wrong hands.";
        ancientArtifact.questType = QuestType.Retrieval;
        ancientArtifact.difficulty = QuestDifficulty.Legendary;
        ancientArtifact.timeLimitDays = 10;
        ancientArtifact.targetAreaName = "Ancient Ruins";
        ancientArtifact.targetMapPosition = new Vector2Int(15, 85);
        ancientArtifact.goldReward = 500;
        ancientArtifact.renownReward = 50;
        ancientArtifact.renownPenalty = 25;
        ancientArtifact.minimumRenown = 40;
        ancientArtifact.itemRewards = new List<string> { "Magical Staff", "Enchanted Robes" };
        ancientArtifact.questTags = new string[] { "retrieval", "artifact", "ruins", "legendary" };
        
        ancientArtifact.goals.Add(new QuestGoal
        {
            description = "Navigate the ancient ruins",
            goalType = QuestGoalType.ReachLocation,
            targetName = "Inner Sanctum",
            targetCount = 1
        });
        
        ancientArtifact.goals.Add(new QuestGoal
        {
            description = "Defeat the Guardian Construct",
            goalType = QuestGoalType.DefeatBoss,
            targetName = "Stone Guardian",
            targetCount = 1
        });
        
        ancientArtifact.goals.Add(new QuestGoal
        {
            description = "Retrieve the Lost Grimoire",
            goalType = QuestGoalType.CollectItems,
            targetName = "Ancient Grimoire",
            targetCount = 1
        });
        
        AssetDatabase.CreateAsset(ancientArtifact, "Assets/Resources/Quests/Retrieval/LostGrimoire.asset");
    }
    
    private static void CreateExplorationQuests()
    {
        EnsureFolderExists("Assets/Resources/Quests/Exploration");
        
        // Map the Region
        var mapRegion = ScriptableObject.CreateInstance<Quest>();
        mapRegion.questTitle = "Survey the Northern Territories";
        mapRegion.questDescription = "The northern territories remain largely unmapped. Explore the region and report back with your findings to help future expeditions.";
        mapRegion.questType = QuestType.Exploration;
        mapRegion.difficulty = QuestDifficulty.Easy;
        mapRegion.timeLimitDays = 14;
        mapRegion.targetAreaName = "Northern Wilderness";
        mapRegion.targetMapPosition = new Vector2Int(50, 90);
        mapRegion.goldReward = 80;
        mapRegion.renownReward = 8;
        mapRegion.renownPenalty = 3;
        mapRegion.questTags = new string[] { "exploration", "survey", "wilderness", "easy" };
        
        mapRegion.goals.Add(new QuestGoal
        {
            description = "Explore 5 different locations",
            goalType = QuestGoalType.ReachLocation,
            targetName = "Exploration Point",
            targetCount = 5
        });
        
        mapRegion.goals.Add(new QuestGoal
        {
            description = "Survive for 7 days in the wilderness",
            goalType = QuestGoalType.SurviveTime,
            targetName = "Wilderness",
            targetCount = 7
        });
        
        AssetDatabase.CreateAsset(mapRegion, "Assets/Resources/Quests/Exploration/SurveyNorthern.asset");
    }
    
    private static void EnsureFolderExists(string path)
    {
        string[] folders = path.Split('/');
        string currentPath = "";
        
        for (int i = 0; i < folders.Length; i++)
        {
            if (i == 0)
            {
                currentPath = folders[i];
                continue;
            }
            
            string parentPath = currentPath;
            currentPath += "/" + folders[i];
            
            if (!AssetDatabase.IsValidFolder(currentPath))
            {
                AssetDatabase.CreateFolder(parentPath, folders[i]);
            }
        }
    }
#endif
}
