// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: invoices.sql

package sqlc

import (
	"context"
	"database/sql"
	"time"
)

const clearKVInvoiceHashIndex = `-- name: ClearKVInvoiceHashIndex :exec
DELETE FROM invoice_payment_hashes
`

func (q *Queries) ClearKVInvoiceHashIndex(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, clearKVInvoiceHashIndex)
	return err
}

const deleteCanceledInvoices = `-- name: DeleteCanceledInvoices :execresult
DELETE
FROM invoices
WHERE state = 2
`

func (q *Queries) DeleteCanceledInvoices(ctx context.Context) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteCanceledInvoices)
}

const deleteInvoice = `-- name: DeleteInvoice :execresult
DELETE 
FROM invoices 
WHERE (
    id = $1 OR 
    $1 IS NULL
) AND (
    hash = $2 OR 
    $2 IS NULL
) AND (
    settle_index = $3 OR 
    $3 IS NULL
) AND (
    payment_addr = $4 OR
    $4 IS NULL
)
`

type DeleteInvoiceParams struct {
	AddIndex    sql.NullInt64
	Hash        []byte
	SettleIndex sql.NullInt64
	PaymentAddr []byte
}

func (q *Queries) DeleteInvoice(ctx context.Context, arg DeleteInvoiceParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, deleteInvoice,
		arg.AddIndex,
		arg.Hash,
		arg.SettleIndex,
		arg.PaymentAddr,
	)
}

const fetchPendingInvoices = `-- name: FetchPendingInvoices :many
SELECT
    invoices.id, invoices.hash, invoices.preimage, invoices.settle_index, invoices.settled_at, invoices.memo, invoices.amount_msat, invoices.cltv_delta, invoices.expiry, invoices.payment_addr, invoices.payment_request, invoices.payment_request_hash, invoices.state, invoices.amount_paid_msat, invoices.is_amp, invoices.is_hodl, invoices.is_keysend, invoices.created_at
FROM invoices
WHERE state IN (0, 3) -- 0 = ContractOpen, 3 = ContractAccepted
  AND id > $1
ORDER BY id ASC
LIMIT $2
`

type FetchPendingInvoicesParams struct {
	IDCursor int64
	NumLimit int32
}

