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

-- name: ListInvoiceItems :many
SELECT * FROM invoice_items;

-- name: CreateInvoiceItem :one
INSERT INTO invoice_items (invoice_id,component_id,original_component_id, required_quantity,actual_quantity, is_substituted, is_short, shortage_quantity, note, metadata)
VALUES (
    sqlc.arg(invoice_id),
    sqlc.arg(component_id),
    sqlc.arg(original_component_id),
    sqlc.arg(required_quantity),
    sqlc.arg(actual_quantity),
    sqlc.arg(is_substituted),
    sqlc.arg(is_short),
    sqlc.arg(shortage_quantity),
    sqlc.arg(note),
    sqlc.arg(metadata)
)
RETURNING *;

-- name: UpdateInvoiceItem :one
UPDATE invoice_items
SET required_quantity = coalesce(sqlc.arg(required_quantity), required_quantity),
    actual_quantity = coalesce(sqlc.arg(actual_quantity), actual_quantity),
    is_substituted = coalesce(sqlc.arg(is_substituted), is_substituted),
    is_short = coalesce(sqlc.arg(is_short), is_short),
    shortage_quantity = coalesce(sqlc.arg(shortage_quantity), shortage_quantity),
    note = coalesce(sqlc.arg(note), note),
    metadata = coalesce(sqlc.arg(metadata), metadata)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteInvoiceItem :execrows
DELETE FROM invoice_items
WHERE id = sqlc.arg(id);
