#version 330

out vec4 f_color;

in vec2 uv;
in float attenuation;
in vec3 color;

uniform float cone_angle;  // Cone angle passed from Python
uniform float ambient_brightness;  // Ambient light level (0-1)

void main() {
    // Convert UV to centered coordinates (-1 to 1)
    vec2 centered = uv * 2.0 - vec2(1.0);
    
    // Calculate angle from center (0° = right, 90° = up)
    float angle = atan(centered.y, centered.x);
    
    // Distance from center
    float dist = length(centered);
    
    // Discard outside radius (keep cone shape)
    if (dist > 1.0) discard;
    
    // Calculate if fragment is inside cone
    // Cone points to the right (0°), spreads cone_angle/2 up and down
    float angle_from_forward = abs(angle);
    float max_angle = radians(cone_angle / 2.0);
    
    // Discard fragments outside cone angle
    if (angle_from_forward > max_angle) discard;
    
    // Angle fade (smooth sides)
    float angle_normalized = 1.0 - (angle_from_forward / max_angle);
    float angle_fade = smoothstep(0.0, 1.0, angle_normalized);
    angle_fade = pow(angle_fade, 0.8);
    
    // Distance fade - simple linear
    float dist_fade = 1.0 - dist;
    
    // Invert intensity based on ambient - bright in dark, dim in light!
    float ambient_factor = 1.0 - ambient_brightness;  // 1.0 at night, 0.0 during day
    
    // Combine
    float final_intensity = clamp(dist_fade * angle_fade * ambient_factor, 0.0, 1.0);
    
    f_color = vec4(color * final_intensity, 0.0);
}
