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

-- name: ListComponents :many
SELECT * FROM components
ORDER BY created_at DESC;

-- name: CreateComponent :one
INSERT INTO components (component_type_id,name, description,unit,min_quantity,metadata, created_at)
VALUES (
    sqlc.arg(component_type_id),
    sqlc.arg(name),
    sqlc.arg(description),
    sqlc.arg(unit),
    sqlc.arg(min_quantity),
    sqlc.arg(metadata),
    sqlc.arg(created_at)
)
RETURNING *;

-- name: UpdateComponent :one
UPDATE components
SET name = CASE WHEN sqlc.arg(name) = '' THEN name ELSE sqlc.arg(name) END,
    component_type_id = coalesce(sqlc.arg(component_type_id), component_type_id),
    description = coalesce(sqlc.arg(description), description),
    unit = coalesce(sqlc.arg(unit), unit),
    min_quantity = coalesce(sqlc.arg(min_quantity), min_quantity),
    metadata = coalesce(sqlc.arg(metadata), metadata),
    updated_at = sqlc.arg(updated_at)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteComponent :execrows
DELETE FROM components
WHERE id = sqlc.arg(id);
