#
# Copyright (c) 2021 the Hugging Face team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.#
"""create_collaborator
Revision ID: 1ff5763812c1
Revises: ba788d7c81bf
Create Date: 2021-05-04 19:04:16.294453
"""
from typing import Tuple

import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic
revision = "1ff5763812c1"
down_revision = "ba788d7c81bf"
branch_labels = None
depends_on = None


def timestamps(indexed: bool = False) -> Tuple[sa.Column, sa.Column]:
    return (
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
            index=indexed,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
            index=indexed,
        ),
    )


def create_collaborators_table() -> None:
    op.create_table(
        "collaborators",
        sa.Column("id", sa.Integer, primary_key=True),
        sa.Column("peer_public_key", sa.LargeBinary, nullable=False, index=False),
        sa.Column("user_id", sa.Integer, nullable=False, index=False),
        sa.Column("whitelist_item_id", sa.Integer, nullable=False, index=False),
        *timestamps(),
    )
    op.execute(
        """
        CREATE TRIGGER update_collaborators_modtime
            BEFORE UPDATE
            ON collaborators
            FOR EACH ROW
        EXECUTE PROCEDURE update_updated_at_column();
        """
    )


def upgrade() -> None:
    op.drop_column("whitelist", "peer_public_key")
    create_collaborators_table()


def downgrade() -> None:
    op.add_column("whitelist", sa.Column("peer_public_key", sa.Text))
    op.drop_table("collaborators")
