"""Alembic: operational docs, inventory items, warehouses."""
from __future__ import annotations

import sqlalchemy as sa
from alembic import op

from app.models.types import GUID

revision = "0003_operational"
down_revision = "0002_phases_57_511"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "business_documents",
        sa.Column("id", GUID(), primary_key=True),
        sa.Column("tenant_id", GUID(), nullable=False),
        sa.Column("module", sa.String(40), nullable=False),
        sa.Column("doc_type", sa.String(50), nullable=False),
        sa.Column("number", sa.String(50), nullable=False),
        sa.Column("doc_date", sa.Date(), nullable=False),
        sa.Column("party_name", sa.String(255), nullable=True),
        sa.Column("party_id", GUID(), nullable=True),
        sa.Column("amount", sa.Numeric(18, 4), nullable=False, server_default="0"),
        sa.Column("status", sa.String(30), nullable=False, server_default="draft"),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("meta_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.UniqueConstraint("tenant_id", "module", "number", name="uq_biz_doc_tenant_module_number"),
    )
    op.create_index(
        "ix_biz_doc_tenant_module_type",
        "business_documents",
        ["tenant_id", "module", "doc_type"],
    )

    op.create_table(
        "inventory_items",
        sa.Column("id", GUID(), primary_key=True),
        sa.Column("tenant_id", GUID(), nullable=False),
        sa.Column("code", sa.String(50), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("item_type", sa.String(30), nullable=False, server_default="goods"),
        sa.Column("unit", sa.String(30), nullable=False, server_default="عدد"),
        sa.Column("quantity_on_hand", sa.Numeric(18, 4), nullable=False, server_default="0"),
        sa.Column("unit_cost", sa.Numeric(18, 4), nullable=False, server_default="0"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.UniqueConstraint("tenant_id", "code", name="uq_inventory_item_tenant_code"),
    )

    op.create_table(
        "warehouses",
        sa.Column("id", GUID(), primary_key=True),
        sa.Column("tenant_id", GUID(), nullable=False),
        sa.Column("code", sa.String(50), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.UniqueConstraint("tenant_id", "code", name="uq_warehouse_tenant_code"),
    )


def downgrade() -> None:
    op.drop_table("warehouses")
    op.drop_table("inventory_items")
    op.drop_index("ix_biz_doc_tenant_module_type", table_name="business_documents")
    op.drop_table("business_documents")
