"""Add missing tables: tags, tag_assignments, approval_task_links,
board_webhook_payloads, task_custom_field_definitions,
board_task_custom_fields, task_custom_field_values.

Revision ID: f0a1b2c3d4e5
Revises: e5b6c7d8f9a0
Create Date: 2026-05-21
"""

from __future__ import annotations

import sqlalchemy as sa
from alembic import op

revision = "f0a1b2c3d4e5"
down_revision = "e5b6c7d8f9a0"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # tags
    op.create_table(
        "tags",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("organization_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=False),
        sa.Column("color", sa.String(), nullable=False, server_default="9e9e9e"),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("organization_id", "slug", name="uq_tags_organization_id_slug"),
    )
    op.create_index("ix_tags_organization_id", "tags", ["organization_id"])
    op.create_index("ix_tags_slug", "tags", ["slug"])

    # tag_assignments
    op.create_table(
        "tag_assignments",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("task_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("tag_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["tag_id"], ["tags.id"]),
        sa.ForeignKeyConstraint(["task_id"], ["tasks.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("task_id", "tag_id", name="uq_tag_assignments_task_id_tag_id"),
    )
    op.create_index("ix_tag_assignments_task_id", "tag_assignments", ["task_id"])
    op.create_index("ix_tag_assignments_tag_id", "tag_assignments", ["tag_id"])

    # approval_task_links
    op.create_table(
        "approval_task_links",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("approval_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("task_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["approval_id"], ["approvals.id"]),
        sa.ForeignKeyConstraint(["task_id"], ["tasks.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "approval_id", "task_id",
            name="uq_approval_task_links_approval_id_task_id",
        ),
    )
    op.create_index("ix_approval_task_links_approval_id", "approval_task_links", ["approval_id"])
    op.create_index("ix_approval_task_links_task_id", "approval_task_links", ["task_id"])

    # board_webhook_payloads
    op.create_table(
        "board_webhook_payloads",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("board_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("webhook_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("payload", sa.JSON(), nullable=True),
        sa.Column("headers", sa.JSON(), nullable=True),
        sa.Column("source_ip", sa.String(), nullable=True),
        sa.Column("content_type", sa.String(), nullable=True),
        sa.Column("received_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["board_id"], ["boards.id"]),
        sa.ForeignKeyConstraint(["webhook_id"], ["board_webhooks.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_board_webhook_payloads_board_id", "board_webhook_payloads", ["board_id"])
    op.create_index("ix_board_webhook_payloads_webhook_id", "board_webhook_payloads", ["webhook_id"])
    op.create_index("ix_board_webhook_payloads_received_at", "board_webhook_payloads", ["received_at"])

    # task_custom_field_definitions
    op.create_table(
        "task_custom_field_definitions",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("organization_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("field_key", sa.String(), nullable=False),
        sa.Column("label", sa.String(), nullable=False),
        sa.Column("field_type", sa.String(), nullable=False, server_default="text"),
        sa.Column("ui_visibility", sa.String(), nullable=False, server_default="always"),
        sa.Column("validation_regex", sa.String(), nullable=True),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("required", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("default_value", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.CheckConstraint(
            "field_type IN ('text','text_long','integer','decimal','boolean','date','date_time','url','json')",
            name="ck_tcf_def_field_type",
        ),
        sa.CheckConstraint(
            "ui_visibility IN ('always','if_set','hidden')",
            name="ck_tcf_def_ui_visibility",
        ),
        sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "organization_id", "field_key",
            name="uq_task_custom_field_definitions_org_id_field_key",
        ),
    )
    op.create_index(
        "ix_task_custom_field_definitions_organization_id",
        "task_custom_field_definitions", ["organization_id"],
    )
    op.create_index(
        "ix_task_custom_field_definitions_field_key",
        "task_custom_field_definitions", ["field_key"],
    )

    # board_task_custom_fields
    op.create_table(
        "board_task_custom_fields",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("organization_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("board_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "task_custom_field_definition_id",
            sa.dialects.postgresql.UUID(as_uuid=True),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["board_id"], ["boards.id"]),
        sa.ForeignKeyConstraint(
            ["task_custom_field_definition_id"], ["task_custom_field_definitions.id"]
        ),
        sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "board_id", "task_custom_field_definition_id",
            name="uq_board_tcf_board_id_def_id",
        ),
    )
    op.create_index("ix_board_task_custom_fields_board_id", "board_task_custom_fields", ["board_id"])
    op.create_index(
        "ix_board_task_custom_fields_task_custom_field_definition_id",
        "board_task_custom_fields", ["task_custom_field_definition_id"],
    )

    # task_custom_field_values
    op.create_table(
        "task_custom_field_values",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("organization_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("task_id", sa.dialects.postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column(
            "task_custom_field_definition_id",
            sa.dialects.postgresql.UUID(as_uuid=True),
            nullable=False,
        ),
        sa.Column("value", sa.JSON(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["task_custom_field_definition_id"], ["task_custom_field_definitions.id"]
        ),
        sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"]),
        sa.ForeignKeyConstraint(["task_id"], ["tasks.id"]),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "task_id", "task_custom_field_definition_id",
            name="uq_tcf_values_task_id_def_id",
        ),
    )
    op.create_index(
        "ix_task_custom_field_values_task_id", "task_custom_field_values", ["task_id"]
    )
    op.create_index(
        "ix_task_custom_field_values_task_custom_field_definition_id",
        "task_custom_field_values", ["task_custom_field_definition_id"],
    )


def downgrade() -> None:
    op.drop_table("task_custom_field_values")
    op.drop_table("board_task_custom_fields")
    op.drop_table("task_custom_field_definitions")
    op.drop_table("board_webhook_payloads")
    op.drop_table("approval_task_links")
    op.drop_table("tag_assignments")
    op.drop_table("tags")
