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

-- name: ListComponentCodes :many
SELECT * FROM component_codes
ORDER BY created_at DESC;

-- name: CreateComponentCode :one
INSERT INTO component_codes (component_id,code, code_type, is_primary,metadata, created_at)
VALUES (
    sqlc.arg(component_id),
    sqlc.arg(code),
    sqlc.arg(code_type),
    sqlc.arg(is_primary),
    sqlc.arg(metadata),
    sqlc.arg(created_at)
)
RETURNING *;

-- name: UpdateComponentCode :one
UPDATE component_codes
SET code = CASE WHEN sqlc.arg(code) = '' THEN code ELSE sqlc.arg(code) END,
    component_id = coalesce(sqlc.arg(component_id), component_id),
    code_type = coalesce(sqlc.arg(code_type), code_type),
    is_primary = coalesce(sqlc.arg(is_primary), is_primary),
    metadata = coalesce(sqlc.arg(metadata), metadata)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteComponentCode :execrows
DELETE FROM component_codes
WHERE id = sqlc.arg(id);
