"""manual invitation send

Revision ID: d5e8b31047af
Revises: c4a1e7f92b18
Create Date: 2026-08-15 23:10:00.000000

Lets one message job carry its own hand-edited content, and gives the wedding a reply
address (add-guest-invitation-send D3, D5, D9).

Purely additive, and every row that already exists reads correctly under the new code
without being touched:

* `subject` and `body_text` null mean "render from `template_id` at send time" — which is
  what every scheduled, bulk and direct job has always done, so nothing is backfilled.
* `override_quiet_hours` false means "defer past quiet hours", which is FR-6.9 unchanged.
  The server default matters as much as the Python one: a job inserted by the old API
  against the new schema must still defer.
* `sent_by_admin_id` null means "not a manual send". `ON DELETE SET NULL` rather than
  CASCADE, because deactivating an admin must not delete the record that a guest was
  emailed.
* `wedding.host_email` null means no reply address, which is what every send did before.

So the deploy window where the old API runs against the new schema is safe in both
directions, and this can go out ahead of the application.

The downgrade drops the stored copy of any manual message — the jobs themselves survive
with their statuses, provider ids and delivery receipts intact, so the message log stays
truthful about what was sent and when; only the body text is lost. It also drops the reply
address, after which replies go to the platform sender again.
"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "d5e8b31047af"
down_revision: str | None = "c4a1e7f92b18"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.add_column("message_job", sa.Column("subject", sa.Text(), nullable=True))
    op.add_column("message_job", sa.Column("body_text", sa.Text(), nullable=True))
    op.add_column(
        "message_job",
        sa.Column(
            "override_quiet_hours",
            sa.Boolean(),
            nullable=False,
            server_default=sa.false(),
        ),
    )
    op.add_column("message_job", sa.Column("sent_by_admin_id", sa.Uuid(), nullable=True))
    op.create_foreign_key(
        "fk_message_job_sent_by_admin",
        "message_job",
        "admin_user",
        ["sent_by_admin_id"],
        ["id"],
        ondelete="SET NULL",
    )

    op.add_column("wedding", sa.Column("host_email", sa.String(length=320), nullable=True))


def downgrade() -> None:
    op.drop_column("wedding", "host_email")

    op.drop_constraint("fk_message_job_sent_by_admin", "message_job", type_="foreignkey")
    op.drop_column("message_job", "sent_by_admin_id")
    op.drop_column("message_job", "override_quiet_hours")
    op.drop_column("message_job", "body_text")
    op.drop_column("message_job", "subject")
