-- ==========================================
-- 03: INSTALL POSTGRESQL EXTENSIONS
-- ==========================================
-- This script installs required extensions on all databases
-- Runs as: postgres (superuser)

\echo '🔌 Installing PostgreSQL extensions...'

-- ==========================================
-- DEVELOPMENT DATABASE
-- ==========================================
\c aurganize_dev

\echo ''
\echo '📦 Installing extensions on aurganize_dev...'

-- UUID Generation (Required)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
    WITH SCHEMA public
    VERSION "1.1";

COMMENT ON EXTENSION "uuid-ossp" IS 'Generate universally unique identifiers (UUIDs)';

\echo '  ✅ uuid-ossp installed'

-- Trigram Matching (For Fuzzy Search)
CREATE EXTENSION IF NOT EXISTS "pg_trgm"
    WITH SCHEMA public;

COMMENT ON EXTENSION "pg_trgm" IS 'Text similarity measurement and index searching based on trigrams';

\echo '  ✅ pg_trgm installed'

-- BTree GIN Index (For Complex Queries)
CREATE EXTENSION IF NOT EXISTS "btree_gin"
    WITH SCHEMA public;

COMMENT ON EXTENSION "btree_gin" IS 'Support for indexing common datatypes in GIN';

\echo '  ✅ btree_gin installed'

-- Test UUID generation
DO $$
DECLARE
    test_uuid UUID;
BEGIN
    test_uuid := uuid_generate_v4();
    RAISE NOTICE '  🧪 Test UUID generated: %', test_uuid;
END $$;

-- Test trigram similarity
DO $$
DECLARE
    similarity_score FLOAT;
BEGIN
    similarity_score := similarity('PostgreSQL', 'Postgres');
    RAISE NOTICE '  🧪 Trigram similarity test: %', similarity_score;
END $$;

-- ==========================================
-- STAGING DATABASE
-- ==========================================
\c aurganize_staging

\echo ''
\echo '📦 Installing extensions on aurganize_staging...'

CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public VERSION "1.1";
CREATE EXTENSION IF NOT EXISTS "pg_trgm" WITH SCHEMA public;
CREATE EXTENSION IF NOT EXISTS "btree_gin" WITH SCHEMA public;

\echo '  ✅ All extensions installed on staging'

-- ==========================================
-- PRODUCTION DATABASE
-- ==========================================
\c aurganize_prod

\echo ''
\echo '📦 Installing extensions on aurganize_prod...'

CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public VERSION "1.1";
CREATE EXTENSION IF NOT EXISTS "pg_trgm" WITH SCHEMA public;
CREATE EXTENSION IF NOT EXISTS "btree_gin" WITH SCHEMA public;

\echo '  ✅ All extensions installed on production'

-- ==========================================
-- SUMMARY
-- ==========================================
\echo ''
\echo '=========================================='
\echo '✅ Extensions installed successfully!'
\echo '=========================================='
\echo ''

-- List all extensions (on dev database)
\c aurganize_dev

SELECT 
    e.extname AS "Extension",
    e.extversion AS "Version",
    n.nspname AS "Schema"
FROM 
    pg_extension e
    JOIN pg_namespace n ON e.extnamespace = n.oid
WHERE 
    e.extname IN ('uuid-ossp', 'pg_trgm', 'btree_gin')
ORDER BY 
    e.extname;

\echo ''