"""Add groups, group_memberships, project_shares, note_shares, notifications tables.

Revision ID: 0025
Revises: 0024
Create Date: 2026-03-11
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision = "0025"
down_revision = "0024"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "groups",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("name", sa.Text(), nullable=False, unique=True),
        sa.Column("description", sa.Text()),
        sa.Column(
            "created_by",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="SET NULL"),
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
    )

    op.create_table(
        "group_memberships",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "group_id",
            sa.Integer(),
            sa.ForeignKey("groups.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("role", sa.Text(), nullable=False, server_default="member"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.UniqueConstraint("group_id", "user_id", name="uq_gm_group_user"),
    )
    op.create_index("idx_gm_user", "group_memberships", ["user_id"])

    op.create_table(
        "project_shares",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "project_id",
            sa.Integer(),
            sa.ForeignKey("projects.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "shared_with_user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="CASCADE"),
        ),
        sa.Column(
            "shared_with_group_id",
            sa.Integer(),
            sa.ForeignKey("groups.id", ondelete="CASCADE"),
        ),
        sa.Column("permission", sa.Text(), nullable=False),
        sa.Column(
            "invited_by",
            sa.Integer(),
            sa.ForeignKey("users.id"),
            nullable=False,
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.CheckConstraint(
            "(shared_with_user_id IS NOT NULL) != (shared_with_group_id IS NOT NULL)",
            name="ck_ps_exclusive_target",
        ),
    )
    op.create_index("idx_ps_project", "project_shares", ["project_id"])
    op.execute(
        "CREATE UNIQUE INDEX idx_ps_user ON project_shares(project_id, shared_with_user_id)"
        " WHERE shared_with_user_id IS NOT NULL"
    )
    op.execute(
        "CREATE UNIQUE INDEX idx_ps_group ON project_shares(project_id, shared_with_group_id)"
        " WHERE shared_with_group_id IS NOT NULL"
    )

    op.create_table(
        "note_shares",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "note_id",
            sa.Integer(),
            sa.ForeignKey("notes.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "shared_with_user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="CASCADE"),
        ),
        sa.Column(
            "shared_with_group_id",
            sa.Integer(),
            sa.ForeignKey("groups.id", ondelete="CASCADE"),
        ),
        sa.Column("permission", sa.Text(), nullable=False),
        sa.Column(
            "invited_by",
            sa.Integer(),
            sa.ForeignKey("users.id"),
            nullable=False,
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.CheckConstraint(
            "(shared_with_user_id IS NOT NULL) != (shared_with_group_id IS NOT NULL)",
            name="ck_ns_exclusive_target",
        ),
    )
    op.execute(
        "CREATE UNIQUE INDEX idx_ns_user ON note_shares(note_id, shared_with_user_id)"
        " WHERE shared_with_user_id IS NOT NULL"
    )
    op.execute(
        "CREATE UNIQUE INDEX idx_ns_group ON note_shares(note_id, shared_with_group_id)"
        " WHERE shared_with_group_id IS NOT NULL"
    )

    op.create_table(
        "notifications",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column(
            "user_id",
            sa.Integer(),
            sa.ForeignKey("users.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("type", sa.Text(), nullable=False),
        sa.Column(
            "payload",
            postgresql.JSONB(),
            nullable=False,
            server_default="{}",
        ),
        sa.Column("read_at", sa.DateTime(timezone=True)),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
    )
    op.execute(
        "CREATE INDEX idx_notif_user_unread ON notifications(user_id, read_at)"
        " WHERE read_at IS NULL"
    )


def downgrade() -> None:
    op.drop_table("notifications")
    op.drop_table("note_shares")
    op.drop_table("project_shares")
    op.drop_table("group_memberships")
    op.drop_table("groups")
