"""initial version

Revision ID: 9af1232f6bc0
Revises:
Create Date: 2023-11-28 11:13:26.408554

"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

from duty_board.alchemy.sqlalchemy_types import UtcDateTime

# revision identifiers, used by Alembic.
revision: str = "9af1232f6bc0"
down_revision: Union[str, None] = None
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",
        sa.Column("uid", sa.String(length=50), nullable=False),
        sa.Column("name", sa.String(length=200), nullable=False),
        sa.Column("description", sa.String(length=5000), nullable=True),
        sa.Column("category", sa.String(length=50), nullable=False),
        sa.Column("order", sa.Integer(), nullable=False),
        sa.Column("icalendar_url", sa.String(length=500), nullable=False),
        sa.Column("event_prefix", sa.String(length=50), nullable=True),
        sa.Column(
            "error_msg", sa.String(length=9999), nullable=True, comment="If any, the error of the latest sync attempt."
        ),
        sa.Column("last_update_utc", UtcDateTime(timezone=True), nullable=False),
        sa.Column("sync", sa.Boolean(), nullable=False),
        sa.PrimaryKeyConstraint("uid"),
    )
    op.create_table(
        "person_image",
        sa.Column("uid", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("image_in_bytes", sa.LargeBinary(), nullable=False),
        sa.PrimaryKeyConstraint("uid"),
        sa.UniqueConstraint("uid"),
    )
    op.create_table(
        "token",
        sa.Column("token", sa.String(length=50), nullable=False),
        sa.Column("username", sa.String(length=50), nullable=False),
        sa.Column("last_update_utc", UtcDateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("token"),
        sa.UniqueConstraint("token"),
        sa.UniqueConstraint("username"),
    )
    op.create_table(
        "person",
        sa.Column("uid", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("username", sa.String(length=50), nullable=True),
        sa.Column("email", sa.String(length=50), nullable=True),
        sa.Column("image_uid", sa.Integer(), nullable=True),
        sa.Column("img_width", sa.Integer(), nullable=True),
        sa.Column("img_height", sa.Integer(), nullable=True),
        sa.Column(
            "extra_attributes_json",
            sa.String(length=100000),
            nullable=True,
            comment="Extra attributes represented as a json.",
        ),
        sa.Column(
            "error_msg", sa.String(length=9999), nullable=True, comment="If any, the error of the latest sync attempt."
        ),
        sa.Column("last_update_utc", UtcDateTime(timezone=True), nullable=False),
        sa.Column("sync", sa.Boolean(), nullable=False),
        sa.CheckConstraint("NOT(username IS NULL AND email IS NULL)", name="user_email_not_both_none"),
        sa.ForeignKeyConstraint(["image_uid"], ["person_image.uid"], ondelete="RESTRICT"),
        sa.PrimaryKeyConstraint("uid"),
        sa.UniqueConstraint("image_uid", name="unique_image_uid"),
        sa.UniqueConstraint("uid"),
        sa.UniqueConstraint("username", "email", name="username_email_unique"),
    )
    op.create_table(
        "on_call_event",
        sa.Column("uid", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("start_event_utc", UtcDateTime(timezone=True), nullable=False),
        sa.Column("end_event_utc", UtcDateTime(timezone=True), nullable=False),
        sa.Column("calendar_uid", sa.String(length=50), nullable=False),
        sa.Column("person_uid", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["calendar_uid"], ["calendar.uid"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["person_uid"], ["person.uid"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("uid"),
        sa.UniqueConstraint("uid"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("on_call_event")
    op.drop_table("person")
    op.drop_table("token")
    op.drop_table("person_image")
    op.drop_table("calendar")
    # ### end Alembic commands ###
