-- Mock Supabase auth schema for local migration testing.
-- In real Supabase, the auth schema + auth.users table + auth.uid() function
-- are created by the platform; locally we simulate them.
--
-- Usage: psql <DATABASE_URL> -f internal/db/migrations/_dev/mock_supabase_auth.sql
--
-- For visibility tests, set the GUC paliad_test.uid in a transaction:
--   BEGIN;
--   SET LOCAL paliad_test.uid = '<some-user-uuid>';
--   SELECT paliad.can_see_akte('<some-akte-uuid>');
--   COMMIT;
--
-- _dev/ prefix excludes this file from golang-migrate's source pattern
-- (which only reads top-level *.sql in the migrations dir).

CREATE SCHEMA IF NOT EXISTS auth;

CREATE TABLE IF NOT EXISTS auth.users (
    id    uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    email text NOT NULL UNIQUE
);

CREATE OR REPLACE FUNCTION auth.uid() RETURNS uuid
LANGUAGE sql STABLE AS $$
    SELECT NULLIF(current_setting('paliad_test.uid', true), '')::uuid;
$$;

DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'authenticated') THEN
        CREATE ROLE authenticated;
    END IF;
END $$;

CREATE EXTENSION IF NOT EXISTS pgcrypto;
