"""Add locations and departments (Department Details form).

Revision ID: 023_departments
Revises: 022_inventory_warehouse_balances
Create Date: 2026-08-09
"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "023_departments"
down_revision: Union[str, None] = "022_inventory_warehouse_balances"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "locations",
        sa.Column("id", sa.String(length=36), primary_key=True),
        sa.Column("company_id", sa.String(length=36), nullable=False),
        sa.Column("code", sa.String(length=30), nullable=False),
        sa.Column("name", sa.String(length=200), nullable=False),
        sa.Column("address", sa.String(length=255), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.text("1")),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["company_id"], ["companies.id"], ondelete="CASCADE"),
        sa.UniqueConstraint("company_id", "code", name="uq_locations_company_code"),
    )
    op.create_index("ix_locations_company_id", "locations", ["company_id"])

    op.create_table(
        "departments",
        sa.Column("id", sa.String(length=36), primary_key=True),
        sa.Column("company_id", sa.String(length=36), nullable=False),
        sa.Column("code", sa.String(length=6), nullable=False),
        sa.Column("name", sa.String(length=200), nullable=False),
        sa.Column("head_id", sa.String(length=36), nullable=True),
        sa.Column("location_id", sa.String(length=36), nullable=True),
        sa.Column("parent_department_id", sa.String(length=36), nullable=True),
        sa.Column("monthly_issue_budget", sa.Numeric(18, 2), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("status", sa.String(length=20), nullable=False, server_default="active"),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.text("1")),
        sa.Column("notify_email", sa.String(length=255), nullable=True),
        sa.Column(
            "low_stock_alert",
            sa.String(length=30),
            nullable=False,
            server_default="head",
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["company_id"], ["companies.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(
            ["head_id"], ["user_registrations.id"], ondelete="SET NULL"
        ),
        sa.ForeignKeyConstraint(["location_id"], ["locations.id"], ondelete="RESTRICT"),
        sa.ForeignKeyConstraint(
            ["parent_department_id"], ["departments.id"], ondelete="SET NULL"
        ),
        sa.UniqueConstraint("company_id", "code", name="uq_departments_company_code"),
    )
    op.create_index("ix_departments_company_id", "departments", ["company_id"])
    op.create_index("ix_departments_head_id", "departments", ["head_id"])
    op.create_index("ix_departments_location_id", "departments", ["location_id"])
    op.create_index(
        "ix_departments_parent_department_id",
        "departments",
        ["parent_department_id"],
    )
    op.create_index("ix_departments_status", "departments", ["status"])


def downgrade() -> None:
    op.drop_index("ix_departments_status", table_name="departments")
    op.drop_index("ix_departments_parent_department_id", table_name="departments")
    op.drop_index("ix_departments_location_id", table_name="departments")
    op.drop_index("ix_departments_head_id", table_name="departments")
    op.drop_index("ix_departments_company_id", table_name="departments")
    op.drop_table("departments")
    op.drop_index("ix_locations_company_id", table_name="locations")
    op.drop_table("locations")
