"""add_invitation_sharing_table

Revision ID: 5769cf0b4d86
Revises: 8658049ceb40
Create Date: 2025-07-18 11:08:46.034945

"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "5769cf0b4d86"
down_revision = "8658049ceb40"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "invitations",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("sent_by_user_id", sa.Integer(), nullable=False),
        sa.Column("sent_to_user_id", sa.Integer(), nullable=False),
        sa.Column("invitation_type", sa.String(), nullable=False),
        sa.Column("status", sa.String(), nullable=False),
        sa.Column("title", sa.String(), nullable=False),
        sa.Column("message", sa.Text(), nullable=True),
        sa.Column("context_data", sa.JSON(), nullable=False),
        sa.Column("expires_at", sa.DateTime(), nullable=True),
        sa.Column("responded_at", sa.DateTime(), nullable=True),
        sa.Column("response_note", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["sent_by_user_id"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["sent_to_user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "sent_by_user_id",
            "sent_to_user_id",
            "invitation_type",
            name="unique_active_invitation",
        ),
    )
    op.create_table(
        "family_history_shares",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("invitation_id", sa.Integer(), nullable=False),
        sa.Column("family_member_id", sa.Integer(), nullable=False),
        sa.Column("shared_by_user_id", sa.Integer(), nullable=False),
        sa.Column("shared_with_user_id", sa.Integer(), nullable=False),
        sa.Column("permission_level", sa.String(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("expires_at", sa.DateTime(), nullable=True),
        sa.Column("sharing_note", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["family_member_id"],
            ["family_members.id"],
        ),
        sa.ForeignKeyConstraint(
            ["invitation_id"],
            ["invitations.id"],
        ),
        sa.ForeignKeyConstraint(
            ["shared_by_user_id"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["shared_with_user_id"],
            ["users.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "family_member_id",
            "shared_with_user_id",
            name="unique_family_history_share",
        ),
    )
    op.create_foreign_key(
        "fk_users_active_patient_id", "users", "patients", ["active_patient_id"], ["id"]
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint("fk_users_active_patient_id", "users", type_="foreignkey")
    op.drop_table("family_history_shares")
    op.drop_table("invitations")
    # ### end Alembic commands ###
