# Generated by Django 5.1.13 on 2025-11-05 10:14

from django.db import migrations


def remove_webpage_content_column(apps, schema_editor):
    """Remove webpage_content column if it exists"""
    with schema_editor.connection.cursor() as cursor:
        # Check if column exists
        cursor.execute("PRAGMA table_info(links_page)")
        columns = [row[1] for row in cursor.fetchall()]
        
        if 'webpage_content' in columns:
            # SQLite doesn't support DROP COLUMN directly, need to recreate table
            cursor.execute("""
                CREATE TABLE links_page_new (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    url varchar(2000) NOT NULL,
                    title varchar(200) NOT NULL,
                    summary TEXT NOT NULL,
                    content TEXT NOT NULL,
                    created_at datetime NOT NULL,
                    updated_at datetime NOT NULL,
                    error_message TEXT NOT NULL,
                    last_retry_at datetime,
                    process_status varchar(20) NOT NULL,
                    retry_count INTEGER NOT NULL,
                    screenshot_error TEXT,
                    screenshot_last_attempt datetime,
                    screenshot_path varchar(255) NOT NULL
                )
            """)
            
            # Copy data
            cursor.execute("""
                INSERT INTO links_page_new 
                SELECT id, url, title, summary, content, created_at, updated_at,
                       error_message, last_retry_at, process_status, retry_count,
                       screenshot_error, screenshot_last_attempt, screenshot_path
                FROM links_page
            """)
            
            # Drop old table
            cursor.execute("DROP TABLE links_page")
            
            # Rename new table
            cursor.execute("ALTER TABLE links_page_new RENAME TO links_page")


class Migration(migrations.Migration):

    dependencies = [
        ('links', '0034_post_summary'),
    ]

    operations = [
        migrations.RunPython(remove_webpage_content_column, migrations.RunPython.noop),
    ]
