"""Project sharing

Revision ID: bc0e8b3327a7
Revises: e0e717ae2f26
Create Date: 2025-11-20 11:40:03.796112

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "bc0e8b3327a7"
down_revision = "e0e717ae2f26"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("linkuserprojectv2", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "is_owner", sa.BOOLEAN(), server_default="true", nullable=False
            )
        )
        batch_op.add_column(
            sa.Column(
                "is_verified",
                sa.BOOLEAN(),
                server_default="true",
                nullable=False,
            )
        )
        batch_op.add_column(
            sa.Column(
                "permissions", sa.String(), server_default="rwx", nullable=False
            )
        )
        batch_op.create_index(
            "ix_linkuserprojectv2_one_owner_per_project",
            ["project_id"],
            unique=True,
            postgresql_where=sa.text("is_owner IS true"),
        )

        # ### end Alembic commands ###

        # Manually add check constraints
        batch_op.create_check_constraint(
            "owner_is_verified", "NOT (is_owner AND NOT is_verified)"
        )
        batch_op.create_check_constraint(
            "owner_full_permissions", "NOT (is_owner AND permissions <> 'rwx')"
        )
        batch_op.create_check_constraint(
            "valid_permissions", "permissions IN ('r', 'rw', 'rwx')"
        )


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("linkuserprojectv2", schema=None) as batch_op:
        batch_op.drop_index(
            "ix_linkuserprojectv2_one_owner_per_project",
            postgresql_where=sa.text("is_owner IS true"),
        )
        batch_op.drop_column("permissions")
        batch_op.drop_column("is_verified")
        batch_op.drop_column("is_owner")

    # ### end Alembic commands ###
