"""Mandate origin: replace free-text with FK to duniter_identities.

Revision ID: e3f4a5b6c7d8
Revises: d91a3c7f8b02
Create Date: 2026-04-25 10:00:00.000000
"""

from __future__ import annotations

from alembic import op

revision = "e3f4a5b6c7d8"
down_revision = "d91a3c7f8b02"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute("ALTER TABLE mandates DROP COLUMN IF EXISTS origin")
    op.execute("ALTER TABLE mandates ADD COLUMN IF NOT EXISTS origin_id UUID REFERENCES duniter_identities(id)")
    op.execute("CREATE INDEX IF NOT EXISTS ix_mandates_origin_id ON mandates (origin_id)")


def downgrade() -> None:
    op.execute("DROP INDEX IF EXISTS ix_mandates_origin_id")
    op.execute("ALTER TABLE mandates DROP COLUMN IF EXISTS origin_id")
    op.execute("ALTER TABLE mandates ADD COLUMN IF NOT EXISTS origin TEXT")
