"""0001 baseline

Capture full schema from live data/relay.db.

Revision ID: 0001
Revises: 
Create Date: 2026-06-15
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0001'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    op.execute("""CREATE TABLE ai_prompt_history (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id) ON DELETE CASCADE,
                    prompt TEXT NOT NULL,
                    fields_generated INTEGER DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE api_keys (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    key_hash TEXT NOT NULL,
                    key_prefix TEXT NOT NULL,
                    name TEXT NOT NULL,
                    permissions TEXT NOT NULL DEFAULT '{"read_forms": true, "write_forms": true, "read_submissions": true, "delete_forms": false}',
                    last_used_at TIMESTAMP,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE campaign_ab_variants (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    campaign_id INTEGER NOT NULL REFERENCES campaigns(id),
                    variant_label TEXT NOT NULL,
                    subject TEXT NOT NULL,
                    body TEXT,
                    split_percent INTEGER NOT NULL DEFAULT 50,
                    sent_count INTEGER DEFAULT 0,
                    opened_count INTEGER DEFAULT 0,
                    clicked_count INTEGER DEFAULT 0,
                    response_count INTEGER DEFAULT 0,
                    is_winner BOOLEAN DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE campaign_recipients (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    campaign_id INTEGER NOT NULL REFERENCES campaigns(id),
                    email TEXT NOT NULL,
                    status TEXT DEFAULT 'pending',
                    message_id TEXT,
                    error TEXT,
                    sent_at TIMESTAMP,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                , tracking_token TEXT, opened_at TIMESTAMP, clicked_at TIMESTAMP, bounce_type TEXT, bounce_reason TEXT, variant_id INTEGER REFERENCES campaign_ab_variants(id), reminder_sent INTEGER DEFAULT 0)""")
    op.execute("""CREATE TABLE campaign_reminders (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    campaign_id INTEGER NOT NULL REFERENCES campaigns(id),
                    delay_hours INTEGER NOT NULL,
                    subject TEXT NOT NULL,
                    body TEXT NOT NULL,
                    template_id TEXT,
                    status TEXT DEFAULT 'scheduled' CHECK(status IN ('scheduled', 'sent', 'cancelled')),
                    scheduled_at TIMESTAMP,
                    sent_at TIMESTAMP,
                    recipient_count INTEGER DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE campaigns (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    name TEXT NOT NULL,
                    subject TEXT NOT NULL,
                    body TEXT NOT NULL,
                    status TEXT DEFAULT 'draft',
                    from_name TEXT,
                    from_email TEXT,
                    template_id TEXT,
                    scheduled_at TIMESTAMP,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                , ab_test_metric TEXT DEFAULT 'opens', ab_test_size INTEGER DEFAULT 20, ab_declare_after_hours INTEGER DEFAULT 24, ab_declared INTEGER DEFAULT 0, ab_winner_variant_id INTEGER REFERENCES campaign_ab_variants(id))""")
    op.execute("""CREATE TABLE custom_templates (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    name TEXT NOT NULL,
                    content TEXT NOT NULL,
                    description TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE document_templates (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER REFERENCES sites(id),
                    name TEXT NOT NULL,
                    document_type TEXT NOT NULL DEFAULT 'invoice',
                    layout TEXT NOT NULL DEFAULT 'classic',
                    field_mapping TEXT NOT NULL DEFAULT '{}',
                    style_config TEXT NOT NULL DEFAULT '{}',
                    is_active INTEGER DEFAULT 1,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                , auto_generate INTEGER DEFAULT 0, invoice_number_format TEXT DEFAULT 'INV-YYYY-####', next_invoice_number TEXT DEFAULT 'INV-2026-0001', user_id INTEGER REFERENCES users(id))""")
    op.execute("""CREATE TABLE documents (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    document_id TEXT NOT NULL,
    site_id INTEGER REFERENCES sites(id),
    submission_id INTEGER REFERENCES submissions(id),
    template_id INTEGER REFERENCES document_templates(id),
    document_type TEXT NOT NULL DEFAULT 'invoice',
    status TEXT NOT NULL DEFAULT 'draft',
    data JSON,
    pdf_path TEXT,
    download_count INTEGER DEFAULT 0,
    expires_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    due_date TIMESTAMP,
    invoice_number TEXT,
    customer_email TEXT,
    stripe_payment_intent TEXT,
    stripe_payment_status TEXT DEFAULT 'none',
    user_id INTEGER REFERENCES users(id)
)""")
    op.execute("""CREATE TABLE email_send_log (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    sent_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
                    count INTEGER DEFAULT 1,
                    campaign_id INTEGER REFERENCES campaigns(id)
                )""")
    op.execute("""CREATE TABLE email_settings (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    from_name TEXT,
                    from_email TEXT,
                    reply_to TEXT,
                    template_id TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                , sender_domain TEXT, domain_verified BOOLEAN DEFAULT 0, default_template TEXT DEFAULT 'default', max_sends_per_hour INTEGER DEFAULT 1000, max_sends_per_day INTEGER DEFAULT 10000, max_burst_per_campaign INTEGER DEFAULT 100, sends_this_hour INTEGER DEFAULT 0, sends_this_day INTEGER DEFAULT 0, hour_reset_at TIMESTAMP, day_reset_at TIMESTAMP)""")
    op.execute("""CREATE TABLE form_actions (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id),
                    type TEXT NOT NULL,
                    config TEXT NOT NULL DEFAULT '{}',
                    trigger_event TEXT DEFAULT 'submission',
                    enabled INTEGER DEFAULT 1,
                    execution_order INTEGER DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE form_impressions (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id),
                    ip_hash TEXT NOT NULL,
                    user_agent TEXT,
                    geo_city TEXT,
                    geo_country TEXT,
                    device_type TEXT,
                    browser TEXT,
                    os TEXT,
                    viewed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE form_sessions (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id),
                    session_id TEXT NOT NULL,
                    visitor_id TEXT NOT NULL,
                    ip_hash TEXT,
                    user_agent TEXT,
                    referrer TEXT,
                    event TEXT NOT NULL DEFAULT 'session_start',
                    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    completed_at TIMESTAMP,
                    fields_viewed TEXT,
                    last_field_reached TEXT,
                    step_reached INTEGER DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE form_templates (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    slug TEXT UNIQUE NOT NULL,
                    name TEXT NOT NULL,
                    description TEXT NOT NULL,
                    category TEXT NOT NULL,
                    field_config TEXT NOT NULL,
                    success_message TEXT,
                    created_by TEXT DEFAULT 'system',
                    is_featured INTEGER DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE form_variants (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id) ON DELETE CASCADE,
                    variant_key TEXT NOT NULL CHECK(variant_key IN ('A', 'B')),
                    name TEXT,
                    field_config TEXT NOT NULL,
                    success_message TEXT,
                    weight INTEGER DEFAULT 50,
                    active INTEGER DEFAULT 1,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE form_versions (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id) ON DELETE CASCADE,
                    version INTEGER NOT NULL,
                    field_config TEXT NOT NULL,
                    metadata TEXT,
                    changed_by TEXT NOT NULL DEFAULT 'system',
                    changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    change_reason TEXT
                )""")
    op.execute("""CREATE TABLE "invoice_schedules" (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                site_id INTEGER,
                template_id INTEGER NOT NULL,
                customer_name_encrypted TEXT NOT NULL,
                customer_email_encrypted TEXT NOT NULL,
                line_items TEXT NOT NULL DEFAULT '[]',
                document_number_prefix TEXT NOT NULL DEFAULT 'INV',
                interval TEXT NOT NULL DEFAULT 'monthly',
                interval_count INTEGER NOT NULL DEFAULT 1,
                start_date DATE NOT NULL,
                next_run DATE,
                last_run DATE,
                status TEXT NOT NULL DEFAULT 'active',
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            , user_id INTEGER REFERENCES users(id))""")
    op.execute("""CREATE TABLE monthly_usage (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    month TEXT NOT NULL,
                    submission_count INTEGER DEFAULT 0,
                    UNIQUE(user_id, month)
                )""")
    op.execute("""CREATE TABLE oauth_accounts (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    provider TEXT NOT NULL,
                    provider_user_id TEXT NOT NULL,
                    provider_email TEXT,
                    provider_name TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    UNIQUE(provider, provider_user_id)
                )""")
    op.execute("""CREATE TABLE referral_codes (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL REFERENCES users(id),
                    code TEXT UNIQUE NOT NULL,
                    uses INTEGER DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE referral_events (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    code TEXT NOT NULL REFERENCES referral_codes(code),
                    referrer_id INTEGER REFERENCES users(id),
                    referee_id INTEGER REFERENCES users(id),
                    reward_credits INTEGER DEFAULT 0,
                    event_type TEXT DEFAULT 'signup',
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE site_analytics (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    site_id INTEGER NOT NULL REFERENCES sites(id),
                    day TEXT NOT NULL,
                    submission_count INTEGER DEFAULT 0,
                    spam_count INTEGER DEFAULT 0,
                    UNIQUE(site_id, day)
                )""")
    op.execute("""CREATE TABLE sites (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            token TEXT UNIQUE NOT NULL,
            name TEXT NOT NULL,
            owner_email TEXT NOT NULL,
            smtp_from TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        , user_id INTEGER REFERENCES users(id), field_config TEXT, webhook_url TEXT, webhook_enabled INTEGER DEFAULT 0, webhook_events TEXT DEFAULT 'submission', honeypot_enabled INTEGER DEFAULT 1, spam_filter_enabled INTEGER DEFAULT 1, recaptcha_enabled INTEGER DEFAULT 0, recaptcha_threshold REAL DEFAULT 0.5, rate_limit_enabled INTEGER DEFAULT 1, rate_limit_burst INTEGER DEFAULT 20, rate_limit_refill REAL DEFAULT 0.5, created_by TEXT DEFAULT 'manual', metadata TEXT, team_id INTEGER REFERENCES teams(id), ai_builder_prompt TEXT, og_title TEXT, og_description TEXT, og_image TEXT, branding_color TEXT, logo_url TEXT, favicon_url TEXT, custom_domain TEXT)""")
    op.execute("""CREATE TABLE submissions (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            site_id INTEGER NOT NULL REFERENCES sites(id),
            customer_name TEXT,
            customer_phone TEXT,
            customer_email TEXT,
            customer_equipment TEXT,
            customer_message TEXT,
            submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        , data TEXT, client_ip TEXT, user_agent TEXT, spam_flag INTEGER DEFAULT 0, geo_city TEXT, geo_country TEXT, device_type TEXT, browser TEXT, os TEXT)""")
    op.execute("""CREATE TABLE "team_members" (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                team_id INTEGER NOT NULL,
                user_id INTEGER,
                role TEXT NOT NULL DEFAULT 'member',
                invited_email_encrypted TEXT,
                invited_email_hash TEXT,
                status TEXT NOT NULL DEFAULT 'pending',
                invite_token TEXT UNIQUE,
                joined_at TIMESTAMP,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            , expires_at TIMESTAMP)""")
    op.execute("""CREATE TABLE teams (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    owner_id INTEGER NOT NULL REFERENCES users(id),
                    tier TEXT DEFAULT 'teams',
                    stripe_customer_id TEXT,
                    stripe_subscription_id TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )""")
    op.execute("""CREATE TABLE test_table (id INTEGER PRIMARY KEY)""")
    op.execute("""CREATE TABLE usage_alerts (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER NOT NULL,
                    last_alerted_at TEXT NOT NULL,
                    last_alert_pct REAL NOT NULL,
                    FOREIGN KEY (user_id) REFERENCES users(id)
                )""")
    op.execute("""CREATE TABLE "users" (
                id INTEGER PRIMARY KEY,
                email_hash TEXT UNIQUE NOT NULL,
                email_encrypted TEXT,
                password_hash TEXT NOT NULL,
                name_encrypted TEXT,
                display_name_encrypted TEXT,
                tier TEXT DEFAULT 'free',
                stripe_customer_id TEXT,
                stripe_subscription_id TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                email_verified INTEGER DEFAULT 0,
                email_verification_token TEXT,
                email_verification_sent_at TIMESTAMP,
                pending_email TEXT,
                password_reset_token TEXT,
                password_reset_sent_at TIMESTAMP,
                magic_login_token TEXT,
                magic_login_sent_at TIMESTAMP,
                magic_login_enabled INTEGER DEFAULT 1
            , is_admin INTEGER DEFAULT 0, referred_by TEXT)""")
    op.execute("""CREATE TABLE waitlist (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            email TEXT UNIQUE NOT NULL,
            source TEXT DEFAULT 'landing',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )""")
    op.execute("""CREATE TABLE "webhook_destinations" (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                site_id INTEGER NOT NULL,
                name TEXT NOT NULL DEFAULT 'Integration',
                type TEXT NOT NULL,
                url_encrypted TEXT,
                url_hash TEXT,
                config TEXT,
                enabled INTEGER DEFAULT 1,
                last_status TEXT,
                last_error TEXT,
                last_delivered_at TIMESTAMP,
                success_count INTEGER DEFAULT 0,
                failure_count INTEGER DEFAULT 0,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )""")
    op.execute("""CREATE TABLE "webhook_logs" (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                site_id INTEGER NOT NULL,
                submission_id INTEGER NOT NULL,
                webhook_url_encrypted TEXT NOT NULL,
                attempt_count INTEGER DEFAULT 0,
                status TEXT DEFAULT 'pending',
                last_error TEXT,
                next_retry_at TIMESTAMP,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                destination_id INTEGER,
                retrying_by TEXT
            )""")
    op.execute("""CREATE INDEX idx_ab_variants_campaign ON campaign_ab_variants(campaign_id)""")
    op.execute("""CREATE INDEX idx_api_keys_hash ON api_keys(key_hash)""")
    op.execute("""CREATE INDEX idx_api_keys_user ON api_keys(user_id)""")
    op.execute("""CREATE INDEX idx_campaign_recipients_campaign ON campaign_recipients(campaign_id)""")
    op.execute("""CREATE INDEX idx_campaign_recipients_tracking_token ON campaign_recipients(tracking_token)""")
    op.execute("""CREATE UNIQUE INDEX idx_custom_templates_user_name ON custom_templates(user_id, name)""")
    op.execute("""CREATE INDEX idx_destinations_site ON webhook_destinations(site_id)""")
    op.execute("""CREATE INDEX idx_form_actions_site ON form_actions(site_id, execution_order)""")
    op.execute("""CREATE INDEX idx_form_impressions_date ON form_impressions(site_id, viewed_at)""")
    op.execute("""CREATE INDEX idx_form_impressions_site ON form_impressions(site_id)""")
    op.execute("""CREATE INDEX idx_form_sessions_site ON form_sessions(site_id, started_at)""")
    op.execute("""CREATE INDEX idx_form_sessions_visitor ON form_sessions(site_id, visitor_id)""")
    op.execute("""CREATE INDEX idx_form_templates_category ON form_templates(category)""")
    op.execute("""CREATE INDEX idx_form_templates_featured ON form_templates(is_featured DESC)""")
    op.execute("""CREATE INDEX idx_form_variants_site ON form_variants(site_id, active)""")
    op.execute("""CREATE INDEX idx_form_versions_site_version ON form_versions(site_id, version DESC)""")
    op.execute("""CREATE INDEX idx_invoice_schedules_next_run ON invoice_schedules(next_run)""")
    op.execute("""CREATE INDEX idx_invoice_schedules_site ON invoice_schedules(site_id)""")
    op.execute("""CREATE INDEX idx_monthly_usage_user_month ON monthly_usage(user_id, month)""")
    op.execute("""CREATE INDEX idx_oauth_accounts_user_id ON oauth_accounts(user_id)""")
    op.execute("""CREATE INDEX idx_prompt_history_site ON ai_prompt_history(site_id)""")
    op.execute("""CREATE INDEX idx_referral_codes_code ON referral_codes(code)""")
    op.execute("""CREATE INDEX idx_referral_codes_user ON referral_codes(user_id)""")
    op.execute("""CREATE INDEX idx_referral_events_referrer ON referral_events(referrer_id)""")
    op.execute("""CREATE INDEX idx_reminders_campaign ON campaign_reminders(campaign_id, status)""")
    op.execute("""CREATE INDEX idx_reminders_scheduled ON campaign_reminders(scheduled_at, status)""")
    op.execute("""CREATE INDEX idx_send_log_user_time ON email_send_log(user_id, sent_at)""")
    op.execute("""CREATE INDEX idx_site_analytics_site_day ON site_analytics(site_id, day)""")
    op.execute("""CREATE INDEX idx_sites_user_id ON sites(user_id)""")
    op.execute("""CREATE INDEX idx_submissions_created ON submissions(submitted_at DESC)""")
    op.execute("""CREATE INDEX idx_submissions_site_created ON submissions(site_id, submitted_at DESC)""")
    op.execute("""CREATE INDEX idx_submissions_site_id ON submissions(site_id)""")
    op.execute("""CREATE INDEX idx_team_members_team ON team_members(team_id)""")
    op.execute("""CREATE INDEX idx_team_members_token ON team_members(invite_token)""")
    op.execute("""CREATE INDEX idx_team_members_user ON team_members(user_id)""")
    op.execute("""CREATE INDEX idx_teams_owner ON teams(owner_id)""")
    op.execute("""CREATE INDEX idx_usage_alerts_user_id ON usage_alerts(user_id)""")
    op.execute("""CREATE INDEX idx_webhook_logs_destination ON webhook_logs(destination_id)""")
    op.execute("""CREATE INDEX idx_webhook_logs_retry ON webhook_logs(next_retry_at, status)""")


def downgrade():
    op.drop_index("idx_webhook_logs_retry")
    op.drop_index("idx_webhook_logs_destination")
    op.drop_index("idx_usage_alerts_user_id")
    op.drop_index("idx_teams_owner")
    op.drop_index("idx_team_members_user")
    op.drop_index("idx_team_members_token")
    op.drop_index("idx_team_members_team")
    op.drop_index("idx_submissions_site_id")
    op.drop_index("idx_submissions_site_created")
    op.drop_index("idx_submissions_created")
    op.drop_index("idx_sites_user_id")
    op.drop_index("idx_site_analytics_site_day")
    op.drop_index("idx_send_log_user_time")
    op.drop_index("idx_reminders_scheduled")
    op.drop_index("idx_reminders_campaign")
    op.drop_index("idx_referral_events_referrer")
    op.drop_index("idx_referral_codes_user")
    op.drop_index("idx_referral_codes_code")
    op.drop_index("idx_prompt_history_site")
    op.drop_index("idx_oauth_accounts_user_id")
    op.drop_index("idx_monthly_usage_user_month")
    op.drop_index("idx_invoice_schedules_site")
    op.drop_index("idx_invoice_schedules_next_run")
    op.drop_index("idx_form_versions_site_version")
    op.drop_index("idx_form_variants_site")
    op.drop_index("idx_form_templates_featured")
    op.drop_index("idx_form_templates_category")
    op.drop_index("idx_form_sessions_visitor")
    op.drop_index("idx_form_sessions_site")
    op.drop_index("idx_form_impressions_site")
    op.drop_index("idx_form_impressions_date")
    op.drop_index("idx_form_actions_site")
    op.drop_index("idx_destinations_site")
    op.drop_index("idx_custom_templates_user_name")
    op.drop_index("idx_campaign_recipients_tracking_token")
    op.drop_index("idx_campaign_recipients_campaign")
    op.drop_index("idx_api_keys_user")
    op.drop_index("idx_api_keys_hash")
    op.drop_index("idx_ab_variants_campaign")
    op.drop_table("webhook_logs")
    op.drop_table("webhook_destinations")
    op.drop_table("waitlist")
    op.drop_table("users")
    op.drop_table("usage_alerts")
    op.drop_table("test_table")
    op.drop_table("teams")
    op.drop_table("team_members")
    op.drop_table("submissions")
    op.drop_table("sites")
    op.drop_table("site_analytics")
    op.drop_table("referral_events")
    op.drop_table("referral_codes")
    op.drop_table("oauth_accounts")
    op.drop_table("monthly_usage")
    op.drop_table("invoice_schedules")
    op.drop_table("form_versions")
    op.drop_table("form_variants")
    op.drop_table("form_templates")
    op.drop_table("form_sessions")
    op.drop_table("form_impressions")
    op.drop_table("form_actions")
    op.drop_table("email_settings")
    op.drop_table("email_send_log")
    op.drop_table("documents")
    op.drop_table("document_templates")
    op.drop_table("custom_templates")
    op.drop_table("campaigns")
    op.drop_table("campaign_reminders")
    op.drop_table("campaign_recipients")
    op.drop_table("campaign_ab_variants")
    op.drop_table("api_keys")
    op.drop_table("ai_prompt_history")