using UnityEngine;

/// <summary>
/// Forest Ambush - Bandits attacking from the woods
/// High probability in forests, lower on roads
/// </summary>
[CreateAssetMenu(fileName = "Forest Ambush", menuName = "RPG/Travel Events/Specific/Forest Ambush")]
public class ForestAmbushEvent : CombatTravelEvent
{
    void OnEnable()
    {
        eventName = "Forest Ambush";
        eventDescription = "Bandits leap from the undergrowth to ambush your party!";
        eventType = EventType.Combat;
        rarity = EventRarity.Common;

        // Very likely in forests, unlikely on roads
        forestChance = 0.95f;
        plainsChance = 0.3f;
        mountainChance = 0.6f;
        roadChance = 0.15f; // Much safer on roads
        townChance = 0.05f;
        villageChance = 0.2f;

        minEnemies = 2;
        maxEnemies = 4;
        // TODO: Assign possibleEnemies array with EnemyCharacterData assets
        // Example: possibleEnemies = new EnemyCharacterData[] { forestBanditAsset, brigandAsset, outlawAsset };

        encounterDescriptions = new string[]
        {
            "Bandits emerge from behind the trees to surround your party!",
            "Your party walks into a carefully planned ambush by forest brigands!",
            "Outlaws drop from the canopy above, weapons drawn!",
            "A group of bandits blocks the forest path ahead!"
        };

        canOccurMultipleTimes = true;
        cooldownDays = 0.5f; // Can happen again after half a day
    }
}

/// <summary>
/// Mountain Ambush - More dangerous encounter in treacherous terrain
/// </summary>
[CreateAssetMenu(fileName = "Mountain Ambush", menuName = "RPG/Travel Events/Specific/Mountain Ambush")]
public class MountainAmbushEvent : CombatTravelEvent
{
    void OnEnable()
    {
        eventName = "Mountain Ambush";
        eventDescription = "Mountain bandits use the rocky terrain to their advantage!";
        eventType = EventType.Combat;
        rarity = EventRarity.Common;

        // Most likely in mountains
        mountainChance = 0.9f;
        forestChance = 0.4f;
        plainsChance = 0.2f;
        roadChance = 0.1f;
        townChance = 0.02f;
        villageChance = 0.1f;

        minEnemies = 1;
        maxEnemies = 3;
        // TODO: Assign possibleEnemies array with EnemyCharacterData assets
        // Example: possibleEnemies = new EnemyCharacterData[] { mountainBanditAsset, highwaymanAsset, raiderAsset };

        encounterDescriptions = new string[]
        {
            "Mountain bandits rain arrows down from the cliffs above!",
            "Your party is caught in a narrow pass by armed raiders!",
            "Highwaymen emerge from rocky crevices to block your path!",
            "A rockslide reveals it was a trap - bandits attack!"
        };

        canOccurMultipleTimes = true;
        cooldownDays = 1f;
    }
}

/// <summary>
/// Traveling Merchant - Friendly trader offering goods
/// Much more likely on roads and near settlements
/// </summary>
[CreateAssetMenu(fileName = "Traveling Merchant", menuName = "RPG/Travel Events/Specific/Traveling Merchant")]
public class TravelingMerchantEvent : TradingTravelEvent
{
    void OnEnable()
    {
        eventName = "Traveling Merchant";
        eventDescription = "A friendly merchant offers to trade goods with your party.";
        eventType = EventType.Trading;
        rarity = EventRarity.Common;

        // Very likely on roads and near settlements
        roadChance = 0.85f;
        townChance = 0.6f;
        villageChance = 0.75f;
        bridgeChance = 0.7f; // Common at crossings
        ferryChance = 0.6f;

        // Less likely in wilderness
        forestChance = 0.25f;
        mountainChance = 0.15f;
        plainsChance = 0.4f;
        lakeChance = 0.3f;
        riverChance = 0.3f;

        merchantTypes = new string[] {
            "Traveling Merchant",
            "Caravan Trader",
            "Wandering Peddler",
            "Road Merchant"
        };

        canHaveRareItems = true;
        rareItemChance = 0.25f;
        priceModifier = 1.1f; // Slightly more expensive than town shops

        encounterDescriptions = new string[]
        {
            "A {merchantType} with a pack full of goods approaches your party.",
            "Your party encounters a {merchantType} resting beside the road.",
            "A {merchantType} calls out, offering to show you their wares.",
            "You come across a {merchantType} leading a pack animal loaded with goods."
        };

        canOccurMultipleTimes = true;
        cooldownDays = 0.25f; // Multiple merchants can be encountered
    }
}

/// <summary>
/// Road Patrol - Guards or soldiers patrolling main roads
/// Only occurs on roads, provides safety information
/// </summary>
[CreateAssetMenu(fileName = "Road Patrol", menuName = "RPG/Travel Events/Specific/Road Patrol")]
public class RoadPatrolEvent : TravelEvent
{
    [Header("Patrol Event Settings")]
    public string[] patrolTypes = { "City Guard Patrol", "Royal Guard", "Local Militia" };
    public bool providesInformation = true;
    public bool canRestoreHealth = true;
    public int healthRestored = 15;

