"""Main entry point for the school simulation demo.

This demonstrates the service-centric ABM framework with a school model
that includes students, teachers, counselors, friendships, and disciplinary actions.
"""

import sys
from pathlib import Path
from datetime import datetime

# Add framework root to path
framework_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(framework_root))
sys.path.insert(0, str(framework_root / "src"))

from demos.school_demo.src.schoolWorld import SchoolWorld
from demos.school_demo.src.schoolSingleRunner import SchoolSingleRunner
from demos.school_demo.src.utils import TeeOutput, print_header, print_initial_state, print_final_state



def main():
    """Main entry point for the school simulation."""

    # Create timestamped output filename
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    output_dir = Path(__file__).parent / "results"
    output_dir.mkdir(exist_ok=True)
    output_file = output_dir / f"school_sim_{timestamp}.txt"

    # Write to both console and file
    with TeeOutput(output_file):
        print(f"Output will be saved to: {output_file}\n")

        # Default parameters
        params = {
            "random_seed": 42,  # For reproducibility
            "max_steps": 1000,

            "studentCount": 100,
            "teacherCount": 5,
            "counselorCount": 2,
            "neighborhood_count": 3,

            #student parameters
            "student_racial_makeup": 0,
            "baseline_friendship_prob": 0,

            #student-mood parameters
            "student_starting_mood": 0,
            "friendship_influence_mood_coef": 0,
            "neighborhood_influence_mood_coef": 0,

            #student-substance parameters
            "friendship_influence_substance_coef": 0,
            "substance_influence_friendship_coef": 0,


            #teacher parameters
            "teacher_racial_makeup": 0,
            "bias_elements" : 0,
            "teacher_implicit_bias": 0,
            "teacher_attitude": 0,

            #counselor parameters
            "counselor_racial_makeup": 0,
            "counselor_attitude": 0,


            #housing parameters
            "prob_change_neighborhood": 0,

        }

        print_header("INITIALIZING SCHOOL SIMULATION")
        print(f"\nParameters:")
        for key, value in params.items():
            print(f"  {key}: {value}")

        # Set up world
        world = SchoolWorld(params)

        print_initial_state(world)

        # Setup events (services schedule themselves)
        world.setup_events()

        # Run simulation
        runner = SchoolSingleRunner(world, params)
        runner.run_simulation(verbose=True)

        print_header("SIMULATION FINISHED")
        print("\n✓ School simulation completed successfully!")
        print(f"✓ Final state saved in 'world' object")
        print(f"✓ Access results via world.population, world.friendshipManager, etc.")
        print(f"✓ Results saved to: {output_file}\n")

    return world


if __name__ == "__main__":
    world = main()