// FetchPendingInvoices returns all invoices in a pending state (open or
// accepted). The invoices_state_idx index on the state column makes this a
// fast index scan rather than a full table scan. id_cursor is an exclusive
// lower bound on the primary key used for cursor-based pagination; the caller
// must supply 0 when starting from the beginning.
func (q *Queries) FetchPendingInvoices(ctx context.Context, arg FetchPendingInvoicesParams) ([]Invoice, error) {
	rows, err := q.db.QueryContext(ctx, fetchPendingInvoices, arg.IDCursor, arg.NumLimit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invoice
	for rows.Next() {
		var i Invoice
		if err := rows.Scan(
			&i.ID,
			&i.Hash,
			&i.Preimage,
			&i.SettleIndex,
			&i.SettledAt,
			&i.Memo,
			&i.AmountMsat,
			&i.CltvDelta,
			&i.Expiry,
			&i.PaymentAddr,
			&i.PaymentRequest,
			&i.PaymentRequestHash,
			&i.State,
			&i.AmountPaidMsat,
			&i.IsAmp,
			&i.IsHodl,
			&i.IsKeysend,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const filterInvoicesByAddIndex = `-- name: FilterInvoicesByAddIndex :many
SELECT
    invoices.id, invoices.hash, invoices.preimage, invoices.settle_index, invoices.settled_at, invoices.memo, invoices.amount_msat, invoices.cltv_delta, invoices.expiry, invoices.payment_addr, invoices.payment_request, invoices.payment_request_hash, invoices.state, invoices.amount_paid_msat, invoices.is_amp, invoices.is_hodl, invoices.is_keysend, invoices.created_at
FROM invoices
WHERE id >= $1
ORDER BY id ASC
LIMIT $2
`

type FilterInvoicesByAddIndexParams struct {
	AddIndexGet int64
	NumLimit    int32
}

// FilterInvoicesByAddIndex returns invoices whose add_index (primary key id)
// is greater than or equal to the given value, ordered by id. Because id is
// the primary key, this is always an efficient range scan on the clustered
// index. For cursor-based pagination the caller advances add_index_get to
// last_returned_id + 1 on each subsequent page.
func (q *Queries) FilterInvoicesByAddIndex(ctx context.Context, arg FilterInvoicesByAddIndexParams) ([]Invoice, error) {
	rows, err := q.db.QueryContext(ctx, filterInvoicesByAddIndex, arg.AddIndexGet, arg.NumLimit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invoice
	for rows.Next() {
		var i Invoice
		if err := rows.Scan(
			&i.ID,
			&i.Hash,
			&i.Preimage,
			&i.SettleIndex,
			&i.SettledAt,
			&i.Memo,
			&i.AmountMsat,
			&i.CltvDelta,
			&i.Expiry,
			&i.PaymentAddr,
			&i.PaymentRequest,
			&i.PaymentRequestHash,
			&i.State,
			&i.AmountPaidMsat,
			&i.IsAmp,
			&i.IsHodl,
			&i.IsKeysend,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const filterInvoicesBySettleIndex = `-- name: FilterInvoicesBySettleIndex :many
SELECT
    invoices.id, invoices.hash, invoices.preimage, invoices.settle_index, invoices.settled_at, invoices.memo, invoices.amount_msat, invoices.cltv_delta, invoices.expiry, invoices.payment_addr, invoices.payment_request, invoices.payment_request_hash, invoices.state, invoices.amount_paid_msat, invoices.is_amp, invoices.is_hodl, invoices.is_keysend, invoices.created_at
FROM invoices
WHERE settle_index >= $1
  AND id > $2
ORDER BY id ASC
LIMIT $3
`

type FilterInvoicesBySettleIndexParams struct {
	SettleIndexGet sql.NullInt64
	IDCursor       int64
	NumLimit       int32
}

// FilterInvoicesBySettleIndex returns settled invoices whose settle_index is
// greater than or equal to the given value, ordered by id. The caller must
// always supply a concrete lower bound so the invoices_settle_index_idx index
// can be used. id_cursor is an exclusive lower bound on the primary key used
// for cursor-based pagination; the caller must supply 0 when starting from
// the beginning.
func (q *Queries) FilterInvoicesBySettleIndex(ctx context.Context, arg FilterInvoicesBySettleIndexParams) ([]Invoice, error) {
	rows, err := q.db.QueryContext(ctx, filterInvoicesBySettleIndex, arg.SettleIndexGet, arg.IDCursor, arg.NumLimit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invoice
	for rows.Next() {
		var i Invoice
		if err := rows.Scan(
			&i.ID,
			&i.Hash,
			&i.Preimage,
			&i.SettleIndex,
			&i.SettledAt,
			&i.Memo,
			&i.AmountMsat,
			&i.CltvDelta,
			&i.Expiry,
			&i.PaymentAddr,
			&i.PaymentRequest,
			&i.PaymentRequestHash,
			&i.State,
			&i.AmountPaidMsat,
			&i.IsAmp,
			&i.IsHodl,
			&i.IsKeysend,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const filterInvoicesForward = `-- name: FilterInvoicesForward :many
SELECT
    invoices.id, invoices.hash, invoices.preimage, invoices.settle_index, invoices.settled_at, invoices.memo, invoices.amount_msat, invoices.cltv_delta, invoices.expiry, invoices.payment_addr, invoices.payment_request, invoices.payment_request_hash, invoices.state, invoices.amount_paid_msat, invoices.is_amp, invoices.is_hodl, invoices.is_keysend, invoices.created_at
FROM invoices
WHERE id >= $1
  AND (NOT $2 OR state IN (0, 3)) -- 0 = ContractOpen, 3 = ContractAccepted
  AND created_at >= $3
  AND created_at < $4
ORDER BY id ASC
LIMIT $5
`

type FilterInvoicesForwardParams struct {
	AddIndexGet   int64
	PendingOnly   interface{}
	CreatedAfter  time.Time
	CreatedBefore time.Time
	NumLimit      int32
}

// FilterInvoicesForward returns invoices in ascending id order. All parameters
// are non-nullable so the planner always sees plain range predicates and can
// use the primary-key index. For cursor-based pagination the caller advances
// add_index_get to last_returned_id + 1 on each subsequent page. The caller
// is responsible for supplying Go-side defaults when a filter is not needed:
//
//	add_index_get  → 1                             (first valid invoice id)
//	created_after  → time.Unix(0, 0).UTC()         (epoch – before any invoice)
//	created_before → time.Date(9999, …)             (far future – no upper cap)
//	pending_only   → false                          (include all states)
func (q *Queries) FilterInvoicesForward(ctx context.Context, arg FilterInvoicesForwardParams) ([]Invoice, error) {
	rows, err := q.db.QueryContext(ctx, filterInvoicesForward,
		arg.AddIndexGet,
		arg.PendingOnly,
		arg.CreatedAfter,
		arg.CreatedBefore,
		arg.NumLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invoice
	for rows.Next() {
		var i Invoice
		if err := rows.Scan(
			&i.ID,
			&i.Hash,
			&i.Preimage,
			&i.SettleIndex,
			&i.SettledAt,
			&i.Memo,
			&i.AmountMsat,
			&i.CltvDelta,
			&i.Expiry,
			&i.PaymentAddr,
			&i.PaymentRequest,
			&i.PaymentRequestHash,
			&i.State,
			&i.AmountPaidMsat,
			&i.IsAmp,
			&i.IsHodl,
			&i.IsKeysend,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const filterInvoicesReverse = `-- name: FilterInvoicesReverse :many
SELECT
    invoices.id, invoices.hash, invoices.preimage, invoices.settle_index, invoices.settled_at, invoices.memo, invoices.amount_msat, invoices.cltv_delta, invoices.expiry, invoices.payment_addr, invoices.payment_request, invoices.payment_request_hash, invoices.state, invoices.amount_paid_msat, invoices.is_amp, invoices.is_hodl, invoices.is_keysend, invoices.created_at
FROM invoices
WHERE id <= $1
  AND (NOT $2 OR state IN (0, 3)) -- 0 = ContractOpen, 3 = ContractAccepted
  AND created_at >= $3
  AND created_at < $4
ORDER BY id DESC
LIMIT $5
`

type FilterInvoicesReverseParams struct {
	AddIndexLet   int64
	PendingOnly   interface{}
	CreatedAfter  time.Time
	CreatedBefore time.Time
	NumLimit      int32
}

// FilterInvoicesReverse is the descending counterpart of FilterInvoicesForward.
// It returns invoices in descending id order. For cursor-based pagination the
// caller advances add_index_let to last_returned_id - 1 on each subsequent
// page; pass math.MaxInt64 to start from the most recent invoice. See
// FilterInvoicesForward for the expected Go-side defaults.
func (q *Queries) FilterInvoicesReverse(ctx context.Context, arg FilterInvoicesReverseParams) ([]Invoice, error) {
	rows, err := q.db.QueryContext(ctx, filterInvoicesReverse,
		arg.AddIndexLet,
		arg.PendingOnly,
		arg.CreatedAfter,
		arg.CreatedBefore,
		arg.NumLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invoice
	for rows.Next() {
		var i Invoice
		if err := rows.Scan(
			&i.ID,
			&i.Hash,
			&i.Preimage,
			&i.SettleIndex,
			&i.SettledAt,
			&i.Memo,
			&i.AmountMsat,
			&i.CltvDelta,
			&i.Expiry,
			&i.PaymentAddr,
			&i.PaymentRequest,
			&i.PaymentRequestHash,
			&i.State,
			&i.AmountPaidMsat,
			&i.IsAmp,
			&i.IsHodl,
			&i.IsKeysend,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getInvoiceByAddr = `-- name: GetInvoiceByAddr :one
SELECT i.id, i.hash, i.preimage, i.settle_index, i.settled_at, i.memo, i.amount_msat, i.cltv_delta, i.expiry, i.payment_addr, i.payment_request, i.payment_request_hash, i.state, i.amount_paid_msat, i.is_amp, i.is_hodl, i.is_keysend, i.created_at
FROM invoices i
WHERE i.payment_addr = $1
`

func (q *Queries) GetInvoiceByAddr(ctx context.Context, paymentAddr []byte) (Invoice, error) {
	row := q.db.QueryRowContext(ctx, getInvoiceByAddr, paymentAddr)
	var i Invoice
	err := row.Scan(
		&i.ID,
		&i.Hash,
		&i.Preimage,
		&i.SettleIndex,
		&i.SettledAt,
		&i.Memo,
		&i.AmountMsat,
		&i.CltvDelta,
		&i.Expiry,
		&i.PaymentAddr,
		&i.PaymentRequest,
		&i.PaymentRequestHash,
		&i.State,
		&i.AmountPaidMsat,
		&i.IsAmp,
		&i.IsHodl,
		&i.IsKeysend,
		&i.CreatedAt,
	)
	return i, err
}

const getInvoiceByHash = `-- name: GetInvoiceByHash :one
SELECT i.id, i.hash, i.preimage, i.settle_index, i.settled_at, i.memo, i.amount_msat, i.cltv_delta, i.expiry, i.payment_addr, i.payment_request, i.payment_request_hash, i.state, i.amount_paid_msat, i.is_amp, i.is_hodl, i.is_keysend, i.created_at
FROM invoices i
WHERE i.hash = $1
`

func (q *Queries) GetInvoiceByHash(ctx context.Context, hash []byte) (Invoice, error) {
	row := q.db.QueryRowContext(ctx, getInvoiceByHash, hash)
	var i Invoice
	err := row.Scan(
		&i.ID,
		&i.Hash,
		&i.Preimage,
		&i.SettleIndex,
		&i.SettledAt,
		&i.Memo,
		&i.AmountMsat,
		&i.CltvDelta,
		&i.Expiry,
		&i.PaymentAddr,
		&i.PaymentRequest,
		&i.PaymentRequestHash,
		&i.State,
		&i.AmountPaidMsat,
		&i.IsAmp,
		&i.IsHodl,
		&i.IsKeysend,
		&i.CreatedAt,
	)
	return i, err
}

const getInvoiceBySetID = `-- name: GetInvoiceBySetID :many
SELECT i.id, i.hash, i.preimage, i.settle_index, i.settled_at, i.memo, i.amount_msat, i.cltv_delta, i.expiry, i.payment_addr, i.payment_request, i.payment_request_hash, i.state, i.amount_paid_msat, i.is_amp, i.is_hodl, i.is_keysend, i.created_at
FROM invoices i
INNER JOIN amp_sub_invoices a 
ON i.id = a.invoice_id AND a.set_id = $1
`

// TODO(ziggie): This query can only return one invoice if the set_id is
// the primary key of amp_sub_invoices table.
func (q *Queries) GetInvoiceBySetID(ctx context.Context, setID []byte) ([]Invoice, error) {
	rows, err := q.db.QueryContext(ctx, getInvoiceBySetID, setID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invoice
	for rows.Next() {
		var i Invoice
		if err := rows.Scan(
			&i.ID,
			&i.Hash,
			&i.Preimage,
			&i.SettleIndex,
			&i.SettledAt,
			&i.Memo,
			&i.AmountMsat,
			&i.CltvDelta,
			&i.Expiry,
			&i.PaymentAddr,
			&i.PaymentRequest,
			&i.PaymentRequestHash,
			&i.State,
			&i.AmountPaidMsat,
			&i.IsAmp,
			&i.IsHodl,
			&i.IsKeysend,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getInvoiceFeatures = `-- name: GetInvoiceFeatures :many
SELECT feature, invoice_id
FROM invoice_features
WHERE invoice_id = $1
`

func (q *Queries) GetInvoiceFeatures(ctx context.Context, invoiceID int64) ([]InvoiceFeature, error) {
	rows, err := q.db.QueryContext(ctx, getInvoiceFeatures, invoiceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []InvoiceFeature
	for rows.Next() {
		var i InvoiceFeature
		if err := rows.Scan(&i.Feature, &i.InvoiceID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getInvoiceHTLCCustomRecords = `-- name: GetInvoiceHTLCCustomRecords :many
SELECT ihcr.htlc_id, key, value
FROM invoice_htlcs ih JOIN invoice_htlc_custom_records ihcr ON ih.id=ihcr.htlc_id 
WHERE ih.invoice_id = $1
`

type GetInvoiceHTLCCustomRecordsRow struct {
	HtlcID int64
	Key    int64
	Value  []byte
}

func (q *Queries) GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error) {
	rows, err := q.db.QueryContext(ctx, getInvoiceHTLCCustomRecords, invoiceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetInvoiceHTLCCustomRecordsRow
	for rows.Next() {
		var i GetInvoiceHTLCCustomRecordsRow
		if err := rows.Scan(&i.HtlcID, &i.Key, &i.Value); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getInvoiceHTLCs = `-- name: GetInvoiceHTLCs :many
SELECT id, chan_id, htlc_id, amount_msat, total_mpp_msat, accept_height, accept_time, expiry_height, state, resolve_time, invoice_id
FROM invoice_htlcs
WHERE invoice_id = $1
`

func (q *Queries) GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error) {
	rows, err := q.db.QueryContext(ctx, getInvoiceHTLCs, invoiceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []InvoiceHtlc
	for rows.Next() {
		var i InvoiceHtlc
		if err := rows.Scan(
			&i.ID,
			&i.ChanID,
			&i.HtlcID,
			&i.AmountMsat,
			&i.TotalMppMsat,
			&i.AcceptHeight,
			&i.AcceptTime,
			&i.ExpiryHeight,
			&i.State,
			&i.ResolveTime,
			&i.InvoiceID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getKVInvoicePaymentHashByAddIndex = `-- name: GetKVInvoicePaymentHashByAddIndex :one
SELECT hash
FROM invoice_payment_hashes
WHERE add_index = $1
`

func (q *Queries) GetKVInvoicePaymentHashByAddIndex(ctx context.Context, addIndex int64) ([]byte, error) {
	row := q.db.QueryRowContext(ctx, getKVInvoicePaymentHashByAddIndex, addIndex)
	var hash []byte
	err := row.Scan(&hash)
	return hash, err
}

const insertInvoice = `-- name: InsertInvoice :one
INSERT INTO invoices (
    hash, preimage, memo, amount_msat, cltv_delta, expiry, payment_addr, 
    payment_request, payment_request_hash, state, amount_paid_msat, is_amp,
    is_hodl, is_keysend, created_at
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
) RETURNING id
`

type InsertInvoiceParams struct {
	Hash               []byte
	Preimage           []byte
	Memo               sql.NullString
	AmountMsat         int64
	CltvDelta          sql.NullInt32
	Expiry             int32
	PaymentAddr        []byte
	PaymentRequest     sql.NullString
	PaymentRequestHash []byte
	State              int16
	AmountPaidMsat     int64
	IsAmp              bool
	IsHodl             bool
	IsKeysend          bool
	CreatedAt          time.Time
}

func (q *Queries) InsertInvoice(ctx context.Context, arg InsertInvoiceParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, insertInvoice,
		arg.Hash,
		arg.Preimage,
		arg.Memo,
		arg.AmountMsat,
		arg.CltvDelta,
		arg.Expiry,
		arg.PaymentAddr,
		arg.PaymentRequest,
		arg.PaymentRequestHash,
		arg.State,
		arg.AmountPaidMsat,
		arg.IsAmp,
		arg.IsHodl,
		arg.IsKeysend,
		arg.CreatedAt,
	)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const insertInvoiceFeature = `-- name: InsertInvoiceFeature :exec
INSERT INTO invoice_features (
    invoice_id, feature
) VALUES (
    $1, $2
)
`

type InsertInvoiceFeatureParams struct {
	InvoiceID int64
	Feature   int32
}

func (q *Queries) InsertInvoiceFeature(ctx context.Context, arg InsertInvoiceFeatureParams) error {
	_, err := q.db.ExecContext(ctx, insertInvoiceFeature, arg.InvoiceID, arg.Feature)
	return err
}

const insertInvoiceHTLC = `-- name: InsertInvoiceHTLC :one
INSERT INTO invoice_htlcs (
    htlc_id, chan_id, amount_msat, total_mpp_msat, accept_height, accept_time,
    expiry_height, state, resolve_time, invoice_id
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
) RETURNING id
`

type InsertInvoiceHTLCParams struct {
	HtlcID       int64
	ChanID       string
	AmountMsat   int64
	TotalMppMsat sql.NullInt64
	AcceptHeight int32
	AcceptTime   time.Time
	ExpiryHeight int32
	State        int16
	ResolveTime  sql.NullTime
	InvoiceID    int64
}

func (q *Queries) InsertInvoiceHTLC(ctx context.Context, arg InsertInvoiceHTLCParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, insertInvoiceHTLC,
		arg.HtlcID,
		arg.ChanID,
		arg.AmountMsat,
		arg.TotalMppMsat,
		arg.AcceptHeight,
		arg.AcceptTime,
		arg.ExpiryHeight,
		arg.State,
		arg.ResolveTime,
		arg.InvoiceID,
	)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const insertInvoiceHTLCCustomRecord = `-- name: InsertInvoiceHTLCCustomRecord :exec
INSERT INTO invoice_htlc_custom_records (
    key, value, htlc_id 
) VALUES (
    $1, $2, $3
)
`

type InsertInvoiceHTLCCustomRecordParams struct {
	Key    int64
	Value  []byte
	HtlcID int64
}

func (q *Queries) InsertInvoiceHTLCCustomRecord(ctx context.Context, arg InsertInvoiceHTLCCustomRecordParams) error {
	_, err := q.db.ExecContext(ctx, insertInvoiceHTLCCustomRecord, arg.Key, arg.Value, arg.HtlcID)
	return err
}

const insertKVInvoiceKeyAndAddIndex = `-- name: InsertKVInvoiceKeyAndAddIndex :exec
INSERT INTO invoice_payment_hashes (
    id, add_index
) VALUES (
    $1, $2
)
`

type InsertKVInvoiceKeyAndAddIndexParams struct {
	ID       int64
	AddIndex int64
}

func (q *Queries) InsertKVInvoiceKeyAndAddIndex(ctx context.Context, arg InsertKVInvoiceKeyAndAddIndexParams) error {
	_, err := q.db.ExecContext(ctx, insertKVInvoiceKeyAndAddIndex, arg.ID, arg.AddIndex)
	return err
}

const insertMigratedInvoice = `-- name: InsertMigratedInvoice :one
INSERT INTO invoices (
    hash, preimage, settle_index, settled_at, memo, amount_msat, cltv_delta, 
    expiry, payment_addr, payment_request, payment_request_hash, state, 
    amount_paid_msat, is_amp, is_hodl, is_keysend, created_at
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
) RETURNING id
`

type InsertMigratedInvoiceParams struct {
	Hash               []byte
	Preimage           []byte
	SettleIndex        sql.NullInt64
	SettledAt          sql.NullTime
	Memo               sql.NullString
	AmountMsat         int64
	CltvDelta          sql.NullInt32
	Expiry             int32
	PaymentAddr        []byte
	PaymentRequest     sql.NullString
	PaymentRequestHash []byte
	State              int16
	AmountPaidMsat     int64
	IsAmp              bool
	IsHodl             bool
	IsKeysend          bool
	CreatedAt          time.Time
}

func (q *Queries) InsertMigratedInvoice(ctx context.Context, arg InsertMigratedInvoiceParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, insertMigratedInvoice,
		arg.Hash,
		arg.Preimage,
		arg.SettleIndex,
		arg.SettledAt,
		arg.Memo,
		arg.AmountMsat,
		arg.CltvDelta,
		arg.Expiry,
		arg.PaymentAddr,
		arg.PaymentRequest,
		arg.PaymentRequestHash,
		arg.State,
		arg.AmountPaidMsat,
		arg.IsAmp,
		arg.IsHodl,
		arg.IsKeysend,
		arg.CreatedAt,
	)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const nextInvoiceSettleIndex = `-- name: NextInvoiceSettleIndex :one
UPDATE invoice_sequences SET current_value = current_value + 1
WHERE name = 'settle_index'
RETURNING current_value
`

func (q *Queries) NextInvoiceSettleIndex(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, nextInvoiceSettleIndex)
	var current_value int64
	err := row.Scan(&current_value)
	return current_value, err
}

const setKVInvoicePaymentHash = `-- name: SetKVInvoicePaymentHash :exec
UPDATE invoice_payment_hashes
SET hash = $2
WHERE id = $1
`

type SetKVInvoicePaymentHashParams struct {
	ID   int64
	Hash []byte
}

func (q *Queries) SetKVInvoicePaymentHash(ctx context.Context, arg SetKVInvoicePaymentHashParams) error {
	_, err := q.db.ExecContext(ctx, setKVInvoicePaymentHash, arg.ID, arg.Hash)
	return err
}

const updateInvoiceAmountPaid = `-- name: UpdateInvoiceAmountPaid :execresult
UPDATE invoices
SET amount_paid_msat = $2
WHERE id = $1
`

type UpdateInvoiceAmountPaidParams struct {
	ID             int64
	AmountPaidMsat int64
}

func (q *Queries) UpdateInvoiceAmountPaid(ctx context.Context, arg UpdateInvoiceAmountPaidParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updateInvoiceAmountPaid, arg.ID, arg.AmountPaidMsat)
}

const updateInvoiceHTLC = `-- name: UpdateInvoiceHTLC :exec
UPDATE invoice_htlcs 
SET state=$4, resolve_time=$5
WHERE htlc_id = $1 AND chan_id = $2 AND invoice_id = $3
`

type UpdateInvoiceHTLCParams struct {
	HtlcID      int64
	ChanID      string
	InvoiceID   int64
	State       int16
	ResolveTime sql.NullTime
}

func (q *Queries) UpdateInvoiceHTLC(ctx context.Context, arg UpdateInvoiceHTLCParams) error {
	_, err := q.db.ExecContext(ctx, updateInvoiceHTLC,
		arg.HtlcID,
		arg.ChanID,
		arg.InvoiceID,
		arg.State,
		arg.ResolveTime,
	)
	return err
}

const updateInvoiceHTLCs = `-- name: UpdateInvoiceHTLCs :exec
UPDATE invoice_htlcs 
SET state=$2, resolve_time=$3
WHERE invoice_id = $1 AND resolve_time IS NULL
`

type UpdateInvoiceHTLCsParams struct {
	InvoiceID   int64
	State       int16
	ResolveTime sql.NullTime
}

func (q *Queries) UpdateInvoiceHTLCs(ctx context.Context, arg UpdateInvoiceHTLCsParams) error {
	_, err := q.db.ExecContext(ctx, updateInvoiceHTLCs, arg.InvoiceID, arg.State, arg.ResolveTime)
	return err
}

const updateInvoiceState = `-- name: UpdateInvoiceState :execresult
UPDATE invoices
SET state = $2,
    preimage = COALESCE(preimage, $3),
    settle_index = COALESCE(settle_index, $4),
    settled_at = COALESCE(settled_at, $5)
WHERE id = $1
`

type UpdateInvoiceStateParams struct {
	ID          int64
	State       int16
	Preimage    []byte
	SettleIndex sql.NullInt64
	SettledAt   sql.NullTime
}

func (q *Queries) UpdateInvoiceState(ctx context.Context, arg UpdateInvoiceStateParams) (sql.Result, error) {
	return q.db.ExecContext(ctx, updateInvoiceState,
		arg.ID,
		arg.State,
		arg.Preimage,
		arg.SettleIndex,
		arg.SettledAt,
	)
}
