-- name: GetInvoiceByID :one
SELECT * FROM invoices
WHERE id = sqlc.arg(id);

-- name: ListInvoices :many
SELECT * FROM invoices
ORDER BY created_at DESC;

-- name: CreateInvoice :one
INSERT INTO invoices (type, status, invoice_config_id, total_items, note, created_by, approved_by, created_at, metadata)
VALUES (
    sqlc.arg(type),
    sqlc.arg(status),
    sqlc.arg(invoice_config_id),
    sqlc.arg(total_items),
    sqlc.arg(note),
    sqlc.arg(created_by),
    sqlc.arg(approved_by),
    sqlc.arg(created_at),
    sqlc.arg(metadata)
)
RETURNING *;

-- name: UpdateInvoice :one
UPDATE invoices
SET type = coalesce(sqlc.arg(type), type),
    status = coalesce(sqlc.arg(status), status),
    invoice_config_id = coalesce(sqlc.arg(invoice_config_id), invoice_config_id),
    total_items = coalesce(sqlc.arg(total_items), total_items),
    note = coalesce(sqlc.arg(note), note),
    metadata = coalesce(sqlc.arg(metadata), metadata),
    updated_at = sqlc.arg(updated_at)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteInvoice :execrows
DELETE FROM invoices
WHERE id = sqlc.arg(id);


