#version 330 core
in vec2 TexCoords;
out vec4 FragColor;

uniform sampler2D uTexture;
uniform vec4 outlineColor = vec4(1.0, 1.0, 0.0, 1.0); // Yellow outline
uniform float threshold = 0.1; // Alpha threshold
uniform float outlineWidth = 1.0 / 256.0; // Adjust based on resolution

void main()
{
    float alpha = texture(uTexture, TexCoords).a;

    // If we're fully inside the texture, draw normally
    if (alpha > threshold)
    {
        FragColor = texture(uTexture, TexCoords);
        return;
    }

    // Sample neighbors to detect edges
    bool isEdge = false;
    for (int x = -1; x <= 1; ++x)
    {
        for (int y = -1; y <= 1; ++y)
        {
            if (x == 0 && y == 0) continue;
            vec2 offset = vec2(x, y) * outlineWidth;
            float neighborAlpha = texture(uTexture, TexCoords + offset).a;
            if (neighborAlpha > threshold)
            {
                isEdge = true;
                break;
            }
        }
        if (isEdge) break;
    }

    if (isEdge)
        FragColor = outlineColor;
    else
        discard;
}
