-- Complete Fix for Sprint 1-3 Migration
-- Safe to run multiple times

-- ==============================================
-- Step 1: Add columns to users table
-- ==============================================

DO $$
BEGIN
    -- Add profile_photo_url
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'users' AND column_name = 'profile_photo_url'
    ) THEN
        ALTER TABLE users ADD COLUMN profile_photo_url VARCHAR;
        RAISE NOTICE 'Added profile_photo_url to users table';
    ELSE
        RAISE NOTICE 'profile_photo_url already exists in users table';
    END IF;

    -- Add social_media_facebook
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'users' AND column_name = 'social_media_facebook'
    ) THEN
        ALTER TABLE users ADD COLUMN social_media_facebook VARCHAR;
        RAISE NOTICE 'Added social_media_facebook to users table';
    ELSE
        RAISE NOTICE 'social_media_facebook already exists in users table';
    END IF;

    -- Add social_media_instagram
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'users' AND column_name = 'social_media_instagram'
    ) THEN
        ALTER TABLE users ADD COLUMN social_media_instagram VARCHAR;
        RAISE NOTICE 'Added social_media_instagram to users table';
    ELSE
        RAISE NOTICE 'social_media_instagram already exists in users table';
    END IF;

    -- Add social_media_twitter
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'users' AND column_name = 'social_media_twitter'
    ) THEN
        ALTER TABLE users ADD COLUMN social_media_twitter VARCHAR;
        RAISE NOTICE 'Added social_media_twitter to users table';
    ELSE
        RAISE NOTICE 'social_media_twitter already exists in users table';
    END IF;

    -- Add social_media_linkedin
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'users' AND column_name = 'social_media_linkedin'
    ) THEN
        ALTER TABLE users ADD COLUMN social_media_linkedin VARCHAR;
        RAISE NOTICE 'Added social_media_linkedin to users table';
    ELSE
        RAISE NOTICE 'social_media_linkedin already exists in users table';
    END IF;
END $$;

-- ==============================================
-- Step 2: Add columns to events table
-- ==============================================

DO $$
BEGIN
    -- Add microsoft_calendar_id
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'events' AND column_name = 'microsoft_calendar_id'
    ) THEN
        ALTER TABLE events ADD COLUMN microsoft_calendar_id VARCHAR;
        RAISE NOTICE 'Added microsoft_calendar_id to events table';
    ELSE
        RAISE NOTICE 'microsoft_calendar_id already exists in events table';
    END IF;

    -- Add microsoft_calendar_sync_enabled
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'events' AND column_name = 'microsoft_calendar_sync_enabled'
    ) THEN
        ALTER TABLE events ADD COLUMN microsoft_calendar_sync_enabled BOOLEAN DEFAULT FALSE;
        RAISE NOTICE 'Added microsoft_calendar_sync_enabled to events table';
    ELSE
        RAISE NOTICE 'microsoft_calendar_sync_enabled already exists in events table';
    END IF;
END $$;

-- ==============================================
-- Step 3: Fix storage_usage initialization
-- ==============================================

-- Delete any incomplete records
DELETE FROM storage_usage WHERE id IS NULL;

-- Insert initial record if table is empty
INSERT INTO storage_usage (id, total_bytes_used, max_bytes_allowed, last_updated)
SELECT
    gen_random_uuid(),
    0,
    10737418240, -- 10GB default
    CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM storage_usage);

-- ==============================================
-- Step 4: Verify everything
-- ==============================================

DO $$
DECLARE
    user_col_count INT;
    event_col_count INT;
    storage_count INT;
BEGIN
    -- Count users columns
    SELECT COUNT(*) INTO user_col_count
    FROM information_schema.columns
    WHERE table_name = 'users'
    AND column_name IN (
        'profile_photo_url',
        'social_media_facebook',
        'social_media_instagram',
        'social_media_twitter',
        'social_media_linkedin'
    );

    -- Count events columns
    SELECT COUNT(*) INTO event_col_count
    FROM information_schema.columns
    WHERE table_name = 'events'
    AND column_name IN (
        'microsoft_calendar_id',
        'microsoft_calendar_sync_enabled'
    );

    -- Count storage_usage records
    SELECT COUNT(*) INTO storage_count FROM storage_usage;

    -- Report results
    RAISE NOTICE '';
    RAISE NOTICE '========================================';
    RAISE NOTICE 'Migration Verification Results:';
    RAISE NOTICE '========================================';
    RAISE NOTICE 'Users table: %/5 columns added', user_col_count;
    RAISE NOTICE 'Events table: %/2 columns added', event_col_count;
    RAISE NOTICE 'Storage usage: % record(s)', storage_count;
    RAISE NOTICE '';

    IF user_col_count = 5 AND event_col_count = 2 AND storage_count > 0 THEN
        RAISE NOTICE '✅ Migration completed successfully!';
    ELSE
        RAISE NOTICE '⚠️  Migration incomplete. Please check the logs above.';
    END IF;
    RAISE NOTICE '========================================';
END $$;
