-- Migration: Ensure AuditLog and AdminSettings use UUIDs
-- Date: 2024-11-30

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

-- ==========================================
-- TABLE: audit_logs
-- ==========================================

DO $$
BEGIN
    -- Check if audit_logs exists
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'audit_logs') THEN
        -- Check if user_id exists and is NOT uuid
        IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'audit_logs' AND column_name = 'user_id' AND data_type NOT IN ('uuid')) THEN
            RAISE NOTICE 'Converting audit_logs.user_id to UUID. Warning: Old integer IDs will be lost (TRUNCATE).';
            
            -- We truncate because we cannot map old int IDs to new UUIDs without the mapping table (which might be gone)
            TRUNCATE TABLE audit_logs;
            
            ALTER TABLE audit_logs DROP COLUMN IF EXISTS user_id;
            ALTER TABLE audit_logs ADD COLUMN user_id UUID;
            
            -- Also migrate ID if it's int
            IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'audit_logs' AND column_name = 'id' AND data_type NOT IN ('uuid')) THEN
                 ALTER TABLE audit_logs DROP CONSTRAINT IF EXISTS audit_logs_pkey;
                 ALTER TABLE audit_logs DROP COLUMN IF EXISTS id;
                 ALTER TABLE audit_logs ADD COLUMN id UUID PRIMARY KEY DEFAULT gen_random_uuid();
            END IF;
            
            -- ResourceID
            IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'audit_logs' AND column_name = 'resource_id' AND data_type NOT IN ('uuid')) THEN
                 ALTER TABLE audit_logs DROP COLUMN IF EXISTS resource_id;
                 ALTER TABLE audit_logs ADD COLUMN resource_id UUID;
            END IF;
        END IF;
    END IF;
END $$;

-- ==========================================
-- TABLE: admin_settings
-- ==========================================

-- Create if not exists
CREATE TABLE IF NOT EXISTS admin_settings (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    key VARCHAR(255) NOT NULL UNIQUE,
    value TEXT,
    type VARCHAR(50),
    description TEXT,
    category VARCHAR(50),
    is_public BOOLEAN DEFAULT false,
    updated_by UUID,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- If it exists but has int ID
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'admin_settings' AND column_name = 'id' AND data_type NOT IN ('uuid')) THEN
        ALTER TABLE admin_settings ADD COLUMN id_uuid UUID DEFAULT gen_random_uuid();
        ALTER TABLE admin_settings DROP CONSTRAINT IF EXISTS admin_settings_pkey;
        ALTER TABLE admin_settings DROP COLUMN id;
        ALTER TABLE admin_settings RENAME COLUMN id_uuid TO id;
        ALTER TABLE admin_settings ADD PRIMARY KEY (id);
    END IF;
    
     IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'admin_settings' AND column_name = 'updated_by' AND data_type NOT IN ('uuid')) THEN
        ALTER TABLE admin_settings DROP COLUMN updated_by;
        ALTER TABLE admin_settings ADD COLUMN updated_by UUID;
    END IF;
END $$;
