-- name: CreateApiService :one
INSERT INTO api_services (
    client_id, client_secret, name, description, redirect_uris, scopes, grant_types, is_active
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8
) RETURNING *;

-- name: GetApiServiceCID :one
SELECT * FROM api_services
WHERE client_id = $1
  AND is_active = true
LIMIT 1;

-- name: GetApiServiceId :one
SELECT * FROM api_services
WHERE id = $1
LIMIT 1;

-- name: ListApiServices :many
SELECT * FROM api_services
ORDER BY created_at DESC;

-- name: UpdateApiService :one
UPDATE api_services
SET
    name = $2,
    description = $3,
    redirect_uris = $4,
    scopes = $5,
    grant_types = $6,
    updated_at = NOW()
WHERE client_id = $1
RETURNING *;

-- name: DeactivateApiService :exec
UPDATE api_services
SET is_active = false,
    updated_at = NOW()
WHERE client_id = $1;

-- name: ActivateApiService :exec
UPDATE api_services
SET is_active = true,
    updated_at = NOW()
WHERE client_id = $1;

-- name: UpdateClientSecret :exec
UPDATE api_services
SET client_secret = $2,
    updated_at = NOW()
WHERE client_id = $1;
