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


def migrate_old_wards(apps, schema_editor):
    City = apps.get_model("api", "City")
    Ward = apps.get_model("api", "Ward")
    for ward in Ward.objects.all().iterator():
        city, _ = City.objects.get_or_create(name=ward.city)
        ward.city_new = city
        ward.save(update_fields=["city_new"])


class Migration(migrations.Migration):
    dependencies = [("api", "0001_initial")]

    operations = [
        migrations.CreateModel(
            name="APIKey",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=150)),
                ("key_prefix", models.CharField(max_length=12, unique=True)),
                ("key_hash", models.CharField(max_length=64, unique=True)),
                ("plan", models.CharField(choices=[("standard", "Standard"), ("enterprise", "Enterprise")], default="standard", max_length=20)),
                ("is_active", models.BooleanField(default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("last_used_at", models.DateTimeField(blank=True, null=True)),
            ],
            options={"db_table": "api_keys"},
        ),
        migrations.CreateModel(
            name="City",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=100, unique=True)),
                ("region", models.CharField(blank=True, default="", max_length=150)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
            ],
            options={"db_table": "cities", "ordering": ["name"]},
        ),
        migrations.CreateModel(
            name="Tag",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("name", models.CharField(max_length=100, unique=True)),
            ],
            options={"db_table": "tags", "ordering": ["name"]},
        ),
        migrations.CreateModel(
            name="Engagement",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("engagement_type", models.CharField(choices=[("citizen", "Citizen"), ("stakeholder", "Stakeholder")], default="citizen", max_length=20)),
                ("occurred_at", models.DateTimeField()),
                ("city", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="engagements", to="api.city")),
                ("spot", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="engagements", to="api.spot")),
            ],
            options={"db_table": "engagements"},
        ),
        migrations.CreateModel(
            name="SpotImage",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("image_type", models.CharField(choices=[("reality", "Reality"), ("potential", "Potential"), ("transformation", "Transformation")], max_length=20)),
                ("url", models.URLField(max_length=1000)),
                ("spot", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="images", to="api.spot")),
            ],
            options={"db_table": "spot_images"},
        ),
        migrations.AddField(
            model_name="ward",
            name="city_new",
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name="wards_new", to="api.city"),
        ),
        migrations.RunPython(migrate_old_wards, migrations.RunPython.noop),
        migrations.RemoveField(model_name="ward", name="city"),
        migrations.RenameField(model_name="ward", old_name="city_new", new_name="city"),
        migrations.AlterField(model_name="ward", name="city", field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="wards", to="api.city")),
        migrations.AddField(model_name="spot", name="kind", field=models.CharField(choices=[("spot", "Shahar Spot"), ("anchor", "Anchor Spot")], default="spot", max_length=20)),
        migrations.AddField(model_name="spot", name="spot_name", field=models.CharField(blank=True, default="", max_length=255)),
        migrations.AddField(model_name="spot", name="address", field=models.CharField(blank=True, default="", max_length=500)),
        migrations.AddField(model_name="spot", name="score", field=models.DecimalField(decimal_places=2, default=0, max_digits=5)),
        migrations.AddField(model_name="spot", name="trend", field=models.DecimalField(decimal_places=2, default=0, max_digits=7)),
        migrations.AddField(model_name="spot", name="reported_date", field=models.DateField(blank=True, null=True)),
        migrations.AddField(model_name="spot", name="created_at", field=models.DateTimeField(auto_now_add=True)),
        migrations.AddField(model_name="spot", name="transformation_at", field=models.DateTimeField(blank=True, null=True)),
        migrations.AddField(model_name="spot", name="shortlisted", field=models.BooleanField(default=False)),
        migrations.AddField(model_name="spot", name="tags", field=models.ManyToManyField(blank=True, related_name="spots", to="api.tag")),
        migrations.AlterField(model_name="spot", name="category", field=models.CharField(blank=True, choices=[("garbage", "Garbage"), ("potholes", "Potholes"), ("footpath", "Footpath")], max_length=30, null=True)),
        migrations.AddIndex(model_name="ward", index=models.Index(fields=["city", "rank"], name="idx_ward_city_rank")),
        migrations.AddConstraint(model_name="ward", constraint=models.UniqueConstraint(fields=["city", "ward"], name="uq_ward_city_ward")),
        migrations.AddIndex(model_name="spot", index=models.Index(fields=["ward", "spot"], name="idx_spot_ward_number")),
        migrations.AddIndex(model_name="spot", index=models.Index(fields=["kind", "shortlisted"], name="idx_spot_kind_shortlist")),
        migrations.AddIndex(model_name="spot", index=models.Index(fields=["created_at"], name="idx_spot_created")),
        migrations.AddIndex(model_name="spot", index=models.Index(fields=["status"], name="idx_spot_status")),
        migrations.AddIndex(model_name="engagement", index=models.Index(fields=["city", "occurred_at"], name="idx_engagement_city_date")),
        migrations.AddConstraint(model_name="spotimage", constraint=models.UniqueConstraint(fields=["spot", "image_type"], name="uq_spot_image_type")),
    ]