    void OnEnable()
    {
        eventName = "Road Patrol";
        eventDescription = "A patrol of guards maintains order on the roads.";
        eventType = EventType.Social;
        rarity = EventRarity.Common;

        // Only on roads and near settlements
        roadChance = 0.9f;
        townChance = 0.8f;
        villageChance = 0.6f;
        bridgeChance = 0.5f;

        // Never in wilderness
        forestChance = 0f;
        mountainChance = 0f;
        plainsChance = 0.1f;
        lakeChance = 0f;
        riverChance = 0f;
        oceanChance = 0f;

        canOccurMultipleTimes = true;
        cooldownDays = 1f;
    }

    public override EventResult ExecuteEvent(TravelEventContext context)
    {
        string patrolType = patrolTypes[Random.Range(0, patrolTypes.Length)];
        string message = $"Your party encounters a {patrolType} maintaining security on the road.";

        if (canRestoreHealth && Random.value < 0.4f)
        {
            message += $" They provide aid, restoring {healthRestored} health to your party.";
            return new EventResult(message) { healthChange = healthRestored };
        }
        else if (providesInformation)
        {
            string[] infoMessages = {
                " They warn you about bandit activity ahead.",
                " They share information about safe camping spots nearby.",
                " They mention a trading post further down the road.",
                " They advise taking the main road for safer travel."
            };
            message += infoMessages[Random.Range(0, infoMessages.Length)];
        }

        return new EventResult(message);
    }
}

/// <summary>
/// Hidden Cache - Discovery of abandoned supplies
/// More likely off main roads in remote areas
/// </summary>
[CreateAssetMenu(fileName = "Hidden Cache", menuName = "RPG/Travel Events/Specific/Hidden Cache")]
public class HiddenCacheEvent : DiscoveryTravelEvent
{
    void OnEnable()
    {
        eventName = "Hidden Cache";
        eventDescription = "Your party discovers a hidden stash of supplies.";
        eventType = EventType.Discovery;
        rarity = EventRarity.Uncommon;

        // More likely in remote areas
        forestChance = 0.8f;
        mountainChance = 0.9f;
        plainsChance = 0.5f;
        riverChance = 0.6f;

        // Less likely on main routes
        roadChance = 0.2f;
        townChance = 0.05f;
        villageChance = 0.1f;

        minGoldFound = 25;
        maxGoldFound = 150;
        canFindItems = true;

        discoveryDescriptions = new string[]
        {
            "Your party discovers a carefully hidden cache containing {amount} gold!",
            "While exploring off the beaten path, you find an old smuggler's stash with {amount} gold pieces.",
            "A party member notices disturbed earth that reveals a buried cache worth {amount} gold!",
            "Behind some rocks, your party uncovers an abandoned supply cache with {amount} gold."
        };

        canOccurMultipleTimes = true;
        cooldownDays = 2f; // Takes time for new caches to be found
    }
}

/// <summary>
/// Wild Animal Encounter - Non-aggressive animal encounter
/// Can be peaceful or turn into combat based on party actions
/// </summary>
[CreateAssetMenu(fileName = "Wild Animal Encounter", menuName = "RPG/Travel Events/Specific/Wild Animal Encounter")]
public class WildAnimalEvent : TravelEvent
{
    [Header("Animal Encounter Settings")]
    public string[] animalTypes = { "Wolf Pack", "Brown Bear", "Wild Boar", "Mountain Lion" };
    public float aggressionChance = 0.3f; // Chance the animal attacks

    void OnEnable()
    {
        eventName = "Wild Animal Encounter";
        eventDescription = "Your party encounters wild animals.";
        eventType = EventType.Mystery;
        rarity = EventRarity.Common;

        // More likely in natural areas
        forestChance = 0.9f;
        mountainChance = 0.8f;
        plainsChance = 0.6f;
        riverChance = 0.5f;
        lakeChance = 0.4f;

        // Very unlikely near civilization
        roadChance = 0.2f;
        townChance = 0.02f;
        villageChance = 0.1f;

        canOccurMultipleTimes = true;
        cooldownDays = 0.5f;
    }

    public override EventResult ExecuteEvent(TravelEventContext context)
    {
        string animalType = animalTypes[Random.Range(0, animalTypes.Length)];

        if (Random.value < aggressionChance)
        {
            // Animal attacks
            return EventResult.SimpleBattle(
                $"A {animalType} attacks your party!",
                1,
                animalType
            );
        }
        else
        {
            // Peaceful encounter
            string[] peacefulMessages = {
                $"Your party spots a {animalType} in the distance. It notices you but moves away peacefully.",
                $"A {animalType} crosses your path but shows no aggression toward your party.",
                $"Your party carefully observes a {animalType} before it disappears into the wilderness."
            };

            return new EventResult(peacefulMessages[Random.Range(0, peacefulMessages.Length)]);
        }
    }
}
