-- name: CreateClient :one
INSERT INTO clients (name) VALUES ($1) RETURNING id;

-- name: GetClient :one
SELECT c.id, c.name, coalesce(cs.canSync, false) as canSync
    FROM clients as c
    LEFT JOIN (
        SELECT clientId, canSync
        FROM clientCanSync
        WHERE clientId = $1
        ORDER BY addedAt DESC
        LIMIT 1
    ) as cs on cs.clientId = c.id
    WHERE c.id = $1;

-- name: UpdateClient :exec
UPDATE clients SET name = $1 WHERE id = $2;

-- name: AddClientCanSync :exec
INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2);