-- Database Fixes for Website Monitoring Frontend
-- Run this in your Supabase SQL editor to fix the console errors

-- 1. Add missing columns to crawl_sessions table that the API expects
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS total_urls INTEGER DEFAULT 0;
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS processed_urls INTEGER DEFAULT 0;
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS progress_percentage INTEGER DEFAULT 0;

-- 2. Ensure all required columns exist in crawl_sessions table
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS pages_discovered INTEGER DEFAULT 0;
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS pages_processed INTEGER DEFAULT 0;
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS current_url VARCHAR;
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS error_message TEXT;
ALTER TABLE crawl_sessions ADD COLUMN IF NOT EXISTS metadata JSONB DEFAULT '{}'::jsonb;

-- 3. Add missing columns to users table if they don't exist
ALTER TABLE users ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW();
ALTER TABLE users ADD COLUMN IF NOT EXISTS last_login_at TIMESTAMPTZ;
ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR DEFAULT 'member';

-- 4. Add missing columns to organizations table if they don't exist
ALTER TABLE organizations ADD COLUMN IF NOT EXISTS api_key TEXT;
ALTER TABLE organizations ADD COLUMN IF NOT EXISTS max_websites INTEGER DEFAULT 10;
ALTER TABLE organizations ADD COLUMN IF NOT EXISTS max_scans_per_month INTEGER DEFAULT 1000;
ALTER TABLE organizations ADD COLUMN IF NOT EXISTS subscription_status VARCHAR DEFAULT 'active';

-- 5. Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_crawl_sessions_status ON crawl_sessions(status);
CREATE INDEX IF NOT EXISTS idx_crawl_sessions_website_status ON crawl_sessions(website_id, status);
CREATE INDEX IF NOT EXISTS idx_users_organization_id ON users(organization_id);
CREATE INDEX IF NOT EXISTS idx_websites_organization_id ON websites(organization_id);

-- 6. Ensure RLS policies exist for crawl_sessions
-- Enable RLS if not already enabled
ALTER TABLE crawl_sessions ENABLE ROW LEVEL SECURITY;

-- Create basic RLS policy for crawl_sessions if it doesn't exist
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'crawl_sessions' 
        AND policyname = 'Allow read for authenticated users'
    ) THEN
        CREATE POLICY "Allow read for authenticated users" ON crawl_sessions
            FOR SELECT USING (true);
    END IF;
    
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'crawl_sessions' 
        AND policyname = 'Allow insert for authenticated users'
    ) THEN
        CREATE POLICY "Allow insert for authenticated users" ON crawl_sessions
            FOR INSERT WITH CHECK (true);
    END IF;
    
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'crawl_sessions' 
        AND policyname = 'Allow update for authenticated users'
    ) THEN
        CREATE POLICY "Allow update for authenticated users" ON crawl_sessions
            FOR UPDATE USING (true);
    END IF;
END $$;

-- 7. Refresh Supabase schema cache to pick up new columns
-- This is important to resolve "Could not find column in schema cache" errors
NOTIFY pgrst, 'reload schema';

-- 8. Verify the fixes by checking table structure
-- You can run these queries to verify the fixes worked:
-- SELECT column_name, data_type, is_nullable, column_default 
-- FROM information_schema.columns 
-- WHERE table_name = 'crawl_sessions' 
-- ORDER BY ordinal_position;

COMMIT;

