-- name: IsDocumentTextExtracted :one
SELECT EXISTS(
    SELECT 1 FROM currentTextEntries WHERE documentId = @documentId
);

-- name: GetDocumentTextExtractionByHash :one
SELECT id FROM currentTextEntries WHERE cleanId = @cleanEntryId and hash = @hash;

-- name: AddDocumentTextTrigger :one
INSERT INTO documentTextTriggerExtractions (cleanId, version) VALUES ($1, $2) returning id;

-- name: AddDocumentTextTriggerJobId :exec
UPDATE documentTextTriggerExtractions SET textractJobId = @textractJobId WHERE id = @id;

-- name: GetDocumentTextTrigger :one
SELECT dt.id, dt.cleanId, dt.version, dt.textractJobId, dc.documentId
    from documentTextTriggerExtractions as dt
    join documentCleans as dc on dt.cleanId = dc.id
    where dt.id = @triggerId;

-- name: AddDocumentText :one
INSERT INTO documentTextExtractions (bucket, key, hash) VALUES ($1, $2, $3) returning id;

-- name: AddDocumentTextEntry :exec
INSERT INTO documentTextExtractionEntries (textId, triggerId, version) VALUES ($1, $2, $3);

-- name: GetTextEntryByDocId :one
SELECT id, documentId, bucket, key, hash, cleanId,
    triggerId, textractJobId, triggerVersion, extractionVersion
    FROM currentTextEntries
    WHERE documentId = @documentId;
