-- name: InsertGlobalSetting :exec
INSERT INTO global_settings (key, value)
VALUES ($1, $2) ON CONFLICT (key) DO
UPDATE
SET value = EXCLUDED.value;
-- name: GetGlobalSettings :many
SELECT *
FROM global_settings;
-- name: GetGlobalSetting :one
SELECT *
FROM global_settings
WHERE key = $1;
-- name: UpdateGlobalSetting :exec
UPDATE global_settings
SET value = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE key = $1;
-- name: InsertCompanySetting :exec
INSERT INTO company_settings (company_id, key, value)
VALUES ($1, $2, $3) ON CONFLICT (company_id, key) DO
UPDATE
SET value = EXCLUDED.value;
-- name: GetAllCompanySettings :many
SELECT *
FROM company_settings;
-- name: GetCompanySetting :many
SELECT *
FROM company_settings
WHERE company_id = $1;
-- name: GetCompanySettingsByKey :many
SELECT *
FROM company_settings
WHERE key = $1;
-- name: GetOverrideSettings :many
SELECT gs.key,
    gs.created_at,
    gs.updated_at,
    COALESCE(cs.value, gs.value) AS value
FROM global_settings gs
    LEFT JOIN company_settings cs ON cs.key = gs.key
    AND cs.company_id = $1;
-- name: DeleteCompanySetting :exec
DELETE FROM company_settings
WHERE company_id = $1
    AND key = $2;
-- name: DeleteAllCompanySetting :exec
DELETE FROM company_settings
WHERE company_id = $1;