"""Add Customer.user_id and remove UserCustomer

Revision ID: c996df1d397f
Revises: 81faf775fce0
Create Date: 2025-01-03 14:43:30.632086

"""

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

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "c996df1d397f"
down_revision = "81faf775fce0"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("customers", sa.Column("user_id", sa.Uuid(), nullable=True))
    op.create_foreign_key(
        op.f("customers_user_id_fkey"),
        "customers",
        "users",
        ["user_id"],
        ["id"],
        ondelete="set null",
    )

    op.execute(
        """
        UPDATE customers
        SET user_id = user_customers.user_id
        FROM user_customers
        WHERE customers.id = user_customers.customer_id
        """
    )

    op.drop_index("ix_user_customers_created_at", table_name="user_customers")
    op.drop_index("ix_user_customers_deleted_at", table_name="user_customers")
    op.drop_index("ix_user_customers_modified_at", table_name="user_customers")
    op.drop_table("user_customers")

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "user_customers",
        sa.Column("user_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("customer_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "modified_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "deleted_at",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customers.id"],
            name="user_customers_customer_id_fkey",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name="user_customers_user_id_fkey",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name="user_customers_pkey"),
        sa.UniqueConstraint("customer_id", name="user_customers_customer_id_key"),
    )
    op.create_index(
        "ix_user_customers_modified_at", "user_customers", ["modified_at"], unique=False
    )
    op.create_index(
        "ix_user_customers_deleted_at", "user_customers", ["deleted_at"], unique=False
    )
    op.create_index(
        "ix_user_customers_created_at", "user_customers", ["created_at"], unique=False
    )

    op.execute(
        """
        INSERT INTO user_customers (user_id, customer_id, created_at)
        SELECT user_id, id, created_at
        FROM customers
        """
    )

    op.drop_constraint(op.f("customers_user_id_fkey"), "customers", type_="foreignkey")
    op.drop_column("customers", "user_id")

    # ### end Alembic commands ###
