"""remove_vendor_owner_user_id_column

Revision ID: 9f3a25ea4991
Revises: 5818330181a5
Create Date: 2025-12-02 17:58:45.663338

This migration removes the owner_user_id column from the vendors table.

Architecture Change:
- OLD: Each vendor had its own owner (vendor.owner_user_id)
- NEW: Vendors belong to a company, company has one owner (company.owner_user_id)

The vendor ownership is now determined via the company relationship:
- vendor.company.owner_user_id contains the owner
"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "9f3a25ea4991"
down_revision: str | None = "5818330181a5"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """
    Remove owner_user_id column from vendors table.

    Ownership is now determined via the company relationship.

    Note: SQLite batch mode recreates the table without the column,
    so we don't need to explicitly drop constraints.
    """
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        # Drop the column - batch mode handles constraints automatically
        batch_op.drop_column("owner_user_id")


def downgrade() -> None:
    """
    Re-add owner_user_id column to vendors table.

    WARNING: This will add the column back but NOT restore the data.
    You will need to manually populate owner_user_id from company.owner_user_id
    if reverting this migration.
    """
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("owner_user_id", sa.Integer(), nullable=True)
        )
        batch_op.create_foreign_key(
            "vendors_owner_user_id_fkey",
            "users",
            ["owner_user_id"],
            ["id"]
        )
