Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
57.3 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: inbox.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const archiveAllInbox = `-- name: ArchiveAllInbox :execrows
UPDATE inbox_item SET archived = true, returned_at = NULL
WHERE workspace_id = $1 AND recipient_type = 'member' AND recipient_id = $2 AND archived = false
  AND (snoozed_until IS NULL OR snoozed_until <= now())
`

type ArchiveAllInboxParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	RecipientID pgtype.UUID `json:"recipient_id"`
}

// Every archive clears returned_at: a row restored later earns its rank anew
// (UnarchiveInboxItem), so old return times never outrank the inbox again.
func (q *Queries) ArchiveAllInbox(ctx context.Context, arg ArchiveAllInboxParams) (int64, error) {
	result, err := q.db.Exec(ctx, archiveAllInbox, arg.WorkspaceID, arg.RecipientID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const archiveAllReadInbox = `-- name: ArchiveAllReadInbox :execrows
WITH newest_groups AS (
    SELECT DISTINCT ON (COALESCE(i.issue_id, i.id))
           COALESCE(i.issue_id, i.id) AS group_id,
           i.read
    FROM inbox_item i
    WHERE i.workspace_id = $1
      AND i.recipient_type = 'member'
      AND i.recipient_id = $2
      AND i.archived = false
      AND (i.snoozed_until IS NULL OR i.snoozed_until <= now())
    ORDER BY COALESCE(i.issue_id, i.id), i.created_at DESC, i.id DESC
), read_groups AS (
    SELECT group_id
    FROM newest_groups
    WHERE read = true
)
UPDATE inbox_item i SET archived = true, returned_at = NULL
FROM read_groups selected
WHERE i.workspace_id = $1
  AND i.recipient_type = 'member'
  AND i.recipient_id = $2
  AND i.archived = false
  AND COALESCE(i.issue_id, i.id) = selected.group_id
`

type ArchiveAllReadInboxParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	RecipientID pgtype.UUID `json:"recipient_id"`
}

// "Read" is the state of the one issue row the inbox renders: the newest
// active notification in that issue group. Archive every row in groups whose
// newest row is read, and leave an unread group wholly untouched. Updating raw
// read rows instead makes an older unread sibling reappear after the newest
// row is archived (and can archive an older read sibling under an unread row).
func (q *Queries) ArchiveAllReadInbox(ctx context.Context, arg ArchiveAllReadInboxParams) (int64, error) {
	result, err := q.db.Exec(ctx, archiveAllReadInbox, arg.WorkspaceID, arg.RecipientID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const archiveCompletedInbox = `-- name: ArchiveCompletedInbox :execrows
UPDATE inbox_item i SET archived = true, snoozed_until = NULL, snoozed_at = NULL, returned_at = NULL
WHERE i.workspace_id = $1 AND i.recipient_type = 'member' AND i.recipient_id = $2 AND i.archived = false
  AND i.issue_id IN (
    SELECT id FROM issue
    WHERE workspace_id = $1
      AND status = ANY($3::text[])
  )
`

type ArchiveCompletedInboxParams struct {
	WorkspaceID        pgtype.UUID `json:"workspace_id"`
	RecipientID        pgtype.UUID `json:"recipient_id"`
	TerminalStatusKeys []string    `json:"terminal_status_keys"`
}

// Snoozed issues included: a done issue leaves the inbox either way.
func (q *Queries) ArchiveCompletedInbox(ctx context.Context, arg ArchiveCompletedInboxParams) (int64, error) {
	result, err := q.db.Exec(ctx, archiveCompletedInbox, arg.WorkspaceID, arg.RecipientID, arg.TerminalStatusKeys)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const archiveInboxByIssue = `-- name: ArchiveInboxByIssue :execrows
UPDATE inbox_item SET archived = true, snoozed_until = NULL, snoozed_at = NULL, returned_at = NULL
WHERE workspace_id = $1 AND recipient_type = $2 AND recipient_id = $3 AND issue_id = $4 AND archived = false
`

type ArchiveInboxByIssueParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
	IssueID       pgtype.UUID `json:"issue_id"`
}

func (q *Queries) ArchiveInboxByIssue(ctx context.Context, arg ArchiveInboxByIssueParams) (int64, error) {
	result, err := q.db.Exec(ctx, archiveInboxByIssue,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.IssueID,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const archiveInboxByIssueAndType = `-- name: ArchiveInboxByIssueAndType :many
UPDATE inbox_item SET archived = true, snoozed_until = NULL, snoozed_at = NULL, returned_at = NULL
WHERE workspace_id = $1 AND issue_id = $2 AND type = $3 AND archived = false
RETURNING recipient_type, recipient_id
`

type ArchiveInboxByIssueAndTypeParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	IssueID     pgtype.UUID `json:"issue_id"`
	Type        string      `json:"type"`
}

type ArchiveInboxByIssueAndTypeRow struct {
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
}

func (q *Queries) ArchiveInboxByIssueAndType(ctx context.Context, arg ArchiveInboxByIssueAndTypeParams) ([]ArchiveInboxByIssueAndTypeRow, error) {
	rows, err := q.db.Query(ctx, archiveInboxByIssueAndType, arg.WorkspaceID, arg.IssueID, arg.Type)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ArchiveInboxByIssueAndTypeRow{}
	for rows.Next() {
		var i ArchiveInboxByIssueAndTypeRow
		if err := rows.Scan(&i.RecipientType, &i.RecipientID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const archiveInboxItem = `-- name: ArchiveInboxItem :one
UPDATE inbox_item SET archived = true, snoozed_until = NULL, snoozed_at = NULL, returned_at = NULL
WHERE id = $1
RETURNING id, workspace_id, recipient_type, recipient_id, type, severity, issue_id, title, body, read, archived, created_at, actor_type, actor_id, details, section, snoozed_until, snoozed_at, returned_at
`

// Archiving ends a snooze (MUL-9333), on every archive path below: a row
// restored later must not come back still hidden.
func (q *Queries) ArchiveInboxItem(ctx context.Context, id pgtype.UUID) (InboxItem, error) {
	row := q.db.QueryRow(ctx, archiveInboxItem, id)
	var i InboxItem
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.RecipientType,
		&i.RecipientID,
		&i.Type,
		&i.Severity,
		&i.IssueID,
		&i.Title,
		&i.Body,
		&i.Read,
		&i.Archived,
		&i.CreatedAt,
		&i.ActorType,
		&i.ActorID,
		&i.Details,
		&i.Section,
		&i.SnoozedUntil,
		&i.SnoozedAt,
		&i.ReturnedAt,
	)
	return i, err
}

const archiveInboxOverflow = `-- name: ArchiveInboxOverflow :execrows
WITH boundary AS MATERIALIZED (
    SELECT GREATEST(b.created_at, b.returned_at) AS shown_at, b.id
    FROM inbox_item b
    LEFT JOIN issue b_iss ON b_iss.id = b.issue_id
    WHERE b.workspace_id = $1 AND b.recipient_type = $2
      AND b.recipient_id = $3
      AND b.archived = false
      AND (b.snoozed_until IS NULL OR b.snoozed_until <= now())
      AND ($4::uuid[] IS NULL
           OR NOT (COALESCE(b_iss.project_id, '00000000-0000-0000-0000-000000000000'::uuid) = ANY($4::uuid[])))
    ORDER BY GREATEST(b.created_at, b.returned_at) DESC, b.id DESC
    OFFSET $5::int - 1
    LIMIT 1
), overflow AS (
    SELECT o.id FROM inbox_item o
    CROSS JOIN boundary
    LEFT JOIN issue o_iss ON o_iss.id = o.issue_id
    WHERE o.workspace_id = $1 AND o.recipient_type = $2
      AND o.recipient_id = $3
      AND o.archived = false
      AND (o.snoozed_until IS NULL OR o.snoozed_until <= now())
      AND ($4::uuid[] IS NULL
           OR NOT (COALESCE(o_iss.project_id, '00000000-0000-0000-0000-000000000000'::uuid) = ANY($4::uuid[])))
      AND (GREATEST(o.created_at, o.returned_at), o.id) < (boundary.shown_at, boundary.id)
      -- An issue back from a snooze whose newest row does not carry the
      -- return yet (written by an older server, or not normalized) ranks too
      -- low here; never archive that row on its stale rank.
      AND NOT (o.issue_id IS NOT NULL AND o.snoozed_until IS NOT NULL
               AND (o.returned_at IS NULL OR o.returned_at < o.snoozed_until)
               AND NOT EXISTS (
                   SELECT 1 FROM inbox_item newer
                   WHERE newer.workspace_id = o.workspace_id AND newer.recipient_type = o.recipient_type
                     AND newer.recipient_id = o.recipient_id AND newer.issue_id = o.issue_id
                     AND newer.archived = false
                     AND (newer.created_at, newer.id) > (o.created_at, o.id)
               ))
    ORDER BY GREATEST(o.created_at, o.returned_at), o.id
    LIMIT $6::int
    FOR UPDATE OF o SKIP LOCKED
)
UPDATE inbox_item AS item SET archived = true, snoozed_until = NULL, snoozed_at = NULL, returned_at = NULL
WHERE item.id = ANY (ARRAY(SELECT overflow.id FROM overflow))
`

type ArchiveInboxOverflowParams struct {
	WorkspaceID      pgtype.UUID   `json:"workspace_id"`
	RecipientType    string        `json:"recipient_type"`
	RecipientID      pgtype.UUID   `json:"recipient_id"`
	HiddenProjectIds []pgtype.UUID `json:"hidden_project_ids"`
	Keep             int32         `json:"keep"`
	BatchSize        int32         `json:"batch_size"`
}

// Archives up to batch_size of the recipient's shown rows ranked below the
// newest `keep`, oldest first, in ListInboxItems' order. The boundary is read
// in the same statement, so every batch sees the inbox as it is now. Rows are
// archived one by one: unlike the issue-level archive actions, newer rows on
// the same issue stay. Snoozed rows, and rows in projects the recipient can
// no longer see, neither count nor get archived: a snooze is never ended
// behind the user's back, and hidden rows never push out visible ones. SKIP LOCKED keeps a trim from waiting on, or
// deadlocking with, the user's own inbox writes; InboxOverflows tells the
// caller whether skipped rows remain.
// MATERIALIZED: the boundary is read once per statement, not once per row.
// ANY(ARRAY(...)) keeps the update on primary-key lookups; a join against the
// CTE lets the planner pick a scan of the whole table.
func (q *Queries) ArchiveInboxOverflow(ctx context.Context, arg ArchiveInboxOverflowParams) (int64, error) {
	result, err := q.db.Exec(ctx, archiveInboxOverflow,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.HiddenProjectIds,
		arg.Keep,
		arg.BatchSize,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const backfillInboxSnoozeReturns = `-- name: BackfillInboxSnoozeReturns :execrows
WITH candidates AS (
    SELECT s.id
    FROM inbox_item s
    WHERE ($1::uuid IS NULL OR s.workspace_id = $1::uuid)
      AND s.archived = false AND s.issue_id IS NOT NULL
      AND s.snoozed_until IS NOT NULL AND s.returned_at IS NULL
      AND NOT EXISTS (
          SELECT 1 FROM inbox_item newer
          WHERE newer.workspace_id = s.workspace_id AND newer.recipient_type = s.recipient_type
            AND newer.recipient_id = s.recipient_id AND newer.issue_id = s.issue_id
            AND newer.archived = false
            AND (newer.created_at, newer.id) > (s.created_at, s.id)
      )
    LIMIT $2::int
    FOR UPDATE OF s SKIP LOCKED
)
UPDATE inbox_item AS item SET returned_at = item.snoozed_until
WHERE item.id = ANY (ARRAY(SELECT candidates.id FROM candidates))
`

type BackfillInboxSnoozeReturnsParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	BatchSize   int32       `json:"batch_size"`
}

// Gives up to batch_size snoozed issues their return rank where a writer that
// predates returned_at left it out: the issue's newest active row ranks by
// when the snooze ends. Snoozes set before the column existed, and by an
// older server during the rollout. Skips rows another write holds and
// touches only rows still without a rank, so it can run again at any time.
// workspace_id narrows it for tests; production passes NULL.
func (q *Queries) BackfillInboxSnoozeReturns(ctx context.Context, arg BackfillInboxSnoozeReturnsParams) (int64, error) {
	result, err := q.db.Exec(ctx, backfillInboxSnoozeReturns, arg.WorkspaceID, arg.BatchSize)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const countUnreadInbox = `-- name: CountUnreadInbox :one
SELECT count(*) FROM inbox_item i
WHERE i.workspace_id = $1
  AND i.recipient_type = $2
  AND i.recipient_id = $3
  AND i.read = false
  AND i.archived = false
  AND (i.snoozed_until IS NULL OR i.snoozed_until <= now())
  -- MUL-8472: private projects the caller cannot see.
  -- A row without an issue counts as outside any project (see ListInboxItems).
  AND ($4::uuid[] IS NULL OR NOT (COALESCE(
      (SELECT hidden_issue.project_id FROM issue hidden_issue WHERE hidden_issue.id = i.issue_id),
      '00000000-0000-0000-0000-000000000000'::uuid) = ANY($4::uuid[])))
`

type CountUnreadInboxParams struct {
	WorkspaceID      pgtype.UUID   `json:"workspace_id"`
	RecipientType    string        `json:"recipient_type"`
	RecipientID      pgtype.UUID   `json:"recipient_id"`
	HiddenProjectIds []pgtype.UUID `json:"hidden_project_ids"`
}

func (q *Queries) CountUnreadInbox(ctx context.Context, arg CountUnreadInboxParams) (int64, error) {
	row := q.db.QueryRow(ctx, countUnreadInbox,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.HiddenProjectIds,
	)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const countUnreadInboxByWorkspace = `-- name: CountUnreadInboxByWorkspace :many
SELECT newest.workspace_id, count(*) AS count
FROM (
    SELECT DISTINCT ON (i.workspace_id, COALESCE(i.issue_id, i.id))
        i.workspace_id, i.read
    FROM inbox_item i
    JOIN member m ON m.workspace_id = i.workspace_id AND m.user_id = i.recipient_id
        AND m.role IN ('owner', 'admin', 'member', 'guest')
    WHERE i.recipient_type = 'member'
      AND i.recipient_id = $1
      AND i.archived = false
      -- MUL-9333: a snoozed issue does not count until it comes back.
      AND (i.snoozed_until IS NULL OR i.snoozed_until <= now())
      -- MUL-8472: issues in private projects the recipient is not a member
      -- of do not count.
      AND NOT EXISTS (
          SELECT 1 FROM issue private_issue
          JOIN project private_project ON private_project.id = private_issue.project_id
          WHERE private_issue.id = i.issue_id
            AND private_project.visibility = 'private'
            AND NOT EXISTS (
                SELECT 1 FROM project_member pm
                WHERE pm.project_id = private_project.id AND pm.user_id = i.recipient_id
            )
      )
      -- MUL-9420: a guest counts only issues in its own projects, so nothing
      -- outside a project and nothing in a project it is not a member of.
      AND (m.role <> 'guest' OR EXISTS (
          SELECT 1 FROM issue guest_issue
          JOIN project_member gpm ON gpm.project_id = guest_issue.project_id AND gpm.user_id = i.recipient_id
          WHERE guest_issue.id = i.issue_id
      ))
    ORDER BY i.workspace_id, COALESCE(i.issue_id, i.id), i.created_at DESC
) newest
WHERE newest.read = false
GROUP BY newest.workspace_id
`

type CountUnreadInboxByWorkspaceRow struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	Count       int64       `json:"count"`
}

// Per-workspace unread inbox counts for a recipient member, matching the
// inbox UI's deduplicated view: notifications are grouped per issue
// (Linear-style, one row per issue) and an issue counts as unread only when
// its NEWEST non-archived item is unread. Opening an issue reads the whole
// group now (MUL-8618), but rows read before that read only the newest item,
// so counting raw unread rows would keep those older siblings alive and light
// the switcher dot for a workspace whose inbox the user sees as empty
// (MUL-3695). Items without an issue group on their own id. The
// member join keeps counts scoped to workspaces the user still belongs to,
// so a stale item left behind in a workspace the user has since left, or holds
// a role this build does not know in, cannot light the dot.
func (q *Queries) CountUnreadInboxByWorkspace(ctx context.Context, recipientID pgtype.UUID) ([]CountUnreadInboxByWorkspaceRow, error) {
	rows, err := q.db.Query(ctx, countUnreadInboxByWorkspace, recipientID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []CountUnreadInboxByWorkspaceRow{}
	for rows.Next() {
		var i CountUnreadInboxByWorkspaceRow
		if err := rows.Scan(&i.WorkspaceID, &i.Count); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const createInboxItem = `-- name: CreateInboxItem :one
INSERT INTO inbox_item (
    workspace_id, recipient_type, recipient_id,
    type, severity, issue_id, title, body,
    actor_type, actor_id, details, id,
    snoozed_until, snoozed_at, returned_at
)
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, COALESCE($12::uuid, gen_random_uuid()),
       snoozed.snoozed_until, snoozed.snoozed_at, snoozed.snoozed_until
FROM (SELECT 1) AS one
LEFT JOIN LATERAL (
    SELECT s.snoozed_until, s.snoozed_at
    FROM inbox_item s
    WHERE s.workspace_id = $1 AND s.recipient_type = $2 AND s.recipient_id = $3
      AND s.issue_id = $6 AND s.archived = false AND s.snoozed_until > now()
    ORDER BY s.snoozed_until DESC
    LIMIT 1
) AS snoozed ON true
WHERE (($2::text = 'member' AND EXISTS (
    SELECT 1 FROM member WHERE workspace_id = $1 AND user_id = $3
      AND role IN ('owner', 'admin', 'member', 'guest')
)) OR ($2::text = 'agent' AND EXISTS (
    SELECT 1 FROM agent WHERE workspace_id = $1 AND id = $3
)))
AND NOT ($2::text = 'member' AND EXISTS (
    SELECT 1 FROM issue private_issue
    JOIN project private_project ON private_project.id = private_issue.project_id
    WHERE private_issue.id = $6
      AND private_project.visibility = 'private'
      AND NOT EXISTS (
          SELECT 1 FROM project_member pm
          WHERE pm.project_id = private_project.id AND pm.user_id = $3
      )
))
RETURNING id, workspace_id, recipient_type, recipient_id, type, severity, issue_id, title, body, read, archived, created_at, actor_type, actor_id, details, section, snoozed_until, snoozed_at, returned_at
`

type CreateInboxItemParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
	Type          string      `json:"type"`
	Severity      string      `json:"severity"`
	IssueID       pgtype.UUID `json:"issue_id"`
	Title         string      `json:"title"`
	Body          pgtype.Text `json:"body"`
	ActorType     pgtype.Text `json:"actor_type"`
	ActorID       pgtype.UUID `json:"actor_id"`
	Details       []byte      `json:"details"`
	ID            pgtype.UUID `json:"id"`
}

// A missing recipient is a quiet skip (pgx.ErrNoRows), not a delivery failure.
// Check the insertion snapshot without adding recipient row locks.
//
// MUL-9333: a row written on an issue the recipient has snoozed joins the
// snooze, so it stays out of the inbox with the rest of the issue.
// inbox.Deliver ends the snooze instead when the row is one that
// wakes it. A joining row is the issue's newest, so it carries the issue's
// return rank (returned_at, the snooze end); inbox.Deliver clears it from the
// row that held it (HandInboxReturnToRow).
// A role this build does not know is no membership (see
// GetMemberByUserAndWorkspace), so it is sent nothing.
// MUL-8472: a member outside a private project gets no notification about its
// issues, whatever their workspace role. Only project members do.
func (q *Queries) CreateInboxItem(ctx context.Context, arg CreateInboxItemParams) (InboxItem, error) {
	row := q.db.QueryRow(ctx, createInboxItem,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.Type,
		arg.Severity,
		arg.IssueID,
		arg.Title,
		arg.Body,
		arg.ActorType,
		arg.ActorID,
		arg.Details,
		arg.ID,
	)
	var i InboxItem
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.RecipientType,
		&i.RecipientID,
		&i.Type,
		&i.Severity,
		&i.IssueID,
		&i.Title,
		&i.Body,
		&i.Read,
		&i.Archived,
		&i.CreatedAt,
		&i.ActorType,
		&i.ActorID,
		&i.Details,
		&i.Section,
		&i.SnoozedUntil,
		&i.SnoozedAt,
		&i.ReturnedAt,
	)
	return i, err
}

const deleteInboxGroup = `-- name: DeleteInboxGroup :execrows
WITH target AS (
    SELECT selected.id, selected.workspace_id, selected.recipient_type, selected.recipient_id, selected.type, selected.severity, selected.issue_id, selected.title, selected.body, selected.read, selected.archived, selected.created_at, selected.actor_type, selected.actor_id, selected.details, selected.section, selected.snoozed_until, selected.snoozed_at, selected.returned_at FROM inbox_item selected
    WHERE selected.id = $1 AND selected.workspace_id = $2
      AND selected.recipient_type = 'member' AND selected.recipient_id = $3
)
DELETE FROM inbox_item item USING target
WHERE item.workspace_id = target.workspace_id
  AND item.recipient_type = target.recipient_type
  AND item.recipient_id = target.recipient_id
  AND item.archived = target.archived
  AND (item.id = target.id OR item.issue_id = target.issue_id)
`

type DeleteInboxGroupParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	RecipientID pgtype.UUID `json:"recipient_id"`
}

// A visible inbox row groups notifications on an issue. Remove that group
// only in the selected view, so older siblings cannot bring the row back and
// deleting an active row does not erase its archived history.
func (q *Queries) DeleteInboxGroup(ctx context.Context, arg DeleteInboxGroupParams) (int64, error) {
	result, err := q.db.Exec(ctx, deleteInboxGroup, arg.ID, arg.WorkspaceID, arg.RecipientID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const getInboxItemInWorkspace = `-- name: GetInboxItemInWorkspace :one
SELECT id, workspace_id, recipient_type, recipient_id, type, severity, issue_id, title, body, read, archived, created_at, actor_type, actor_id, details, section, snoozed_until, snoozed_at, returned_at FROM inbox_item
WHERE id = $1 AND workspace_id = $2
`

type GetInboxItemInWorkspaceParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) GetInboxItemInWorkspace(ctx context.Context, arg GetInboxItemInWorkspaceParams) (InboxItem, error) {
	row := q.db.QueryRow(ctx, getInboxItemInWorkspace, arg.ID, arg.WorkspaceID)
	var i InboxItem
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.RecipientType,
		&i.RecipientID,
		&i.Type,
		&i.Severity,
		&i.IssueID,
		&i.Title,
		&i.Body,
		&i.Read,
		&i.Archived,
		&i.CreatedAt,
		&i.ActorType,
		&i.ActorID,
		&i.Details,
		&i.Section,
		&i.SnoozedUntil,
		&i.SnoozedAt,
		&i.ReturnedAt,
	)
	return i, err
}

const handInboxReturnToRow = `-- name: HandInboxReturnToRow :exec
UPDATE inbox_item AS item SET returned_at = NULL
FROM inbox_item AS kept
WHERE kept.id = $5
  AND item.workspace_id = $1 AND item.recipient_type = $2 AND item.recipient_id = $3 AND item.issue_id = $4
  AND item.archived = false AND item.returned_at IS NOT NULL
  AND (item.created_at, item.id) < (kept.created_at, kept.id)
`

type HandInboxReturnToRowParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
	IssueID       pgtype.UUID `json:"issue_id"`
	KeepID        pgtype.UUID `json:"keep_id"`
}

// Clears the issue's return rank from the active rows older than keep_id, the
// row that now carries it (see CreateInboxItem). Only older rows: two joining
// rows handing over concurrently, in either order, leave the rank on the newer.
func (q *Queries) HandInboxReturnToRow(ctx context.Context, arg HandInboxReturnToRowParams) error {
	_, err := q.db.Exec(ctx, handInboxReturnToRow,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.IssueID,
		arg.KeepID,
	)
	return err
}

const inboxOverflows = `-- name: InboxOverflows :one
SELECT count(*) > 0 FROM (
    SELECT 1 FROM inbox_item i
    LEFT JOIN issue iss ON iss.id = i.issue_id
    WHERE i.workspace_id = $1 AND i.recipient_type = $2 AND i.recipient_id = $3
      AND i.archived = false
      AND (i.snoozed_until IS NULL OR i.snoozed_until <= now())
      AND ($4::uuid[] IS NULL
           OR NOT (COALESCE(iss.project_id, '00000000-0000-0000-0000-000000000000'::uuid) = ANY($4::uuid[])))
    ORDER BY GREATEST(i.created_at, i.returned_at) DESC, i.id DESC
    OFFSET $5::int
    LIMIT 1
) beyond
`

type InboxOverflowsParams struct {
	WorkspaceID      pgtype.UUID   `json:"workspace_id"`
	RecipientType    string        `json:"recipient_type"`
	RecipientID      pgtype.UUID   `json:"recipient_id"`
	HiddenProjectIds []pgtype.UUID `json:"hidden_project_ids"`
	Keep             int32         `json:"keep"`
}

// Whether the recipient's inbox shows more than `keep` rows, by the rules
// ListInboxItems shows them: active, not snoozed, outside the projects the
// recipient cannot see (hidden_project_ids), ranked by when they arrived or
// came back.
func (q *Queries) InboxOverflows(ctx context.Context, arg InboxOverflowsParams) (bool, error) {
	row := q.db.QueryRow(ctx, inboxOverflows,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.HiddenProjectIds,
		arg.Keep,
	)
	var column_1 bool
	err := row.Scan(&column_1)
	return column_1, err
}

const inboxReturnsSettled = `-- name: InboxReturnsSettled :one
WITH marked AS MATERIALIZED (
    SELECT g.issue_id, max(g.returned_at) AS returned_at
    FROM inbox_item g
    WHERE g.workspace_id = $1 AND g.recipient_type = $2 AND g.recipient_id = $3
      AND g.archived = false AND g.issue_id IS NOT NULL
      AND (g.returned_at IS NOT NULL OR g.snoozed_until IS NOT NULL)
    GROUP BY g.issue_id
), wanted AS MATERIALIZED (
    SELECT DISTINCT ON (r.issue_id) r.issue_id, r.id,
           GREATEST(marked.returned_at, r.snoozed_until) AS returned_at
    FROM inbox_item r
    JOIN marked ON marked.issue_id = r.issue_id
    WHERE r.workspace_id = $1 AND r.recipient_type = $2 AND r.recipient_id = $3
      AND r.archived = false
    ORDER BY r.issue_id, r.created_at DESC, r.id DESC
)
SELECT count(*) = 0 FROM (
    SELECT 1 FROM inbox_item item
    JOIN wanted ON wanted.issue_id = item.issue_id
    WHERE item.workspace_id = $1 AND item.recipient_type = $2 AND item.recipient_id = $3
      AND item.archived = false
      AND item.returned_at IS DISTINCT FROM (CASE WHEN item.id = wanted.id THEN wanted.returned_at END)
    LIMIT 1
) unsettled
`

type InboxReturnsSettledParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
}

// Whether NormalizeInboxReturns has nothing left to fix for the recipient,
// rows another write holds included. The inbox limit archives only then, so
// an unsettled duplicate rank never widens the set of rows it keeps.
func (q *Queries) InboxReturnsSettled(ctx context.Context, arg InboxReturnsSettledParams) (bool, error) {
	row := q.db.QueryRow(ctx, inboxReturnsSettled, arg.WorkspaceID, arg.RecipientType, arg.RecipientID)
	var column_1 bool
	err := row.Scan(&column_1)
	return column_1, err
}

const listArchivedInboxItems = `-- name: ListArchivedInboxItems :many
WITH eligible_archived AS MATERIALIZED (
    SELECT i.id,
           COALESCE(i.issue_id, i.id) AS group_id,
           i.created_at,
           i.details
    FROM inbox_item i
    WHERE i.workspace_id = $1
      AND i.recipient_type = $2
      AND i.recipient_id = $3
      AND i.archived = true
      -- MUL-8472: private projects the caller cannot see.
      -- A row without an issue counts as outside any project, which a guest's
      -- hidden set holds (projectaccess.NoProject), as ListInboxItems does.
      AND ($4::uuid[] IS NULL OR NOT (COALESCE(
          (SELECT hidden_issue.project_id FROM issue hidden_issue WHERE hidden_issue.id = i.issue_id),
          '00000000-0000-0000-0000-000000000000'::uuid) = ANY($4::uuid[])))
      AND (i.issue_id IS NULL OR NOT EXISTS (
          SELECT 1
          FROM inbox_item active
          WHERE active.workspace_id = i.workspace_id
            AND active.recipient_type = i.recipient_type
            AND active.recipient_id = i.recipient_id
            AND active.issue_id = i.issue_id
            AND active.archived = false
      ))
), newest_groups AS (
    SELECT DISTINCT ON (group_id)
           group_id,
           id AS newest_id,
           created_at AS newest_created_at
    FROM eligible_archived
    ORDER BY group_id, created_at DESC, id DESC
), limited_groups AS (
    SELECT group_id, newest_id
    FROM newest_groups
    ORDER BY newest_created_at DESC, newest_id DESC
    LIMIT 200
), comment_anchors AS (
    SELECT DISTINCT ON (archived.group_id)
           archived.group_id,
           archived.id
    FROM eligible_archived archived
    JOIN limited_groups selected USING (group_id)
    WHERE NULLIF(archived.details->>'comment_id', '') IS NOT NULL
    ORDER BY archived.group_id, archived.created_at DESC, archived.id DESC
), selected_ids AS (
    SELECT newest_id AS id FROM limited_groups
    UNION
    SELECT id FROM comment_anchors
)
SELECT i.id, i.workspace_id, i.recipient_type, i.recipient_id, i.type, i.severity, i.issue_id, i.title, i.body, i.read, i.archived, i.created_at, i.actor_type, i.actor_id, i.details, i.section, i.snoozed_until, i.snoozed_at, i.returned_at,
       iss.number AS issue_number,
       iss.status AS issue_status,
       iss.priority AS issue_priority,
       iss.project_id AS issue_project_id,
       iss.assignee_type AS issue_assignee_type,
       iss.assignee_id AS issue_assignee_id,
       iss.due_date AS issue_due_date,
       ARRAY(SELECT il.label_id::text FROM issue_to_label il
             WHERE iss.id IS NOT NULL AND il.issue_id = iss.id
             ORDER BY il.label_id)::text[] AS issue_label_ids
FROM inbox_item i
JOIN selected_ids selected ON selected.id = i.id
LEFT JOIN issue iss ON iss.id = i.issue_id
ORDER BY i.created_at DESC, i.id DESC
`

type ListArchivedInboxItemsParams struct {
	WorkspaceID      pgtype.UUID   `json:"workspace_id"`
	RecipientType    string        `json:"recipient_type"`
	RecipientID      pgtype.UUID   `json:"recipient_id"`
	HiddenProjectIds []pgtype.UUID `json:"hidden_project_ids"`
}

type ListArchivedInboxItemsRow struct {
	ID                pgtype.UUID        `json:"id"`
	WorkspaceID       pgtype.UUID        `json:"workspace_id"`
	RecipientType     string             `json:"recipient_type"`
	RecipientID       pgtype.UUID        `json:"recipient_id"`
	Type              string             `json:"type"`
	Severity          string             `json:"severity"`
	IssueID           pgtype.UUID        `json:"issue_id"`
	Title             string             `json:"title"`
	Body              pgtype.Text        `json:"body"`
	Read              bool               `json:"read"`
	Archived          bool               `json:"archived"`
	CreatedAt         pgtype.Timestamptz `json:"created_at"`
	ActorType         pgtype.Text        `json:"actor_type"`
	ActorID           pgtype.UUID        `json:"actor_id"`
	Details           []byte             `json:"details"`
	Section           pgtype.Text        `json:"section"`
	SnoozedUntil      pgtype.Timestamptz `json:"snoozed_until"`
	SnoozedAt         pgtype.Timestamptz `json:"snoozed_at"`
	ReturnedAt        pgtype.Timestamptz `json:"returned_at"`
	IssueNumber       pgtype.Int4        `json:"issue_number"`
	IssueStatus       pgtype.Text        `json:"issue_status"`
	IssuePriority     pgtype.Text        `json:"issue_priority"`
	IssueProjectID    pgtype.UUID        `json:"issue_project_id"`
	IssueAssigneeType pgtype.Text        `json:"issue_assignee_type"`
	IssueAssigneeID   pgtype.UUID        `json:"issue_assignee_id"`
	IssueDueDate      pgtype.Date        `json:"issue_due_date"`
	IssueLabelIds     []string           `json:"issue_label_ids"`
}

// Archived counterpart of ListInboxItems, backing the inbox's "Archived"
// sub-view (MUL-3736).
//
// An issue whose group still has an active row is excluded: archiving is
// issue-level, so a NEW notification on an already-archived issue leaves the
// old archived rows in place alongside the fresh active one. The issue belongs
// in the main inbox at that point, and the two lists must stay mutually
// exclusive per issue group — otherwise the same issue renders in both. The
// exclusion lives here rather than in the client so neither list depends on
// the other's cache being loaded. Items without an issue_id group on their own
// id and can never have an active sibling, hence the IS NULL short-circuit.
//
// LIMIT applies to ISSUE GROUPS, not raw notification rows. Applying it after
// the final SELECT lets one noisy issue consume all 200 rows and hide other
// archived issues, which makes both the visible list and its count too small.
// The response stays bounded at two rows per group: the newest row the UI
// renders plus, when different, the newest row carrying a comment anchor. The
// client already merges those two rows while deduplicating, preserving direct
// comment landing without sending every historical notification in the group.
// Keep the materialized working set narrow: full row data is joined only for
// the final selected ids, not copied for every archived notification scanned.
func (q *Queries) ListArchivedInboxItems(ctx context.Context, arg ListArchivedInboxItemsParams) ([]ListArchivedInboxItemsRow, error) {
	rows, err := q.db.Query(ctx, listArchivedInboxItems,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.HiddenProjectIds,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListArchivedInboxItemsRow{}
	for rows.Next() {
		var i ListArchivedInboxItemsRow
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.RecipientType,
			&i.RecipientID,
			&i.Type,
			&i.Severity,
			&i.IssueID,
			&i.Title,
			&i.Body,
			&i.Read,
			&i.Archived,
			&i.CreatedAt,
			&i.ActorType,
			&i.ActorID,
			&i.Details,
			&i.Section,
			&i.SnoozedUntil,
			&i.SnoozedAt,
			&i.ReturnedAt,
			&i.IssueNumber,
			&i.IssueStatus,
			&i.IssuePriority,
			&i.IssueProjectID,
			&i.IssueAssigneeType,
			&i.IssueAssigneeID,
			&i.IssueDueDate,
			&i.IssueLabelIds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listInboxItems = `-- name: ListInboxItems :many
SELECT i.id, i.workspace_id, i.recipient_type, i.recipient_id, i.type, i.severity, i.issue_id, i.title, i.body, i.read, i.archived, i.created_at, i.actor_type, i.actor_id, i.details, i.section, i.snoozed_until, i.snoozed_at, i.returned_at,
       iss.number AS issue_number,
       iss.status AS issue_status,
       iss.priority AS issue_priority,
       iss.project_id AS issue_project_id,
       iss.assignee_type AS issue_assignee_type,
       iss.assignee_id AS issue_assignee_id,
       iss.due_date AS issue_due_date,
       ARRAY(SELECT il.label_id::text FROM issue_to_label il
             WHERE iss.id IS NOT NULL AND il.issue_id = iss.id
             ORDER BY il.label_id)::text[] AS issue_label_ids
FROM inbox_item i
LEFT JOIN issue iss ON iss.id = i.issue_id
WHERE i.workspace_id = $1 AND i.recipient_type = $2 AND i.recipient_id = $3 AND i.archived = false
  -- MUL-9333: a snoozed issue stays out of the inbox until its time. A row
  -- whose time has passed is listed again and keeps snoozed_until, which is
  -- how the client tells an issue that came back on its own.
  AND (i.snoozed_until IS NULL OR i.snoozed_until <= now())
  AND ($4::uuid[] IS NULL OR NOT (COALESCE(iss.project_id, '00000000-0000-0000-0000-000000000000'::uuid) = ANY($4::uuid[])))
ORDER BY GREATEST(i.created_at, i.returned_at) DESC, i.id DESC
LIMIT $5
`

type ListInboxItemsParams struct {
	WorkspaceID      pgtype.UUID   `json:"workspace_id"`
	RecipientType    string        `json:"recipient_type"`
	RecipientID      pgtype.UUID   `json:"recipient_id"`
	HiddenProjectIds []pgtype.UUID `json:"hidden_project_ids"`
	RowLimit         int32         `json:"row_limit"`
}

type ListInboxItemsRow struct {
	ID                pgtype.UUID        `json:"id"`
	WorkspaceID       pgtype.UUID        `json:"workspace_id"`
	RecipientType     string             `json:"recipient_type"`
	RecipientID       pgtype.UUID        `json:"recipient_id"`
	Type              string             `json:"type"`
	Severity          string             `json:"severity"`
	IssueID           pgtype.UUID        `json:"issue_id"`
	Title             string             `json:"title"`
	Body              pgtype.Text        `json:"body"`
	Read              bool               `json:"read"`
	Archived          bool               `json:"archived"`
	CreatedAt         pgtype.Timestamptz `json:"created_at"`
	ActorType         pgtype.Text        `json:"actor_type"`
	ActorID           pgtype.UUID        `json:"actor_id"`
	Details           []byte             `json:"details"`
	Section           pgtype.Text        `json:"section"`
	SnoozedUntil      pgtype.Timestamptz `json:"snoozed_until"`
	SnoozedAt         pgtype.Timestamptz `json:"snoozed_at"`
	ReturnedAt        pgtype.Timestamptz `json:"returned_at"`
	IssueNumber       pgtype.Int4        `json:"issue_number"`
	IssueStatus       pgtype.Text        `json:"issue_status"`
	IssuePriority     pgtype.Text        `json:"issue_priority"`
	IssueProjectID    pgtype.UUID        `json:"issue_project_id"`
	IssueAssigneeType pgtype.Text        `json:"issue_assignee_type"`
	IssueAssigneeID   pgtype.UUID        `json:"issue_assignee_id"`
	IssueDueDate      pgtype.Date        `json:"issue_due_date"`
	IssueLabelIds     []string           `json:"issue_label_ids"`
}

// The issue projections feed the inbox filters. Keep this column list
// identical to ListArchivedInboxItems: the handler converts one row type into
// the other.
// Newest first by when the row arrived or came back (returned_at), the order
// idx_inbox_active_shown keeps. The
// inbox keeps only its newest rows (inbox.ActiveLimit); inbox.Trimmer archives
// the rest, and the limit bounds the response until it has.
func (q *Queries) ListInboxItems(ctx context.Context, arg ListInboxItemsParams) ([]ListInboxItemsRow, error) {
	rows, err := q.db.Query(ctx, listInboxItems,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.HiddenProjectIds,
		arg.RowLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListInboxItemsRow{}
	for rows.Next() {
		var i ListInboxItemsRow
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.RecipientType,
			&i.RecipientID,
			&i.Type,
			&i.Severity,
			&i.IssueID,
			&i.Title,
			&i.Body,
			&i.Read,
			&i.Archived,
			&i.CreatedAt,
			&i.ActorType,
			&i.ActorID,
			&i.Details,
			&i.Section,
			&i.SnoozedUntil,
			&i.SnoozedAt,
			&i.ReturnedAt,
			&i.IssueNumber,
			&i.IssueStatus,
			&i.IssuePriority,
			&i.IssueProjectID,
			&i.IssueAssigneeType,
			&i.IssueAssigneeID,
			&i.IssueDueDate,
			&i.IssueLabelIds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listSnoozedInbox = `-- name: ListSnoozedInbox :many
SELECT g.issue_id::uuid AS issue_id,
       newest.id AS item_id,
       g.snoozed_until::timestamptz AS snoozed_until,
       g.snoozed_at::timestamptz AS snoozed_at,
       g.updates::bigint AS updates,
       iss.number AS issue_number,
       iss.title AS issue_title,
       iss.status AS issue_status
FROM (
    SELECT i.issue_id,
           max(i.snoozed_until) AS snoozed_until,
           min(i.snoozed_at) AS snoozed_at,
           count(*) FILTER (WHERE i.snoozed_at IS NOT NULL AND i.created_at > i.snoozed_at) AS updates
    FROM inbox_item i
    WHERE i.workspace_id = $1
      AND i.recipient_type = 'member'
      AND i.recipient_id = $2
      AND i.archived = false
      AND i.issue_id IS NOT NULL
      AND i.snoozed_until > now()
      -- MUL-8472: private projects the caller cannot see.
      AND ($3::uuid[] IS NULL OR NOT EXISTS (
          SELECT 1 FROM issue hidden_issue
          WHERE hidden_issue.id = i.issue_id
            AND COALESCE(hidden_issue.project_id, '00000000-0000-0000-0000-000000000000'::uuid) = ANY($3::uuid[])
      ))
    GROUP BY i.issue_id
) g
JOIN LATERAL (
    SELECT n.id
    FROM inbox_item n
    WHERE n.workspace_id = $1
      AND n.recipient_type = 'member'
      AND n.recipient_id = $2
      AND n.issue_id = g.issue_id
      AND n.archived = false
    ORDER BY n.created_at DESC, n.id DESC
    LIMIT 1
) newest ON true
LEFT JOIN issue iss ON iss.id = g.issue_id
ORDER BY g.snoozed_until ASC, g.issue_id
`

type ListSnoozedInboxParams struct {
	WorkspaceID      pgtype.UUID   `json:"workspace_id"`
	RecipientID      pgtype.UUID   `json:"recipient_id"`
	HiddenProjectIds []pgtype.UUID `json:"hidden_project_ids"`
}

type ListSnoozedInboxRow struct {
	IssueID      pgtype.UUID        `json:"issue_id"`
	ItemID       pgtype.UUID        `json:"item_id"`
	SnoozedUntil pgtype.Timestamptz `json:"snoozed_until"`
	SnoozedAt    pgtype.Timestamptz `json:"snoozed_at"`
	Updates      int64              `json:"updates"`
	IssueNumber  pgtype.Int4        `json:"issue_number"`
	IssueTitle   pgtype.Text        `json:"issue_title"`
	IssueStatus  pgtype.Text        `json:"issue_status"`
}

// The recipient's snoozed issues, soonest back first (MUL-9333). `updates` is
// how many rows arrived while snoozed; `item_id` is the issue's newest row,
// the one the snooze endpoints take. `issue_number` builds the identifier the
// row links to; `issue_title` and `issue_status` let the row render without
// fetching each issue (MUL-10056).
func (q *Queries) ListSnoozedInbox(ctx context.Context, arg ListSnoozedInboxParams) ([]ListSnoozedInboxRow, error) {
	rows, err := q.db.Query(ctx, listSnoozedInbox, arg.WorkspaceID, arg.RecipientID, arg.HiddenProjectIds)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListSnoozedInboxRow{}
	for rows.Next() {
		var i ListSnoozedInboxRow
		if err := rows.Scan(
			&i.IssueID,
			&i.ItemID,
			&i.SnoozedUntil,
			&i.SnoozedAt,
			&i.Updates,
			&i.IssueNumber,
			&i.IssueTitle,
			&i.IssueStatus,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markAllInboxRead = `-- name: MarkAllInboxRead :execrows
UPDATE inbox_item SET read = true
WHERE workspace_id = $1 AND recipient_type = 'member' AND recipient_id = $2 AND archived = false AND read = false
  AND (snoozed_until IS NULL OR snoozed_until <= now())
`

type MarkAllInboxReadParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	RecipientID pgtype.UUID `json:"recipient_id"`
}

// The batch actions act on what the inbox lists, so they skip snoozed issues
// (MUL-9333).
func (q *Queries) MarkAllInboxRead(ctx context.Context, arg MarkAllInboxReadParams) (int64, error) {
	result, err := q.db.Exec(ctx, markAllInboxRead, arg.WorkspaceID, arg.RecipientID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const markInboxRead = `-- name: MarkInboxRead :one
WITH target AS (
    SELECT t.workspace_id, t.recipient_type, t.recipient_id, t.issue_id
    FROM inbox_item t
    WHERE t.id = $1 AND t.issue_id IS NOT NULL
), siblings AS (
    UPDATE inbox_item s SET read = true
    FROM target
    WHERE s.workspace_id = target.workspace_id
      AND s.recipient_type = target.recipient_type
      AND s.recipient_id = target.recipient_id
      AND s.issue_id = target.issue_id
      AND s.archived = false AND s.read = false
      AND s.id <> $1
    RETURNING s.id
)
UPDATE inbox_item AS item SET read = true
WHERE item.id = $1
RETURNING item.id, item.workspace_id, item.recipient_type, item.recipient_id, item.type, item.severity, item.issue_id, item.title, item.body, item.read, item.archived, item.created_at, item.actor_type, item.actor_id, item.details, item.section, item.snoozed_until, item.snoozed_at, item.returned_at, (SELECT count(*) FROM siblings)::bigint AS siblings_read
`

type MarkInboxReadRow struct {
	InboxItem    InboxItem `json:"inbox_item"`
	SiblingsRead int64     `json:"siblings_read"`
}

// Opening a row in the inbox opens its issue, so it reads every active unread
// row on that issue, not just the one shown (MUL-8618). The inbox renders one
// row per issue carrying its newest item; reading only that item left older
// unread siblings (an assignment under a later comment) unread for good, out of
// sight. This is the same rule as MarkInboxReadByIssue, which reads an issue
// opened outside the inbox. The item itself is read whether or not it is
// archived; its siblings only while active, as in MarkInboxReadByIssue, which
// keeps the probe on the partial index idx_inbox_active_by_issue. Items without
// an issue are their own group. siblings_read is how many other rows it read.
func (q *Queries) MarkInboxRead(ctx context.Context, id pgtype.UUID) (MarkInboxReadRow, error) {
	row := q.db.QueryRow(ctx, markInboxRead, id)
	var i MarkInboxReadRow
	err := row.Scan(
		&i.InboxItem.ID,
		&i.InboxItem.WorkspaceID,
		&i.InboxItem.RecipientType,
		&i.InboxItem.RecipientID,
		&i.InboxItem.Type,
		&i.InboxItem.Severity,
		&i.InboxItem.IssueID,
		&i.InboxItem.Title,
		&i.InboxItem.Body,
		&i.InboxItem.Read,
		&i.InboxItem.Archived,
		&i.InboxItem.CreatedAt,
		&i.InboxItem.ActorType,
		&i.InboxItem.ActorID,
		&i.InboxItem.Details,
		&i.InboxItem.Section,
		&i.InboxItem.SnoozedUntil,
		&i.InboxItem.SnoozedAt,
		&i.InboxItem.ReturnedAt,
		&i.SiblingsRead,
	)
	return i, err
}

const markInboxReadByIssue = `-- name: MarkInboxReadByIssue :execrows
UPDATE inbox_item SET read = true
WHERE workspace_id = $1 AND recipient_type = 'member' AND recipient_id = $2
  AND issue_id = $3 AND archived = false AND read = false
  AND (snoozed_until IS NULL OR snoozed_until <= now())
`

type MarkInboxReadByIssueParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	RecipientID pgtype.UUID `json:"recipient_id"`
	IssueID     pgtype.UUID `json:"issue_id"`
}

// Reading an issue outside the inbox (its page, a side peek, the Unread view)
// reads its notifications too, so the inbox does not ask about the same issue
// again (MUL-8588). Every unread row in the issue's group, not just the newest,
// whatever its severity: opening the issue shows all of it, so an assignment,
// review request or mention on it counts as seen (MUL-8618; this replaces the
// MUL-8596 rule that kept those unread). Opening the row in the inbox does the
// same through MarkInboxRead. Archived rows are left as they are, as in
// MarkAllInboxRead, which also keeps the lookup on the partial index
// idx_inbox_active_by_issue.
// A snoozed issue is left unread: viewing it elsewhere does not end the
// snooze, and it comes back as something to look at (MUL-9333).
func (q *Queries) MarkInboxReadByIssue(ctx context.Context, arg MarkInboxReadByIssueParams) (int64, error) {
	result, err := q.db.Exec(ctx, markInboxReadByIssue, arg.WorkspaceID, arg.RecipientID, arg.IssueID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const markInboxUnread = `-- name: MarkInboxUnread :one
UPDATE inbox_item SET read = false
WHERE id = $1
RETURNING id, workspace_id, recipient_type, recipient_id, type, severity, issue_id, title, body, read, archived, created_at, actor_type, actor_id, details, section, snoozed_until, snoozed_at, returned_at
`

// Item-level, unlike MarkInboxRead: the inbox renders one row per issue
// carrying that group's NEWEST item, and the group's read state is that item's
// read state, so flipping the newest item is enough to make the row unread.
// Flipping the whole group unread would resurrect older siblings the user
// already dealt with and inflate CountUnreadInbox (which counts raw rows),
// while changing nothing the UI shows.
func (q *Queries) MarkInboxUnread(ctx context.Context, id pgtype.UUID) (InboxItem, error) {
	row := q.db.QueryRow(ctx, markInboxUnread, id)
	var i InboxItem
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.RecipientType,
		&i.RecipientID,
		&i.Type,
		&i.Severity,
		&i.IssueID,
		&i.Title,
		&i.Body,
		&i.Read,
		&i.Archived,
		&i.CreatedAt,
		&i.ActorType,
		&i.ActorID,
		&i.Details,
		&i.Section,
		&i.SnoozedUntil,
		&i.SnoozedAt,
		&i.ReturnedAt,
	)
	return i, err
}

const normalizeInboxReturns = `-- name: NormalizeInboxReturns :execrows
WITH marked AS MATERIALIZED (
    SELECT g.issue_id, max(g.returned_at) AS returned_at
    FROM inbox_item g
    WHERE g.workspace_id = $1 AND g.recipient_type = $2
      AND g.recipient_id = $3
      AND g.archived = false AND g.issue_id IS NOT NULL
      AND (g.returned_at IS NOT NULL OR g.snoozed_until IS NOT NULL)
    GROUP BY g.issue_id
), wanted AS MATERIALIZED (
    SELECT DISTINCT ON (r.issue_id) r.issue_id, r.id,
           GREATEST(marked.returned_at, r.snoozed_until) AS returned_at
    FROM inbox_item r
    JOIN marked ON marked.issue_id = r.issue_id
    WHERE r.workspace_id = $1 AND r.recipient_type = $2
      AND r.recipient_id = $3
      AND r.archived = false
    ORDER BY r.issue_id, r.created_at DESC, r.id DESC
), stale AS (
    SELECT item.id, CASE WHEN item.id = wanted.id THEN wanted.returned_at END AS returned_at
    FROM inbox_item item
    JOIN wanted ON wanted.issue_id = item.issue_id
    WHERE item.workspace_id = $1 AND item.recipient_type = $2
      AND item.recipient_id = $3
      AND item.archived = false
      AND item.returned_at IS DISTINCT FROM (CASE WHEN item.id = wanted.id THEN wanted.returned_at END)
    ORDER BY item.issue_id, item.id
    LIMIT $4::int
    FOR UPDATE OF item SKIP LOCKED
)
UPDATE inbox_item AS fixed
SET returned_at = (SELECT stale.returned_at FROM stale WHERE stale.id = fixed.id)
WHERE fixed.id = ANY (ARRAY(SELECT stale.id FROM stale))
`

type NormalizeInboxReturnsParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
	BatchSize     int32       `json:"batch_size"`
}

// Leaves at most one row per issue ranked by its return (returned_at): the
// issue's newest active row, carrying the latest return any of its rows had,
// or its snooze time. Writes keep this true; this repairs what they could not,
// such as rows an older server wrote during a rollout. Fixes up to batch_size
// rows, skipping rows another write holds, so it never waits on a lock; the
// caller runs it again while it returns a full batch.
// MATERIALIZED: each step runs once; inlined, a planner without statistics
// re-ran them for every row.
func (q *Queries) NormalizeInboxReturns(ctx context.Context, arg NormalizeInboxReturnsParams) (int64, error) {
	result, err := q.db.Exec(ctx, normalizeInboxReturns,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.BatchSize,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const snoozeInboxGroup = `-- name: SnoozeInboxGroup :many
WITH target AS (
    SELECT t.workspace_id, t.recipient_type, t.recipient_id, t.issue_id
    FROM inbox_item t
    WHERE t.id = $2 AND t.issue_id IS NOT NULL AND t.archived = false
), newest AS (
    SELECT n.id
    FROM inbox_item n
    JOIN target ON n.workspace_id = target.workspace_id
        AND n.recipient_type = target.recipient_type
        AND n.recipient_id = target.recipient_id
        AND n.issue_id = target.issue_id
    WHERE n.archived = false
    ORDER BY n.created_at DESC, n.id DESC
    LIMIT 1
)
UPDATE inbox_item item
SET snoozed_until = $1::timestamptz,
    snoozed_at = CASE WHEN item.snoozed_until > now() AND item.snoozed_at IS NOT NULL THEN item.snoozed_at ELSE now() END,
    read = CASE WHEN item.id = (SELECT id FROM newest) THEN false ELSE item.read END,
    -- The issue comes back on top when the snooze ends: the newest row ranks
    -- by that time for the inbox limit, the others by when they arrived.
    returned_at = CASE WHEN item.id = (SELECT id FROM newest) THEN $1::timestamptz END
FROM target
WHERE item.workspace_id = target.workspace_id
  AND item.recipient_type = target.recipient_type
  AND item.recipient_id = target.recipient_id
  AND item.issue_id = target.issue_id
  AND item.archived = false
RETURNING item.id, item.workspace_id, item.recipient_type, item.recipient_id, item.type, item.severity, item.issue_id, item.title, item.body, item.read, item.archived, item.created_at, item.actor_type, item.actor_id, item.details, item.section, item.snoozed_until, item.snoozed_at, item.returned_at
`

type SnoozeInboxGroupParams struct {
	SnoozedUntil pgtype.Timestamptz `json:"snoozed_until"`
	ID           pgtype.UUID        `json:"id"`
}

// Snooze is issue-level, like archive (MUL-9333): every active row the
// recipient has on the item's issue carries the time it comes back. Changing
// the time of a running snooze keeps when it started, so the count of what
// arrived meanwhile carries on. The newest row turns unread, so the issue
// comes back as something to look at.
func (q *Queries) SnoozeInboxGroup(ctx context.Context, arg SnoozeInboxGroupParams) ([]InboxItem, error) {
	rows, err := q.db.Query(ctx, snoozeInboxGroup, arg.SnoozedUntil, arg.ID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []InboxItem{}
	for rows.Next() {
		var i InboxItem
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.RecipientType,
			&i.RecipientID,
			&i.Type,
			&i.Severity,
			&i.IssueID,
			&i.Title,
			&i.Body,
			&i.Read,
			&i.Archived,
			&i.CreatedAt,
			&i.ActorType,
			&i.ActorID,
			&i.Details,
			&i.Section,
			&i.SnoozedUntil,
			&i.SnoozedAt,
			&i.ReturnedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const unarchiveInboxByIssue = `-- name: UnarchiveInboxByIssue :execrows
UPDATE inbox_item SET archived = false
WHERE workspace_id = $1 AND recipient_type = $2 AND recipient_id = $3 AND issue_id = $4 AND archived = true
`

type UnarchiveInboxByIssueParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
	IssueID       pgtype.UUID `json:"issue_id"`
}

// Issue-level restore, mirroring ArchiveInboxByIssue: archiving one item
// archives every sibling for the same issue, so unarchiving must bring the
// whole group back. Leaves `read` untouched for the same reason as above.
func (q *Queries) UnarchiveInboxByIssue(ctx context.Context, arg UnarchiveInboxByIssueParams) (int64, error) {
	result, err := q.db.Exec(ctx, unarchiveInboxByIssue,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.IssueID,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const unarchiveInboxItem = `-- name: UnarchiveInboxItem :one
UPDATE inbox_item SET archived = false, returned_at = now()
WHERE id = $1
RETURNING id, workspace_id, recipient_type, recipient_id, type, severity, issue_id, title, body, read, archived, created_at, actor_type, actor_id, details, section, snoozed_until, snoozed_at, returned_at
`

// Deliberately does not touch `read`: unarchiving restores an item to the main
// inbox in the exact read/unread state it was archived in, so restoring an
// unread item legitimately raises the unread badge again (MUL-3736).
// returned_at ranks the row as just back for the inbox limit, so a restored
// old notification is not trimmed straight back into the archive. Only the
// restored row: its issue's older rows, restored by UnarchiveInboxByIssue,
// keep their rank so a busy issue does not outrank the whole inbox.
func (q *Queries) UnarchiveInboxItem(ctx context.Context, id pgtype.UUID) (InboxItem, error) {
	row := q.db.QueryRow(ctx, unarchiveInboxItem, id)
	var i InboxItem
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.RecipientType,
		&i.RecipientID,
		&i.Type,
		&i.Severity,
		&i.IssueID,
		&i.Title,
		&i.Body,
		&i.Read,
		&i.Archived,
		&i.CreatedAt,
		&i.ActorType,
		&i.ActorID,
		&i.Details,
		&i.Section,
		&i.SnoozedUntil,
		&i.SnoozedAt,
		&i.ReturnedAt,
	)
	return i, err
}

const unsnoozeInboxGroup = `-- name: UnsnoozeInboxGroup :many
WITH newest AS (
    SELECT n.id FROM inbox_item n
    WHERE n.workspace_id = $1 AND n.recipient_type = $2 AND n.recipient_id = $3 AND n.issue_id = $4
      AND n.archived = false
    ORDER BY n.created_at DESC, n.id DESC
    LIMIT 1
)
UPDATE inbox_item AS item SET snoozed_until = NULL, snoozed_at = NULL,
    returned_at = CASE WHEN item.id = (SELECT id FROM newest) THEN LEAST(item.snoozed_until, now()) END
WHERE item.workspace_id = $1 AND item.recipient_type = $2 AND item.recipient_id = $3 AND item.issue_id = $4
  AND item.archived = false AND item.snoozed_until IS NOT NULL
RETURNING item.id, item.workspace_id, item.recipient_type, item.recipient_id, item.type, item.severity, item.issue_id, item.title, item.body, item.read, item.archived, item.created_at, item.actor_type, item.actor_id, item.details, item.section, item.snoozed_until, item.snoozed_at, item.returned_at
`

type UnsnoozeInboxGroupParams struct {
	WorkspaceID   pgtype.UUID `json:"workspace_id"`
	RecipientType string      `json:"recipient_type"`
	RecipientID   pgtype.UUID `json:"recipient_id"`
	IssueID       pgtype.UUID `json:"issue_id"`
}

// Ends a snooze on one issue: back in the inbox now. Also clears the time a
// snooze that already ran out left on the rows. The issue's newest row ranks
// as back now for the inbox limit, or when the snooze ran out if it already
// had; the others rank by when they arrived.
func (q *Queries) UnsnoozeInboxGroup(ctx context.Context, arg UnsnoozeInboxGroupParams) ([]InboxItem, error) {
	rows, err := q.db.Query(ctx, unsnoozeInboxGroup,
		arg.WorkspaceID,
		arg.RecipientType,
		arg.RecipientID,
		arg.IssueID,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []InboxItem{}
	for rows.Next() {
		var i InboxItem
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.RecipientType,
			&i.RecipientID,
			&i.Type,
			&i.Severity,
			&i.IssueID,
			&i.Title,
			&i.Body,
			&i.Read,
			&i.Archived,
			&i.CreatedAt,
			&i.ActorType,
			&i.ActorID,
			&i.Details,
			&i.Section,
			&i.SnoozedUntil,
			&i.SnoozedAt,
			&i.ReturnedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}