"""Phase 1 — Create GuestStay table with idswh nullable.

No uniqueness constraint yet. No backfill yet.
Safe to deploy to production with zero downtime.
"""
from __future__ import annotations

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


class Migration(migrations.Migration):

    dependencies = [
        ("bookings", "0016_booking_checkin_status"),
        ("guests", "0009_guest_istat_fields"),
    ]

    operations = [
        migrations.CreateModel(
            name="GuestStay",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "booking",
                    models.ForeignKey(
                        help_text="The booking this stay belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="guest_stays",
                        to="bookings.booking",
                    ),
                ),
                (
                    "guest",
                    models.ForeignKey(
                        help_text="The guest this stay belongs to.",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="stays",
                        to="guests.guest",
                    ),
                ),
                (
                    "check_in_date",
                    models.DateField(
                        help_text="Check-in date copied from the booking at creation time.",
                    ),
                ),
                (
                    "check_out_date",
                    models.DateField(
                        help_text="Check-out date copied from the booking at creation time.",
                    ),
                ),
                # Phase 1: nullable, no unique constraint yet.
                # Phase 2 backfills existing rows.
                # Phase 3 enforces unique=True.
                (
                    "idswh",
                    models.CharField(
                        db_index=True,
                        editable=False,
                        help_text=(
                            "Permanent, globally unique regulatory identity token. "
                            "Generated once at creation. Never recomputed, never overwritten."
                        ),
                        max_length=64,
                        null=True,
                        unique=False,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "db_table": "guest_stays",
            },
        ),
        migrations.AddIndex(
            model_name="gueststay",
            index=models.Index(
                fields=["booking", "guest"],
                name="gueststay_booking_guest_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="gueststay",
            index=models.Index(
                fields=["check_in_date", "check_out_date"],
                name="gueststay_dates_idx",
            ),
        ),
    ]
