"""add organization domains table

Revision ID: 103eb05cde37
Revises: 55c5d2499eee
Create Date: 2026-02-09 12:20:06.819716

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "103eb05cde37"
down_revision: str | None = "55c5d2499eee"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "organization_domain",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("domain", sa.String(length=255), nullable=False),
        sa.Column("normalized_domain", sa.String(length=255), nullable=False),
        sa.Column(
            "is_primary", sa.Boolean(), server_default=sa.text("false"), nullable=False
        ),
        sa.Column(
            "is_active", sa.Boolean(), server_default=sa.text("true"), nullable=False
        ),
        sa.Column("verified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column(
            "verification_method",
            sa.String(length=64),
            server_default=sa.text("'platform_admin'"),
            nullable=False,
        ),
        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.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
            name=op.f("fk_organization_domain_organization_id_organization"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_organization_domain")),
    )
    op.create_index(
        "ix_org_domain_normalized_domain_active_unique",
        "organization_domain",
        ["normalized_domain"],
        unique=True,
        postgresql_where=sa.text("is_active IS TRUE"),
    )
    op.create_index(
        "ix_org_domain_org_primary_active_unique",
        "organization_domain",
        ["organization_id"],
        unique=True,
        postgresql_where=sa.text("is_active IS TRUE AND is_primary IS TRUE"),
    )
    op.create_index(
        op.f("ix_organization_domain_normalized_domain"),
        "organization_domain",
        ["normalized_domain"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organization_domain_organization_id"),
        "organization_domain",
        ["organization_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_organization_domain_organization_id"), table_name="organization_domain"
    )
    op.drop_index(
        op.f("ix_organization_domain_normalized_domain"),
        table_name="organization_domain",
    )
    op.drop_index(
        "ix_org_domain_org_primary_active_unique",
        table_name="organization_domain",
        postgresql_where=sa.text("is_active IS TRUE AND is_primary IS TRUE"),
    )
    op.drop_index(
        "ix_org_domain_normalized_domain_active_unique",
        table_name="organization_domain",
        postgresql_where=sa.text("is_active IS TRUE"),
    )
    op.drop_table("organization_domain")
    # ### end Alembic commands ###
