from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


def clear_legacy_bookmarks(apps, schema_editor):
    Bookmark = apps.get_model("annotations", "Bookmark")
    Bookmark.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ("books", "0003_readingprogress_epub_location"),
        ("annotations", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.RunPython(clear_legacy_bookmarks, migrations.RunPython.noop),
        migrations.RemoveConstraint(
            model_name="bookmark",
            name="uq_bookmark_user_book_page",
        ),
        migrations.RemoveField(
            model_name="bookmark",
            name="book",
        ),
        migrations.AddField(
            model_name="bookmark",
            name="ebook",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="bookmarks",
                to="books.ebook",
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="bookmark",
            name="epub_cfi",
            field=models.CharField(db_index=True, default="", max_length=2048),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="bookmark",
            name="chapter_index",
            field=models.PositiveIntegerField(db_index=True, default=0),
        ),
        migrations.AddField(
            model_name="bookmark",
            name="chapter_title",
            field=models.CharField(blank=True, default="", max_length=512),
        ),
        migrations.AddField(
            model_name="bookmark",
            name="content",
            field=models.TextField(blank=True, default=""),
        ),
        migrations.AlterField(
            model_name="bookmark",
            name="page",
            field=models.PositiveIntegerField(
                default=1,
                help_text="Legacy/display page; derived from chapter_index + 1",
            ),
        ),
        migrations.AlterModelOptions(
            name="bookmark",
            options={
                "ordering": ["chapter_index", "epub_cfi"],
                "verbose_name": "Bookmark",
                "verbose_name_plural": "Bookmarks",
            },
        ),
        # ebook was added nullable for SQLite; enforce NOT NULL via AlterField
        migrations.AlterField(
            model_name="bookmark",
            name="ebook",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="bookmarks",
                to="books.ebook",
            ),
        ),
        migrations.AddConstraint(
            model_name="bookmark",
            constraint=models.UniqueConstraint(
                fields=("user", "ebook", "epub_cfi"),
                name="uq_bookmark_user_ebook_cfi",
            ),
        ),
    ]
