// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: invites.sql

package dbq

import (
	"context"

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

const createInvite = `-- name: CreateInvite :one
INSERT INTO user_invites (token, invited_by, note, expires_at)
VALUES ($1, $2, $3, $4)
RETURNING token, invited_by, note, created_at, expires_at, redeemed_at, redeemed_by
`

type CreateInviteParams struct {
	Token     string
	InvitedBy pgtype.UUID
	Note      *string
	ExpiresAt pgtype.Timestamptz
}

func (q *Queries) CreateInvite(ctx context.Context, arg CreateInviteParams) (UserInvite, error) {
	row := q.db.QueryRow(ctx, createInvite,
		arg.Token,
		arg.InvitedBy,
		arg.Note,
		arg.ExpiresAt,
	)
	var i UserInvite
	err := row.Scan(
		&i.Token,
		&i.InvitedBy,
		&i.Note,
		&i.CreatedAt,
		&i.ExpiresAt,
		&i.RedeemedAt,
		&i.RedeemedBy,
	)
	return i, err
}

const deleteInvite = `-- name: DeleteInvite :exec
DELETE FROM user_invites WHERE token = $1 AND redeemed_at IS NULL
`

// Admin-revoke. Only deletes unredeemed invites.
func (q *Queries) DeleteInvite(ctx context.Context, token string) error {
	_, err := q.db.Exec(ctx, deleteInvite, token)
	return err
}

const getInviteByToken = `-- name: GetInviteByToken :one
SELECT token, invited_by, note, created_at, expires_at, redeemed_at, redeemed_by FROM user_invites WHERE token = $1
`

func (q *Queries) GetInviteByToken(ctx context.Context, token string) (UserInvite, error) {
	row := q.db.QueryRow(ctx, getInviteByToken, token)
	var i UserInvite
	err := row.Scan(
		&i.Token,
		&i.InvitedBy,
		&i.Note,
		&i.CreatedAt,
		&i.ExpiresAt,
		&i.RedeemedAt,
		&i.RedeemedBy,
	)
	return i, err
}

const listInvitesActive = `-- name: ListInvitesActive :many
SELECT token, invited_by, note, created_at, expires_at, redeemed_at, redeemed_by FROM user_invites
 WHERE redeemed_at IS NULL AND expires_at > now()
 ORDER BY created_at DESC
`

// Active = not redeemed and not expired. Used by the admin invites
// panel as the primary list.
func (q *Queries) ListInvitesActive(ctx context.Context) ([]UserInvite, error) {
	rows, err := q.db.Query(ctx, listInvitesActive)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserInvite
	for rows.Next() {
		var i UserInvite
		if err := rows.Scan(
			&i.Token,
			&i.InvitedBy,
			&i.Note,
			&i.CreatedAt,
			&i.ExpiresAt,
			&i.RedeemedAt,
			&i.RedeemedBy,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listInvitesAll = `-- name: ListInvitesAll :many
SELECT token, invited_by, note, created_at, expires_at, redeemed_at, redeemed_by FROM user_invites
 ORDER BY created_at DESC
 LIMIT $1
`

// Includes redeemed + expired for full audit visibility. Caller
// supplies a LIMIT to bound the response.
func (q *Queries) ListInvitesAll(ctx context.Context, limit int32) ([]UserInvite, error) {
	rows, err := q.db.Query(ctx, listInvitesAll, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserInvite
	for rows.Next() {
		var i UserInvite
		if err := rows.Scan(
			&i.Token,
			&i.InvitedBy,
			&i.Note,
			&i.CreatedAt,
			&i.ExpiresAt,
			&i.RedeemedAt,
			&i.RedeemedBy,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const redeemInvite = `-- name: RedeemInvite :execrows
UPDATE user_invites
   SET redeemed_at = now(), redeemed_by = $2
 WHERE token = $1
   AND redeemed_at IS NULL
   AND expires_at > now()
`

type RedeemInviteParams struct {
	Token      string
	RedeemedBy pgtype.UUID
}

// Atomic claim: returns rows-affected so the caller can detect
// already-redeemed / expired tokens (rows=0) versus successful claim
// (rows=1). Use this from the registration handler.
func (q *Queries) RedeemInvite(ctx context.Context, arg RedeemInviteParams) (int64, error) {
	result, err := q.db.Exec(ctx, redeemInvite, arg.Token, arg.RedeemedBy)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
