"""initial schema

Revision ID: 001
Revises:
Create Date: 2026-03-01

"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision: str = "001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # users
    op.create_table(
        "users",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("email", sa.String(255), nullable=False, unique=True),
        sa.Column("password_hash", sa.String(255), nullable=False),
        sa.Column("full_name", sa.String(255), nullable=False),
        sa.Column("role", sa.Enum("admin", "client", name="userrole"), nullable=False, server_default="client"),
        sa.Column("is_active", sa.Boolean, nullable=False, server_default="true"),
        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()),
    )
    op.create_index("ix_users_email", "users", ["email"])

    # templates
    op.create_table(
        "templates",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("category_key", sa.String(100), nullable=False, unique=True),
        sa.Column("standard_fields", postgresql.JSONB, nullable=False, server_default="{}"),
        sa.Column("component_schema", postgresql.JSONB, nullable=False, server_default="{}"),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("is_active", sa.Boolean, nullable=False, server_default="true"),
        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()),
    )
    op.create_index("ix_templates_category_key", "templates", ["category_key"])

    # cad_files
    op.create_table(
        "cad_files",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("original_name", sa.String(500), nullable=False),
        sa.Column("stored_path", sa.String(1000), nullable=False),
        sa.Column("file_hash", sa.String(64), nullable=False, unique=True),
        sa.Column("file_size", sa.BigInteger, nullable=True),
        sa.Column("parsed_objects", postgresql.JSONB, nullable=True),
        sa.Column("thumbnail_path", sa.String(1000), nullable=True),
        sa.Column("gltf_path", sa.String(1000), nullable=True),
        sa.Column(
            "processing_status",
            sa.Enum("pending", "processing", "completed", "failed", name="processingstatus"),
            nullable=False,
            server_default="pending",
        ),
        sa.Column("error_message", sa.String(2000), nullable=True),
        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()),
    )
    op.create_index("ix_cad_files_file_hash", "cad_files", ["file_hash"])

    # orders
    op.create_table(
        "orders",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("order_number", sa.String(50), nullable=False, unique=True),
        sa.Column("template_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("templates.id"), nullable=True),
        sa.Column(
            "status",
            sa.Enum("draft", "submitted", "processing", "completed", "rejected", name="orderstatus"),
            nullable=False,
            server_default="draft",
        ),
        sa.Column("created_by", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("source_excel", sa.String(1000), nullable=True),
        sa.Column("notes", sa.Text, nullable=True),
        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()),
    )
    op.create_index("ix_orders_order_number", "orders", ["order_number"])

    # order_items
    op.create_table(
        "order_items",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("order_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("orders.id"), nullable=False),
        sa.Column("row_index", sa.Integer, nullable=False),
        sa.Column("ebene1", sa.String(500), nullable=True),
        sa.Column("ebene2", sa.String(500), nullable=True),
        sa.Column("baureihe", sa.String(500), nullable=True),
        sa.Column("pim_id", sa.String(500), nullable=True),
        sa.Column("produkt_baureihe", sa.String(500), nullable=True),
        sa.Column("gewaehltes_produkt", sa.String(500), nullable=True),
        sa.Column("name_cad_modell", sa.String(500), nullable=True),
        sa.Column("gewuenschte_bildnummer", sa.String(500), nullable=True),
        sa.Column("lagertyp", sa.String(500), nullable=True),
        sa.Column("medias_rendering", sa.Boolean, nullable=True),
        sa.Column("components", postgresql.JSONB, nullable=False, server_default="[]"),
        sa.Column("cad_file_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("cad_files.id"), nullable=True),
        sa.Column("thumbnail_path", sa.String(1000), nullable=True),
        sa.Column(
            "ai_validation_status",
            sa.Enum("not_started", "pending", "completed", "failed", name="aivalidationstatus"),
            nullable=False,
            server_default="not_started",
        ),
        sa.Column("ai_validation_result", postgresql.JSONB, nullable=True),
        sa.Column(
            "item_status",
            sa.Enum("pending", "approved", "rejected", name="itemstatus"),
            nullable=False,
            server_default="pending",
        ),
        sa.Column("notes", sa.Text, nullable=True),
        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()),
    )

    # audit_log
    op.create_table(
        "audit_log",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id"), nullable=True),
        sa.Column("action", sa.String(100), nullable=False),
        sa.Column("entity_type", sa.String(100), nullable=True),
        sa.Column("entity_id", sa.String(255), nullable=True),
        sa.Column("details", postgresql.JSONB, nullable=True),
        sa.Column("timestamp", sa.DateTime, nullable=False, server_default=sa.func.now()),
    )


def downgrade() -> None:
    op.drop_table("audit_log")
    op.drop_table("order_items")
    op.drop_table("orders")
    op.drop_table("cad_files")
    op.drop_table("templates")
    op.drop_table("users")
    op.execute("DROP TYPE IF EXISTS userrole")
    op.execute("DROP TYPE IF EXISTS orderstatus")
    op.execute("DROP TYPE IF EXISTS processingstatus")
    op.execute("DROP TYPE IF EXISTS aivalidationstatus")
    op.execute("DROP TYPE IF EXISTS itemstatus")
