-- Fix beacon coordinates: Add lat/long to fetch_beacons function
-- This replaces the existing function to include actual beacon coordinates

CREATE OR REPLACE FUNCTION fetch_beacons(
    lat DOUBLE PRECISION,
    long DOUBLE PRECISION,
    radius_meters DOUBLE PRECISION DEFAULT 5000,
    beacon_type_filter TEXT DEFAULT NULL,
    limit_count INTEGER DEFAULT 50
)
RETURNS TABLE (
    id UUID,
    body TEXT,
    author_id UUID,
    beacon_type TEXT,
    confidence_score NUMERIC,
    is_active_beacon BOOLEAN,
    created_at TIMESTAMPTZ,
    distance_meters DOUBLE PRECISION,
    beacon_lat DOUBLE PRECISION,
    beacon_long DOUBLE PRECISION,
    author_handle TEXT,
    author_display_name TEXT,
    author_avatar_url TEXT,
    vouch_count INTEGER,
    report_count INTEGER,
    status_color TEXT
) LANGUAGE plpgsql STABLE AS $$
BEGIN
    RETURN QUERY
    SELECT
        p.id,
        p.body,
        p.author_id,
        p.beacon_type::TEXT,
        p.confidence_score,
        p.is_active_beacon,
        p.created_at,
        ST_Distance(p.location, ST_SetSRID(ST_MakePoint(long, lat), 4326)::geography) AS distance_meters,
        ST_Y(p.location::geometry) AS beacon_lat,
        ST_X(p.location::geometry) AS beacon_long,
        prof.handle AS author_handle,
        prof.display_name AS author_display_name,
        prof.avatar_url AS author_avatar_url,
        COALESCE(vouch.cnt, 0)::INT,
        COALESCE(report.cnt, 0)::INT,
        get_beacon_status_color(p.confidence_score)
    FROM posts p
    LEFT JOIN profiles prof ON p.author_id = prof.id
    LEFT JOIN (SELECT beacon_id, COUNT(*)::INT AS cnt FROM beacon_votes WHERE vote_type = 'vouch' GROUP BY beacon_id) vouch ON p.id = vouch.beacon_id
    LEFT JOIN (SELECT beacon_id, COUNT(*)::INT AS cnt FROM beacon_votes WHERE vote_type = 'report' GROUP BY beacon_id) report ON p.id = report.beacon_id
    WHERE p.is_beacon = TRUE
      AND p.is_active_beacon = TRUE
      AND p.deleted_at IS NULL
      AND (beacon_type_filter IS NULL OR p.beacon_type::TEXT = beacon_type_filter)
      AND ST_DWithin(p.location, ST_SetSRID(ST_MakePoint(long, lat), 4326)::geography, radius_meters)
    ORDER BY p.confidence_score DESC, p.created_at DESC
    LIMIT limit_count;
END;
$$;

-- Create alias function for backward compatibility with Dart service
CREATE OR REPLACE FUNCTION fetch_nearby_beacons(
    p_lat DOUBLE PRECISION,
    p_long DOUBLE PRECISION,
    p_radius INTEGER DEFAULT 5000
)
RETURNS TABLE (
    id UUID,
    body TEXT,
    author_id UUID,
    beacon_type TEXT,
    confidence_score NUMERIC,
    is_active_beacon BOOLEAN,
    created_at TIMESTAMPTZ,
    distance_meters DOUBLE PRECISION,
    beacon_lat DOUBLE PRECISION,
    beacon_long DOUBLE PRECISION,
    author_handle TEXT,
    author_display_name TEXT,
    author_avatar_url TEXT,
    vouch_count INTEGER,
    report_count INTEGER,
    status_color TEXT
) LANGUAGE plpgsql STABLE AS $$
BEGIN
    RETURN QUERY
    SELECT * FROM fetch_beacons(p_lat, p_long, p_radius::DOUBLE PRECISION, NULL, 50);
END;
$$;
