//! Demonstration of new behavior tree keyword syntax
//! This file showcases all the new features approved for implementation

// Simple behavior with choose and then
behavior SimpleGuardPatrol {
    ---description
    A village guard who responds to threats or patrols.
    Demonstrates basic choose/then keywords.
    ---

    choose patrol_action {
        then respond_to_threat {
            if(threat_detected)
            sound_alarm
            rush_to_threat
        }

        repeat {
            then {
                patrol_north
                patrol_east
                patrol_south
                patrol_west
            }
        }
    }
}

// Named nodes
behavior ComplexBehavior {
    ---description
    Demonstrates named nodes for better organization and debugging.
    Labels help identify which part of the tree is executing.
    ---

    choose root {
        then handle_urgent {
            when(need.any is urgent)
            identify_need
            satisfy_need
        }

        then social_interaction {
            when(friend_nearby)
            greet_friend
            chat
        }

        then default_activity {
            idle_wait
        }
    }
}

// Advanced decorators
behavior WhiteRabbitAnxiety {
    ---description
    The White Rabbit from Alice in Wonderland, demonstrating
    all decorator types: repeat, retry, timeout, cooldown, if.
    ---

    choose {
        // Extreme panic when very late
        then panic_mode {
            if(minutes_late > 100)
            timeout(3s) {
                repeat(5) {
                    then {
                        check_pocket_watch
                        mutter_desperately
                    }
                }
            }
            sprint_to_destination
        }

        // Try to find alternate route when blocked
        then find_route {
            if(obstacle_encountered)
            drop_gloves
            drop_fan
            retry(2) {
                find_alternate_route
            }
        }

        // Queen response with cooldown
        then queen_response {
            if(queen_nearby)
            flatten_ears_in_fear
            trembling_bow
            cooldown(60s) {
                await_commands
            }
        }

        // Only run if energy is sufficient
        if(energy > 30) {
            scurry_around_frantically
        }

        // Default: perpetual anxiety
        repeat {
            then {
                check_watch
                mutter_anxiously
                scurry_forward
            }
        }
    }
}

// Mad Tea Party with infinite repeat
behavior MadTeaParty {
    ---description
    The Mad Hatter's tea party that loops forever.
    Demonstrates infinite repeat decorator.
    ---

    repeat {
        choose tea_party_activities {
            then riddles {
                pose_riddle
                wait_for_answer
                declare_answer_wrong
            }

            then seat_rotation {
                switch_seats
                clean_dirty_teacup
                pour_fresh_tea
            }

            then dormouse_interaction {
                wake_dormouse
                listen_to_brief_story
                stuff_dormouse_in_teapot
            }
        }
    }
}

// Cheshire Cat with timing decorators
behavior CheshireCatMaterialization {
    ---description
    Complex behavior with nested decorators showing gradual
    materialization and dematerialization with timing controls.
    ---

    choose {
        // Materialize when Alice is near
        then materialize {
            if(alice_nearby and visibility < 0.1)
            timeout(10s) {
                repeat(5) {
                    then {
                        increase_visibility(0.2)
                        pause_for_effect(1s)
                    }
                }
            }
            materialize_grin
            speak_in_riddles
        }

        // Dematerialize when fully visible
        then dematerialize {
            if(visibility > 0.9)
            cooldown(30s) {
                timeout(8s) {
                    repeat(5) {
                        then {
                            decrease_visibility(0.2)
                            pause_for_effect(1s)
                        }
                    }
                }
            }
            vanish_except_grin
        }

        // Only float when partially visible
        if(visibility >= 0.5) {
            idle_float
        }
    }
}

// Subtree inclusion
behavior WorkBehaviorWithSubtree {
    ---description
    Demonstrates subtree inclusion with the new 'include' keyword.
    ---

    choose {
        include common::handle_urgent_needs

        then work {
            when(at_workplace)
            perform_work_tasks
        }

        idle_wait
    }
}

// Invert decorator
behavior AvoidEnemies {
    ---description
    Uses invert decorator to reverse condition logic.
    ---

    choose {
        then safe_path {
            invert {
                if(enemy_nearby)
            }
            take_direct_route
        }

        then danger_path {
            take_long_safe_route
        }
    }
}

// Repeat with range
behavior SearchBehavior {
    ---description
    Demonstrates repeat with min..max range.
    ---

    repeat(3..7) {
        then {
            search_area
            rest_briefly
        }
    }
}

// Always succeed/fail decorators (for debugging)
behavior DebugBehavior {
    ---description
    Shows succeed_always and fail_always decorators.
    ---

    choose {
        succeed_always {
            experimental_feature
        }

        fail_always {
            disabled_legacy_behavior
        }

        fallback_behavior
    }
}
