"""empty message

Revision ID: f350153a5691
Revises: 920329927dc6
Create Date: 2021-11-05 23:50:21.539575

"""
import sqlalchemy as sa
import sqlalchemy_utils
from alembic import op
from sqlalchemy import orm
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext.declarative import declarative_base

from project import dbtypes

# revision identifiers, used by Alembic.
revision = "f350153a5691"
down_revision = "920329927dc6"
branch_labels = None
depends_on = None

Base = declarative_base()


class Event(Base):
    __tablename__ = "event"
    id = sa.Column(sa.Integer(), primary_key=True)
    start = sa.Column(sa.DateTime(timezone=True), nullable=False)
    end = sa.Column(sa.DateTime(timezone=True), nullable=True)
    allday = sa.Column(
        sa.Boolean(),
        nullable=False,
        default=False,
        server_default="0",
    )
    recurrence_rule = sa.Column(sa.UnicodeText())
    date_definitions = orm.relationship(
        "EventDateDefinition",
        backref=orm.backref("event", lazy=False),
        cascade="all, delete-orphan",
    )


class EventDateDefinition(Base):
    __tablename__ = "eventdatedefinition"
    id = sa.Column(sa.Integer(), primary_key=True)
    event_id = sa.Column(sa.Integer, sa.ForeignKey("event.id"), nullable=False)
    start = sa.Column(sa.DateTime(timezone=True), nullable=False)
    end = sa.Column(sa.DateTime(timezone=True), nullable=True)
    allday = sa.Column(
        sa.Boolean(),
        nullable=False,
        default=False,
        server_default="0",
    )
    recurrence_rule = sa.Column(sa.UnicodeText())


def upgrade_event_definitions():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    for event in session.query(Event):
        date_definition = EventDateDefinition()
        date_definition.event = event
        date_definition.start = event.start
        date_definition.end = event.end
        date_definition.allday = event.allday
        date_definition.recurrence_rule = event.recurrence_rule
        event.date_definitions = [date_definition]

    session.commit()


def downgrade_event_definitions():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    for event in session.query(Event):
        date_definition = event.date_definitions[0]
        event.start = date_definition.start
        event.end = date_definition.end
        event.allday = date_definition.allday
        event.recurrence_rule = date_definition.recurrence_rule

    session.commit()


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "eventdatedefinition",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("event_id", sa.Integer(), nullable=False),
        sa.Column("start", sa.DateTime(timezone=True), nullable=False),
        sa.Column("end", sa.DateTime(timezone=True), nullable=True),
        sa.Column("allday", sa.Boolean(), server_default="0", nullable=False),
        sa.Column("recurrence_rule", sa.UnicodeText(), nullable=True),
        sa.ForeignKeyConstraint(
            ["event_id"],
            ["event.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )

    upgrade_event_definitions()

    op.drop_column("event", "end")
    op.drop_column("event", "allday")
    op.drop_column("event", "start")
    op.drop_column("event", "recurrence_rule")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "event",
        sa.Column("recurrence_rule", sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "event",
        sa.Column(
            "start",
            postgresql.TIMESTAMP(timezone=True),
            server_default=sa.text("CURRENT_TIMESTAMP"),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.add_column(
        "event",
        sa.Column(
            "allday",
            sa.BOOLEAN(),
            server_default=sa.text("false"),
            autoincrement=False,
            nullable=False,
        ),
    )
    op.add_column(
        "event",
        sa.Column(
            "end",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
    )

    downgrade_event_definitions()

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