from django.db import migrations

def migrate_screenshots(apps, schema_editor):
    Page = apps.get_model('links', 'Page')
    Screenshot = apps.get_model('links', 'Screenshot')

    for page in Page.objects.all():
        if page.screenshot_path:
            Screenshot.objects.create(
                page=page,
                path=page.screenshot_path,
                status='completed'
            )

def reverse_migrate(apps, schema_editor):
    Page = apps.get_model('links', 'Page')
    Screenshot = apps.get_model('links', 'Screenshot')

    for screenshot in Screenshot.objects.filter(status='completed'):
        screenshot.page.screenshot_path = screenshot.path
        screenshot.page.save()

class Migration(migrations.Migration):

    dependencies = [
        ('links', '0015_create_screenshot_model'),
    ]

    operations = [
        migrations.RunPython(migrate_screenshots, reverse_migrate),
    ]
