-- name: CreateClient :exec
INSERT INTO clients (clientId, name) VALUES ($1, $2);

-- name: GetClient :one
SELECT * FROM fullClients WHERE clientId = $1;

-- name: ListClients :many
-- Returns all clients ordered by name
SELECT * FROM fullClients ORDER BY name;

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

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

-- name: GetAllDocumentIDsForClient :many
-- Get all document IDs for a client, used to cascade document deletes during client delete
SELECT id FROM documents WHERE clientId = @client_id;

-- name: DeleteCollectorMinCleanVersions :exec
-- @sqlc-vet-disable
-- Delete collector minimum clean version records for a client
DELETE FROM collectorMinCleanVersions WHERE clientId = @client_id;

-- name: DeleteCollectorActiveVersions :exec
-- @sqlc-vet-disable
-- Delete collector active version records for a client
DELETE FROM collectorActiveVersions WHERE clientId = @client_id;

-- name: DeleteCollectorVersions :exec
-- @sqlc-vet-disable
-- Delete collector version records for a client
DELETE FROM collectorVersions WHERE clientId = @client_id;

-- name: DeleteBatchUploads :exec
-- @sqlc-vet-disable
-- Delete all batch upload records for a client
DELETE FROM batch_uploads WHERE client_id = @client_id;

-- name: DeleteClientCanSync :exec
-- @sqlc-vet-disable
-- Delete clientCanSync records for a client
DELETE FROM clientCanSync WHERE clientId = @client_id;

-- name: DeleteDocumentUploadsForClient :exec
-- @sqlc-vet-disable
-- Delete all documentUploads for a client
DELETE FROM documentUploads WHERE clientId = @client_id;

-- name: DeleteAllFoldersForClient :exec
-- @sqlc-vet-disable
-- Delete all folders for a client. All documents must already be deleted to avoid FK violations.
DELETE FROM folders WHERE clientId = @client_id;

-- name: HardDeleteClient :execrows
-- @sqlc-vet-disable
-- Hard delete the client row itself. Returns the number of rows deleted (0 or 1).
DELETE FROM clients WHERE clientId = @client_id;

-- name: IsClientSynced :one
-- Text extraction has been removed. A client is considered synced when
-- all documents have completed cleaning (clean entry exists OR clean failed).
-- See remove_texttract_and_mocks_plan.md for details.
WITH
docs AS (
    -- Get all documents for this client
    SELECT id, clientId FROM documents WHERE clientId = $1
),
doc_clean_entries as (
    -- Documents with their current clean entries
    SELECT
        d.id AS document_id,
        d.clientId as client_id,
        cte.id AS clean_entry_id,
        cte.fail as clean_fail
    FROM
        docs d
        LEFT JOIN currentCleanEntries cte ON cte.documentId = d.id
)
SELECT (
    -- No documents means client is synced
    NOT EXISTS (SELECT 1 FROM docs)

    OR

    -- All documents have completed processing (clean entry exists OR clean failed)
    NOT EXISTS (
        SELECT 1 FROM doc_clean_entries
        WHERE clean_entry_id IS NULL
    )
)::bool as is_synced;
