use crate::components::*;
use bevy::{light::NotShadowCaster, prelude::*};

pub fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    asset_server: Res<AssetServer>,
) {
    // Camera
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(0.0, 25.0, -25.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));

    // Light
    commands.insert_resource(AmbientLight {
        color: Color::WHITE,
        brightness: 200.0, // Subtle fill light so shadows aren't pitch black
        ..default()
    });

    commands.spawn((
        DirectionalLight {
            illuminance: 1000.0,
            shadows_enabled: true,
            ..default()
        },
        Transform::from_rotation(Quat::from_euler(
            EulerRot::XYZ,
            -0.8, // Pitch down (looking down at scene)
            0.5,  // Yaw (angle from side)
            0.0,  // Roll (no rotation)
        )),
    ));

    // Floor plane
    commands.spawn((
        Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(50.0)))),
        MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
    ));

    // Fryer
    commands.spawn((
        Mesh3d(meshes.add(Cuboid::new(2.0, 1.75, 2.0))),
        Transform::from_xyz(-10.0, 0.875, 0.0),
        MeshMaterial3d(materials.add(Color::srgb(0.1, 0.1, 0.1))),
        Fryer {
            range: 3.0,
            speed: 50.0,
        },
    ));

    // Front Counter
    commands.spawn((
        Mesh3d(meshes.add(Cuboid::new(2.0, 1.5, 10.0))),
        Transform::from_xyz(10.0, 0.75, 0.0),
        MeshMaterial3d(materials.add(Color::srgb(0.7, 0.7, 0.7))),
    ));

    // Player
    commands.spawn((
        SceneRoot(asset_server.load("models/employee.glb#Scene0")),
        Transform::from_xyz(0.0, 1.5, 0.0).with_scale(Vec3::splat(0.5)),
        Unit {
            move_speed: 10.0,
            height_offset: 1.5,
        },
        Player {
            holding: Item::None,
            progress: 0.0,
        },
        MoveTarget {
            position: Vec3::new(0.0, 0.0, 0.0),
        },
        // Burger indicator
        children![(
            SceneRoot(asset_server.load("models/burger.glb#Scene0")),
            Transform::from_xyz(0.0, 7.1, 0.0).with_scale(Vec3::splat(0.9)),
            Visibility::Hidden,
            PlayerBurgerIndicator,
        )],
    ));

    // ProgressBar
    commands.spawn((
        Mesh3d(meshes.add(Cuboid::new(4.0, 0.5, 0.1))),
        MeshMaterial3d(materials.add(Color::srgb(0.0, 1.0, 0.0))),
        Transform::from_xyz(0.0, 0.0, 0.0),
        ProgressBar {
            width: 1.0,
            player_height: 4.0,
        },
        NotShadowCaster,
    ));

    // Customer spawner
    commands.insert_resource(CounterQueue {
        positions: vec![
            Vec3::new(12.0, 0.0, -4.0),
            Vec3::new(12.0, 0.0, 0.0),
            Vec3::new(12.0, 0.0, 4.0),
        ],
        occupied: vec![false, false, false],
    });

    commands.insert_resource(CustomerSpawner {
        timer: Timer::from_seconds(7.0, TimerMode::Repeating),
    });
}
