"""make_vendor_owner_user_id_nullable_for_company_ownership

Revision ID: 5818330181a5
Revises: d0325d7c0f25
Create Date: 2025-12-01 20:30:06.158027

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    """
    Make vendor.owner_user_id nullable to support company-level ownership.

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

    This allows one company owner to manage multiple vendor brands.
    """
    # Use batch operations for SQLite compatibility
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        batch_op.alter_column("owner_user_id",
                              existing_type=sa.INTEGER(),
                              nullable=True)


def downgrade() -> None:
    """
    Revert vendor.owner_user_id to non-nullable.

    WARNING: This will fail if there are vendors without owner_user_id!
    """
    # Use batch operations for SQLite compatibility
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        batch_op.alter_column("owner_user_id",
                              existing_type=sa.INTEGER(),
                              nullable=False)
