"""add calendar

Revision ID: d8e204bbf615
Revises: d4a1c446458c
Create Date: 2025-09-10 19:56:22.295756

"""

from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = "d8e204bbf615"
down_revision: Union[str, None] = "d4a1c446458c"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "calendar_event",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("room_id", sa.String(), nullable=False),
        sa.Column("ics_uid", sa.Text(), nullable=False),
        sa.Column("title", sa.Text(), nullable=True),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("start_time", sa.DateTime(timezone=True), nullable=False),
        sa.Column("end_time", sa.DateTime(timezone=True), nullable=False),
        sa.Column("attendees", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("location", sa.Text(), nullable=True),
        sa.Column("ics_raw_data", sa.Text(), nullable=True),
        sa.Column("last_synced", sa.DateTime(timezone=True), nullable=False),
        sa.Column(
            "is_deleted", sa.Boolean(), server_default=sa.text("false"), nullable=False
        ),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
        sa.ForeignKeyConstraint(
            ["room_id"],
            ["room.id"],
            name="fk_calendar_event_room_id",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("room_id", "ics_uid", name="uq_room_calendar_event"),
    )
    with op.batch_alter_table("calendar_event", schema=None) as batch_op:
        batch_op.create_index(
            "idx_calendar_event_deleted",
            ["is_deleted"],
            unique=False,
            postgresql_where=sa.text("NOT is_deleted"),
        )
        batch_op.create_index(
            "idx_calendar_event_room_start", ["room_id", "start_time"], unique=False
        )

    with op.batch_alter_table("meeting", schema=None) as batch_op:
        batch_op.add_column(sa.Column("calendar_event_id", sa.String(), nullable=True))
        batch_op.add_column(
            sa.Column(
                "calendar_metadata",
                postgresql.JSONB(astext_type=sa.Text()),
                nullable=True,
            )
        )
        batch_op.create_index(
            "idx_meeting_calendar_event", ["calendar_event_id"], unique=False
        )
        batch_op.create_foreign_key(
            "fk_meeting_calendar_event_id",
            "calendar_event",
            ["calendar_event_id"],
            ["id"],
            ondelete="SET NULL",
        )

    with op.batch_alter_table("room", schema=None) as batch_op:
        batch_op.add_column(sa.Column("ics_url", sa.Text(), nullable=True))
        batch_op.add_column(
            sa.Column(
                "ics_fetch_interval", sa.Integer(), server_default="300", nullable=True
            )
        )
        batch_op.add_column(
            sa.Column(
                "ics_enabled",
                sa.Boolean(),
                server_default=sa.text("false"),
                nullable=False,
            )
        )
        batch_op.add_column(
            sa.Column("ics_last_sync", sa.DateTime(timezone=True), nullable=True)
        )
        batch_op.add_column(sa.Column("ics_last_etag", sa.Text(), nullable=True))
        batch_op.create_index("idx_room_ics_enabled", ["ics_enabled"], unique=False)

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("room", schema=None) as batch_op:
        batch_op.drop_index("idx_room_ics_enabled")
        batch_op.drop_column("ics_last_etag")
        batch_op.drop_column("ics_last_sync")
        batch_op.drop_column("ics_enabled")
        batch_op.drop_column("ics_fetch_interval")
        batch_op.drop_column("ics_url")

    with op.batch_alter_table("meeting", schema=None) as batch_op:
        batch_op.drop_constraint("fk_meeting_calendar_event_id", type_="foreignkey")
        batch_op.drop_index("idx_meeting_calendar_event")
        batch_op.drop_column("calendar_metadata")
        batch_op.drop_column("calendar_event_id")

    with op.batch_alter_table("calendar_event", schema=None) as batch_op:
        batch_op.drop_index("idx_calendar_event_room_start")
        batch_op.drop_index(
            "idx_calendar_event_deleted", postgresql_where=sa.text("NOT is_deleted")
        )

    op.drop_table("calendar_event")
    # ### end Alembic commands ###
