"""add_entities_and_entity_fields

Revision ID: 584c872028d2
Revises: c39ad50b46a9
Create Date: 2025-08-26 16:05:51.322677

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "584c872028d2"
down_revision: str | None = "c39ad50b46a9"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    sa.Enum(
        "INTEGER",
        "NUMBER",
        "TEXT",
        "BOOL",
        "JSON",
        "DATETIME",
        "DATE",
        "SELECT",
        "MULTI_SELECT",
        name="fieldtype",
    ).create(op.get_bind())
    op.create_table(
        "entity",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=False),
        sa.Column("key", sa.String(), nullable=False),
        sa.Column("display_name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("icon", sa.String(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="entity_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="entity_pkey"),
        sa.UniqueConstraint("id", name="entity_id_key"),
        sa.UniqueConstraint("owner_id", "key", name="uq_entity_owner_key"),
    )
    op.create_table(
        "entity_field",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("entity_id", sa.UUID(), nullable=False),
        sa.Column("key", sa.String(), nullable=False),
        sa.Column(
            "type",
            postgresql.ENUM(
                "INTEGER",
                "NUMBER",
                "TEXT",
                "BOOL",
                "JSON",
                "DATETIME",
                "DATE",
                "SELECT",
                "MULTI_SELECT",
                name="fieldtype",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("display_name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column(
            "default_value", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.ForeignKeyConstraint(
            ["entity_id"],
            ["entity.id"],
            ondelete="CASCADE",
            name="entity_field_entity_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="entity_field_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="entity_field_pkey"),
        sa.UniqueConstraint("entity_id", "key", name="uq_entity_field_key"),
        sa.UniqueConstraint("id", name="entity_field_id_key"),
    )
    op.create_table(
        "entity_field_option",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("field_id", sa.UUID(), nullable=False),
        sa.Column("key", sa.String(), nullable=False),
        sa.Column("label", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["field_id"],
            ["entity_field.id"],
            ondelete="CASCADE",
            name="entity_field_option_field_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="entity_field_option_pkey"),
        sa.UniqueConstraint("field_id", "key", name="uq_field_option_key"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("entity_field_option")
    op.drop_table("entity_field")
    op.drop_table("entity")
    sa.Enum(
        "INTEGER",
        "NUMBER",
        "TEXT",
        "BOOL",
        "JSON",
        "DATETIME",
        "DATE",
        "SELECT",
        "MULTI_SELECT",
        name="fieldtype",
    ).drop(op.get_bind())
    # ### end Alembic commands ###
