"""Add CustomField and ProductCustomField

Revision ID: 76d3895ffd2a
Revises: d7c8189db05f
Create Date: 2024-10-30 13:51:51.528872

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "76d3895ffd2a"
down_revision = "d7c8189db05f"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "custom_fields",
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("slug", postgresql.CITEXT(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column(
            "properties", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("organization_id", sa.Uuid(), nullable=False),
        sa.Column(
            "user_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("custom_fields_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("custom_fields_pkey")),
        sa.UniqueConstraint(
            "slug",
            "organization_id",
            name=op.f("custom_fields_slug_organization_id_key"),
        ),
    )
    op.create_index(
        op.f("ix_custom_fields_created_at"),
        "custom_fields",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_custom_fields_deleted_at"),
        "custom_fields",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_custom_fields_modified_at"),
        "custom_fields",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_custom_fields_slug"), "custom_fields", ["slug"], unique=False
    )
    op.create_index(
        op.f("ix_custom_fields_type"), "custom_fields", ["type"], unique=False
    )
    op.create_table(
        "product_custom_fields",
        sa.Column("product_id", sa.Uuid(), nullable=False),
        sa.Column("custom_field_id", sa.Uuid(), nullable=False),
        sa.Column("order", sa.Integer(), nullable=False),
        sa.Column("required", sa.Boolean(), nullable=False),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["custom_field_id"],
            ["custom_fields.id"],
            name=op.f("product_custom_fields_custom_field_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["product_id"],
            ["products.id"],
            name=op.f("product_custom_fields_product_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint(
            "product_id",
            "custom_field_id",
            "id",
            name=op.f("product_custom_fields_pkey"),
        ),
        sa.UniqueConstraint(
            "product_id",
            "order",
            name=op.f("product_custom_fields_product_id_order_key"),
        ),
    )
    op.create_index(
        op.f("ix_product_custom_fields_created_at"),
        "product_custom_fields",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_product_custom_fields_deleted_at"),
        "product_custom_fields",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_product_custom_fields_modified_at"),
        "product_custom_fields",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_product_custom_fields_order"),
        "product_custom_fields",
        ["order"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_product_custom_fields_order"), table_name="product_custom_fields"
    )
    op.drop_index(
        op.f("ix_product_custom_fields_modified_at"), table_name="product_custom_fields"
    )
    op.drop_index(
        op.f("ix_product_custom_fields_deleted_at"), table_name="product_custom_fields"
    )
    op.drop_index(
        op.f("ix_product_custom_fields_created_at"), table_name="product_custom_fields"
    )
    op.drop_table("product_custom_fields")
    op.drop_index(op.f("ix_custom_fields_type"), table_name="custom_fields")
    op.drop_index(op.f("ix_custom_fields_slug"), table_name="custom_fields")
    op.drop_index(op.f("ix_custom_fields_modified_at"), table_name="custom_fields")
    op.drop_index(op.f("ix_custom_fields_deleted_at"), table_name="custom_fields")
    op.drop_index(op.f("ix_custom_fields_created_at"), table_name="custom_fields")
    op.drop_table("custom_fields")
    # ### end Alembic commands ###
