"""add_companies_table_and_restructure_vendors

Revision ID: d0325d7c0f25
Revises: 0bd9ffaaced1
Create Date: 2025-11-30 14:58:17.165142

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    # Create companies table
    op.create_table(
        "companies",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("owner_user_id", sa.Integer(), nullable=False),
        sa.Column("contact_email", sa.String(), nullable=False),
        sa.Column("contact_phone", sa.String(), nullable=True),
        sa.Column("website", sa.String(), nullable=True),
        sa.Column("business_address", sa.Text(), nullable=True),
        sa.Column("tax_number", sa.String(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("is_verified", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(), nullable=False, server_default=sa.func.now(), onupdate=sa.func.now()),
        sa.ForeignKeyConstraint(["owner_user_id"], ["users.id"], ),
        sa.PrimaryKeyConstraint("id")
    )
    op.create_index(op.f("ix_companies_id"), "companies", ["id"], unique=False)
    op.create_index(op.f("ix_companies_name"), "companies", ["name"], unique=False)

    # Use batch mode for SQLite to modify vendors table
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        # Add company_id column
        batch_op.add_column(sa.Column("company_id", sa.Integer(), nullable=True))
        batch_op.create_index(batch_op.f("ix_vendors_company_id"), ["company_id"], unique=False)
        batch_op.create_foreign_key("fk_vendors_company_id", "companies", ["company_id"], ["id"])

        # Remove old contact fields
        batch_op.drop_column("contact_email")
        batch_op.drop_column("contact_phone")
        batch_op.drop_column("website")
        batch_op.drop_column("business_address")
        batch_op.drop_column("tax_number")


def downgrade() -> None:
    # Use batch mode for SQLite to modify vendors table
    with op.batch_alter_table("vendors", schema=None) as batch_op:
        # Re-add contact fields to vendors
        batch_op.add_column(sa.Column("tax_number", sa.String(), nullable=True))
        batch_op.add_column(sa.Column("business_address", sa.Text(), nullable=True))
        batch_op.add_column(sa.Column("website", sa.String(), nullable=True))
        batch_op.add_column(sa.Column("contact_phone", sa.String(), nullable=True))
        batch_op.add_column(sa.Column("contact_email", sa.String(), nullable=True))

        # Remove company_id from vendors
        batch_op.drop_constraint("fk_vendors_company_id", type_="foreignkey")
        batch_op.drop_index(batch_op.f("ix_vendors_company_id"))
        batch_op.drop_column("company_id")

    # Drop companies table
    op.drop_index(op.f("ix_companies_name"), table_name="companies")
    op.drop_index(op.f("ix_companies_id"), table_name="companies")
    op.drop_table("companies")
