Skip to content

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

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

package db

import (
	"context"

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

const claimNextDueSeatCapacityIntent = `-- name: ClaimNextDueSeatCapacityIntent :one
WITH due AS (
    SELECT operation_token
    FROM seat_capacity_outbox
    WHERE dead_lettered_at IS NULL
      AND next_attempt_at <= now()
    ORDER BY next_attempt_at, created_at
    FOR UPDATE SKIP LOCKED
    LIMIT 1
)
UPDATE seat_capacity_outbox AS outbox
SET next_attempt_at = $1,
    lease_token = gen_random_uuid(),
    updated_at = now()
FROM due
WHERE outbox.operation_token = due.operation_token
RETURNING outbox.workspace_id, outbox.operation_token, outbox.action, outbox.subject_id, outbox.member_id, outbox.invitation_id, outbox.share_link_id, outbox.user_id, outbox.expires_at, outbox.delivered_at, outbox.attempt_count, outbox.next_attempt_at, outbox.lease_token, outbox.last_error, outbox.dead_lettered_at, outbox.created_at, outbox.updated_at
`

func (q *Queries) ClaimNextDueSeatCapacityIntent(ctx context.Context, leaseUntil pgtype.Timestamptz) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, claimNextDueSeatCapacityIntent, leaseUntil)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createOrReactivateShareJoinCapacityIntent = `-- name: CreateOrReactivateShareJoinCapacityIntent :one
INSERT INTO seat_capacity_outbox (
    workspace_id, operation_token, action, subject_id, share_link_id, user_id,
    next_attempt_at
) VALUES (
    $1, $2, 'claim_share_join',
    $2, $3, $4,
    $5
)
ON CONFLICT (workspace_id, share_link_id, user_id)
WHERE share_link_id IS NOT NULL AND user_id IS NOT NULL
DO UPDATE SET
    action = 'claim_share_join',
    subject_id = seat_capacity_outbox.operation_token,
    member_id = NULL,
    invitation_id = NULL,
    expires_at = NULL,
    delivered_at = NULL,
    attempt_count = 0,
    next_attempt_at = EXCLUDED.next_attempt_at,
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE seat_capacity_outbox.action IN ('claim_share_join', 'release')
RETURNING workspace_id, operation_token, action, subject_id, member_id, invitation_id, share_link_id, user_id, expires_at, delivered_at, attempt_count, next_attempt_at, lease_token, last_error, dead_lettered_at, created_at, updated_at
`

type CreateOrReactivateShareJoinCapacityIntentParams struct {
	WorkspaceID    pgtype.UUID        `json:"workspace_id"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	ShareLinkID    pgtype.UUID        `json:"share_link_id"`
	UserID         pgtype.UUID        `json:"user_id"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
}

func (q *Queries) CreateOrReactivateShareJoinCapacityIntent(ctx context.Context, arg CreateOrReactivateShareJoinCapacityIntentParams) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, createOrReactivateShareJoinCapacityIntent,
		arg.WorkspaceID,
		arg.OperationToken,
		arg.ShareLinkID,
		arg.UserID,
		arg.NextAttemptAt,
	)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deferClaimedSeatCapacityIntent = `-- name: DeferClaimedSeatCapacityIntent :execrows
UPDATE seat_capacity_outbox
SET last_error = left($1, 1000),
    next_attempt_at = $2,
    lease_token = NULL,
    updated_at = now()
WHERE operation_token = $3
  AND action = $4
  AND lease_token = $5
  AND dead_lettered_at IS NULL
`

