"""Add task_logs table."""

from alembic import op

revision = "0021"
down_revision = "0020"


def upgrade() -> None:
    op.execute("""
        CREATE TABLE IF NOT EXISTS task_logs (
            id               SERIAL PRIMARY KEY,
            task_id          INTEGER NOT NULL REFERENCES notes(id) ON DELETE CASCADE,
            user_id          INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
            content          TEXT NOT NULL,
            duration_minutes INTEGER,
            created_at       TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
            updated_at       TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
        )
    """)
    op.execute("CREATE INDEX IF NOT EXISTS ix_task_logs_task_id ON task_logs(task_id)")
    op.execute("CREATE INDEX IF NOT EXISTS ix_task_logs_user_id ON task_logs(user_id)")


def downgrade() -> None:
    op.execute("DROP INDEX IF EXISTS ix_task_logs_user_id")
    op.execute("DROP INDEX IF EXISTS ix_task_logs_task_id")
    op.execute("DROP TABLE IF EXISTS task_logs")
