"""sales: add plot_id/plot_price to proposals

Revision ID: 0075
Revises: 0074
Create Date: 2026-08-24

Backs the new "Plot Selection" card on the New Proposal page — a proposal
can now reference the specific vacant plot it was quoted against, with
plot_price snapshotting the plot's effective price (price_override, or the
plot type's default_price) at the moment the proposal was created, so the
quoted price stays stable even if the plot's price is edited afterwards.

Mirrors the existing Contract.plot_id / Opportunity.plot_id FK pattern
(ON DELETE SET NULL — a deleted plot never cascades into deleting sales
history).
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision = "0075"
down_revision = "0074"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        "proposals",
        sa.Column(
            "plot_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("plots.id", ondelete="SET NULL"),
            nullable=True,
        ),
    )
    op.add_column(
        "proposals",
        sa.Column("plot_price", sa.Numeric(12, 2), nullable=True),
    )
    op.create_index("ix_proposals_plot_id", "proposals", ["plot_id"])


def downgrade() -> None:
    op.drop_index("ix_proposals_plot_id", table_name="proposals")
    op.drop_column("proposals", "plot_price")
    op.drop_column("proposals", "plot_id")