type DeferClaimedSeatCapacityIntentParams struct {
	LastError      string             `json:"last_error"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	Action         string             `json:"action"`
	LeaseToken     pgtype.UUID        `json:"lease_token"`
}

func (q *Queries) DeferClaimedSeatCapacityIntent(ctx context.Context, arg DeferClaimedSeatCapacityIntentParams) (int64, error) {
	result, err := q.db.Exec(ctx, deferClaimedSeatCapacityIntent,
		arg.LastError,
		arg.NextAttemptAt,
		arg.OperationToken,
		arg.Action,
		arg.LeaseToken,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const deleteClaimedSeatCapacityIntent = `-- name: DeleteClaimedSeatCapacityIntent :execrows
DELETE FROM seat_capacity_outbox
WHERE operation_token = $1
  AND action = $2
  AND lease_token = $3
`

type DeleteClaimedSeatCapacityIntentParams struct {
	OperationToken pgtype.UUID `json:"operation_token"`
	Action         string      `json:"action"`
	LeaseToken     pgtype.UUID `json:"lease_token"`
}

func (q *Queries) DeleteClaimedSeatCapacityIntent(ctx context.Context, arg DeleteClaimedSeatCapacityIntentParams) (int64, error) {
	result, err := q.db.Exec(ctx, deleteClaimedSeatCapacityIntent, arg.OperationToken, arg.Action, arg.LeaseToken)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const deleteSeatCapacityConfirmIntentsForWorkspaceDeletion = `-- name: DeleteSeatCapacityConfirmIntentsForWorkspaceDeletion :exec
DELETE FROM seat_capacity_outbox
WHERE workspace_id = $1
  AND action = 'confirm'
`

// Confirmed seats are released only through the member-scoped operation below.
// Never rewrite them to token release: Cloud may accept token release for a
// used operation, which would let a stale confirm bypass member ownership.
func (q *Queries) DeleteSeatCapacityConfirmIntentsForWorkspaceDeletion(ctx context.Context, workspaceID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteSeatCapacityConfirmIntentsForWorkspaceDeletion, workspaceID)
	return err
}

const deleteSeatCapacityIntentForAction = `-- name: DeleteSeatCapacityIntentForAction :exec
DELETE FROM seat_capacity_outbox
WHERE operation_token = $1 AND action = $2
`

type DeleteSeatCapacityIntentForActionParams struct {
	OperationToken pgtype.UUID `json:"operation_token"`
	Action         string      `json:"action"`
}

func (q *Queries) DeleteSeatCapacityIntentForAction(ctx context.Context, arg DeleteSeatCapacityIntentForActionParams) error {
	_, err := q.db.Exec(ctx, deleteSeatCapacityIntentForAction, arg.OperationToken, arg.Action)
	return err
}

const enqueueSeatCapacityRelease = `-- name: EnqueueSeatCapacityRelease :one
INSERT INTO seat_capacity_outbox (
    workspace_id, operation_token, action, subject_id, next_attempt_at
) VALUES (
    $1, $2, 'release',
    $3, $4
)
ON CONFLICT (operation_token) DO UPDATE SET
    action = 'release',
    subject_id = EXCLUDED.subject_id,
    member_id = NULL,
    invitation_id = NULL,
    share_link_id = NULL,
    user_id = NULL,
    expires_at = NULL,
    delivered_at = NULL,
    attempt_count = 0,
    next_attempt_at = EXCLUDED.next_attempt_at,
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE seat_capacity_outbox.action IN (
    'reserve_invitation', 'consume_invitation', 'claim_share_join', 'release'
)
RETURNING workspace_id, operation_token, action, subject_id, member_id, invitation_id, share_link_id, user_id, expires_at, delivered_at, attempt_count, next_attempt_at, lease_token, last_error, dead_lettered_at, created_at, updated_at
`

type EnqueueSeatCapacityReleaseParams struct {
	WorkspaceID    pgtype.UUID        `json:"workspace_id"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	SubjectID      pgtype.UUID        `json:"subject_id"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
}

func (q *Queries) EnqueueSeatCapacityRelease(ctx context.Context, arg EnqueueSeatCapacityReleaseParams) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, enqueueSeatCapacityRelease,
		arg.WorkspaceID,
		arg.OperationToken,
		arg.SubjectID,
		arg.NextAttemptAt,
	)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const expireInvitationForCapacityRecovery = `-- name: ExpireInvitationForCapacityRecovery :exec
UPDATE workspace_invitation
SET status = 'expired', updated_at = now()
WHERE id = $1 AND status = 'pending'
`

func (q *Queries) ExpireInvitationForCapacityRecovery(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, expireInvitationForCapacityRecovery, id)
	return err
}

const getClaimedSeatCapacityIntent = `-- name: GetClaimedSeatCapacityIntent :one
SELECT workspace_id, operation_token, action, subject_id, member_id, invitation_id, share_link_id, user_id, expires_at, delivered_at, attempt_count, next_attempt_at, lease_token, last_error, dead_lettered_at, created_at, updated_at FROM seat_capacity_outbox
WHERE operation_token = $1
  AND action = $2
  AND lease_token = $3
`

type GetClaimedSeatCapacityIntentParams struct {
	OperationToken pgtype.UUID `json:"operation_token"`
	Action         string      `json:"action"`
	LeaseToken     pgtype.UUID `json:"lease_token"`
}

func (q *Queries) GetClaimedSeatCapacityIntent(ctx context.Context, arg GetClaimedSeatCapacityIntentParams) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, getClaimedSeatCapacityIntent, arg.OperationToken, arg.Action, arg.LeaseToken)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getMemberReleaseCapacityIntent = `-- name: GetMemberReleaseCapacityIntent :one
SELECT workspace_id, operation_token, action, subject_id, member_id, invitation_id, share_link_id, user_id, expires_at, delivered_at, attempt_count, next_attempt_at, lease_token, last_error, dead_lettered_at, created_at, updated_at FROM seat_capacity_outbox
WHERE workspace_id = $1
  AND member_id = $2
  AND action = 'release_member'
ORDER BY created_at DESC
LIMIT 1
`

type GetMemberReleaseCapacityIntentParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	MemberID    pgtype.UUID `json:"member_id"`
}

func (q *Queries) GetMemberReleaseCapacityIntent(ctx context.Context, arg GetMemberReleaseCapacityIntentParams) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, getMemberReleaseCapacityIntent, arg.WorkspaceID, arg.MemberID)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getSeatCapacityIntent = `-- name: GetSeatCapacityIntent :one
SELECT workspace_id, operation_token, action, subject_id, member_id, invitation_id, share_link_id, user_id, expires_at, delivered_at, attempt_count, next_attempt_at, lease_token, last_error, dead_lettered_at, created_at, updated_at FROM seat_capacity_outbox WHERE operation_token = $1
`

func (q *Queries) GetSeatCapacityIntent(ctx context.Context, operationToken pgtype.UUID) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, getSeatCapacityIntent, operationToken)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const markClaimedSeatCapacityIntentDeadLettered = `-- name: MarkClaimedSeatCapacityIntentDeadLettered :execrows
UPDATE seat_capacity_outbox
SET attempt_count = attempt_count + 1,
    last_error = left($1, 1000),
    dead_lettered_at = now(),
    lease_token = NULL,
    updated_at = now()
WHERE operation_token = $2
  AND action = $3
  AND lease_token = $4
  AND dead_lettered_at IS NULL
`

type MarkClaimedSeatCapacityIntentDeadLetteredParams struct {
	LastError      string      `json:"last_error"`
	OperationToken pgtype.UUID `json:"operation_token"`
	Action         string      `json:"action"`
	LeaseToken     pgtype.UUID `json:"lease_token"`
}

func (q *Queries) MarkClaimedSeatCapacityIntentDeadLettered(ctx context.Context, arg MarkClaimedSeatCapacityIntentDeadLetteredParams) (int64, error) {
	result, err := q.db.Exec(ctx, markClaimedSeatCapacityIntentDeadLettered,
		arg.LastError,
		arg.OperationToken,
		arg.Action,
		arg.LeaseToken,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const markClaimedSeatCapacityIntentFailed = `-- name: MarkClaimedSeatCapacityIntentFailed :execrows
UPDATE seat_capacity_outbox
SET attempt_count = attempt_count + 1,
    last_error = left($1, 1000),
    next_attempt_at = $2,
    lease_token = NULL,
    updated_at = now()
WHERE operation_token = $3
  AND action = $4
  AND lease_token = $5
`

type MarkClaimedSeatCapacityIntentFailedParams struct {
	LastError      string             `json:"last_error"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	Action         string             `json:"action"`
	LeaseToken     pgtype.UUID        `json:"lease_token"`
}

func (q *Queries) MarkClaimedSeatCapacityIntentFailed(ctx context.Context, arg MarkClaimedSeatCapacityIntentFailedParams) (int64, error) {
	result, err := q.db.Exec(ctx, markClaimedSeatCapacityIntentFailed,
		arg.LastError,
		arg.NextAttemptAt,
		arg.OperationToken,
		arg.Action,
		arg.LeaseToken,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const markSeatCapacityIntentDelivered = `-- name: MarkSeatCapacityIntentDelivered :exec
UPDATE seat_capacity_outbox
SET delivered_at = now(),
    attempt_count = attempt_count + 1,
    lease_token = NULL,
    last_error = NULL,
    updated_at = now()
WHERE operation_token = $1 AND action = $2
`

type MarkSeatCapacityIntentDeliveredParams struct {
	OperationToken pgtype.UUID `json:"operation_token"`
	Action         string      `json:"action"`
}

func (q *Queries) MarkSeatCapacityIntentDelivered(ctx context.Context, arg MarkSeatCapacityIntentDeliveredParams) error {
	_, err := q.db.Exec(ctx, markSeatCapacityIntentDelivered, arg.OperationToken, arg.Action)
	return err
}

const markSeatCapacityIntentFailed = `-- name: MarkSeatCapacityIntentFailed :exec
UPDATE seat_capacity_outbox
SET attempt_count = attempt_count + 1,
    last_error = left($1, 1000),
    next_attempt_at = $2,
    lease_token = NULL,
    updated_at = now()
WHERE operation_token = $3 AND action = $4
`

type MarkSeatCapacityIntentFailedParams struct {
	LastError      string             `json:"last_error"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	Action         string             `json:"action"`
}

func (q *Queries) MarkSeatCapacityIntentFailed(ctx context.Context, arg MarkSeatCapacityIntentFailedParams) error {
	_, err := q.db.Exec(ctx, markSeatCapacityIntentFailed,
		arg.LastError,
		arg.NextAttemptAt,
		arg.OperationToken,
		arg.Action,
	)
	return err
}

const prepareSeatCapacityInvitationReleasesForWorkspaceDeletion = `-- name: PrepareSeatCapacityInvitationReleasesForWorkspaceDeletion :exec
INSERT INTO seat_capacity_outbox (
    workspace_id, operation_token, action, subject_id, invitation_id,
    next_attempt_at
)
SELECT invitation.workspace_id, invitation.id, 'release', invitation.id, invitation.id, now()
FROM workspace_invitation AS invitation
WHERE invitation.workspace_id = $1
  AND invitation.status = 'pending'
ON CONFLICT (operation_token) DO UPDATE SET
    action = 'release',
    member_id = NULL,
    delivered_at = NULL,
    attempt_count = 0,
    next_attempt_at = now(),
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE seat_capacity_outbox.action IN (
    'reserve_invitation', 'consume_invitation', 'claim_share_join', 'release'
)
`

func (q *Queries) PrepareSeatCapacityInvitationReleasesForWorkspaceDeletion(ctx context.Context, workspaceID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, prepareSeatCapacityInvitationReleasesForWorkspaceDeletion, workspaceID)
	return err
}

const prepareSeatCapacityMemberReleasesForWorkspaceDeletion = `-- name: PrepareSeatCapacityMemberReleasesForWorkspaceDeletion :exec
INSERT INTO seat_capacity_outbox (
    workspace_id, operation_token, action, member_id, next_attempt_at
)
SELECT m.workspace_id, gen_random_uuid(), 'release_member', m.id, now()
FROM member AS m
WHERE m.workspace_id = $1
  AND NOT EXISTS (
      SELECT 1
      FROM seat_capacity_outbox AS existing
      WHERE existing.workspace_id = m.workspace_id
        AND existing.member_id = m.id
        AND existing.action = 'release_member'
  )
`

func (q *Queries) PrepareSeatCapacityMemberReleasesForWorkspaceDeletion(ctx context.Context, workspaceID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, prepareSeatCapacityMemberReleasesForWorkspaceDeletion, workspaceID)
	return err
}

const prepareSeatCapacityOperationReleasesForWorkspaceDeletion = `-- name: PrepareSeatCapacityOperationReleasesForWorkspaceDeletion :exec
UPDATE seat_capacity_outbox
SET action = 'release',
    member_id = NULL,
    delivered_at = NULL,
    attempt_count = 0,
    next_attempt_at = now(),
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE workspace_id = $1
  AND action NOT IN ('confirm', 'release_member')
`

// Workspace teardown commits these compensations atomically with removal of
// the product rows. The FK-free outbox intentionally survives so Cloud can be
// reconciled after the local workspace no longer exists.
func (q *Queries) PrepareSeatCapacityOperationReleasesForWorkspaceDeletion(ctx context.Context, workspaceID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, prepareSeatCapacityOperationReleasesForWorkspaceDeletion, workspaceID)
	return err
}

const transitionClaimedSeatCapacityIntent = `-- name: TransitionClaimedSeatCapacityIntent :execrows
UPDATE seat_capacity_outbox
SET action = $1,
    member_id = $2,
    delivered_at = NULL,
    attempt_count = 0,
    next_attempt_at = $3,
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE operation_token = $4
  AND action = $5
  AND lease_token = $6
`

type TransitionClaimedSeatCapacityIntentParams struct {
	NextAction     string             `json:"next_action"`
	MemberID       pgtype.UUID        `json:"member_id"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	CurrentAction  string             `json:"current_action"`
	LeaseToken     pgtype.UUID        `json:"lease_token"`
}

func (q *Queries) TransitionClaimedSeatCapacityIntent(ctx context.Context, arg TransitionClaimedSeatCapacityIntentParams) (int64, error) {
	result, err := q.db.Exec(ctx, transitionClaimedSeatCapacityIntent,
		arg.NextAction,
		arg.MemberID,
		arg.NextAttemptAt,
		arg.OperationToken,
		arg.CurrentAction,
		arg.LeaseToken,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const transitionSeatCapacityIntent = `-- name: TransitionSeatCapacityIntent :execrows
UPDATE seat_capacity_outbox
SET action = $1,
    member_id = $2,
    delivered_at = NULL,
    attempt_count = 0,
    next_attempt_at = $3,
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE operation_token = $4
  AND action = $5
`

type TransitionSeatCapacityIntentParams struct {
	NextAction     string             `json:"next_action"`
	MemberID       pgtype.UUID        `json:"member_id"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	CurrentAction  string             `json:"current_action"`
}

func (q *Queries) TransitionSeatCapacityIntent(ctx context.Context, arg TransitionSeatCapacityIntentParams) (int64, error) {
	result, err := q.db.Exec(ctx, transitionSeatCapacityIntent,
		arg.NextAction,
		arg.MemberID,
		arg.NextAttemptAt,
		arg.OperationToken,
		arg.CurrentAction,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const upsertSeatCapacityIntent = `-- name: UpsertSeatCapacityIntent :one
INSERT INTO seat_capacity_outbox (
    workspace_id, operation_token, action, subject_id, member_id,
    invitation_id, share_link_id, user_id, expires_at, next_attempt_at
) VALUES (
    $1, $2, $3,
    $4, $5, $6,
    $7, $8, $9,
    $10
)
ON CONFLICT (operation_token) DO UPDATE SET
    action = EXCLUDED.action,
    subject_id = EXCLUDED.subject_id,
    member_id = EXCLUDED.member_id,
    invitation_id = EXCLUDED.invitation_id,
    share_link_id = EXCLUDED.share_link_id,
    user_id = EXCLUDED.user_id,
    expires_at = EXCLUDED.expires_at,
    attempt_count = CASE
        WHEN seat_capacity_outbox.dead_lettered_at IS NOT NULL THEN 0
        WHEN seat_capacity_outbox.action <> EXCLUDED.action THEN 0
        ELSE seat_capacity_outbox.attempt_count
    END,
    delivered_at = CASE
        WHEN seat_capacity_outbox.action = EXCLUDED.action THEN seat_capacity_outbox.delivered_at
        ELSE NULL
    END,
    next_attempt_at = EXCLUDED.next_attempt_at,
    lease_token = NULL,
    last_error = NULL,
    dead_lettered_at = NULL,
    updated_at = now()
WHERE seat_capacity_outbox.action = EXCLUDED.action
   OR (seat_capacity_outbox.action = 'reserve_invitation' AND EXCLUDED.action = 'consume_invitation')
RETURNING workspace_id, operation_token, action, subject_id, member_id, invitation_id, share_link_id, user_id, expires_at, delivered_at, attempt_count, next_attempt_at, lease_token, last_error, dead_lettered_at, created_at, updated_at
`

type UpsertSeatCapacityIntentParams struct {
	WorkspaceID    pgtype.UUID        `json:"workspace_id"`
	OperationToken pgtype.UUID        `json:"operation_token"`
	Action         string             `json:"action"`
	SubjectID      pgtype.UUID        `json:"subject_id"`
	MemberID       pgtype.UUID        `json:"member_id"`
	InvitationID   pgtype.UUID        `json:"invitation_id"`
	ShareLinkID    pgtype.UUID        `json:"share_link_id"`
	UserID         pgtype.UUID        `json:"user_id"`
	ExpiresAt      pgtype.Timestamptz `json:"expires_at"`
	NextAttemptAt  pgtype.Timestamptz `json:"next_attempt_at"`
}

func (q *Queries) UpsertSeatCapacityIntent(ctx context.Context, arg UpsertSeatCapacityIntentParams) (SeatCapacityOutbox, error) {
	row := q.db.QueryRow(ctx, upsertSeatCapacityIntent,
		arg.WorkspaceID,
		arg.OperationToken,
		arg.Action,
		arg.SubjectID,
		arg.MemberID,
		arg.InvitationID,
		arg.ShareLinkID,
		arg.UserID,
		arg.ExpiresAt,
		arg.NextAttemptAt,
	)
	var i SeatCapacityOutbox
	err := row.Scan(
		&i.WorkspaceID,
		&i.OperationToken,
		&i.Action,
		&i.SubjectID,
		&i.MemberID,
		&i.InvitationID,
		&i.ShareLinkID,
		&i.UserID,
		&i.ExpiresAt,
		&i.DeliveredAt,
		&i.AttemptCount,
		&i.NextAttemptAt,
		&i.LeaseToken,
		&i.LastError,
		&i.DeadLetteredAt,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}