-- 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, createdAt, part) VALUES
    ($1, $2, $3, $4)
    returning id;

-- name: GetTextractOutputCurrentPart :one
WITH client as (
    select id from clients where id = @clientId
),
parts as (
    SELECT triggers.part
        FROM client as c
        JOIN documents as docs on docs.clientId = c.id
        JOIN documentCleans as clean on docs.id = clean.documentId
        JOIN documentTextTriggerExtractions as triggers
            on triggers.cleanId = clean.id
        WHERE date(triggers.createdAt) = date(@queryDate)
),
max_part as (
    SELECT COALESCE(max(part), 0) as max_part_num FROM parts
)
SELECT
    COALESCE(p.part, 0)::unsignedsmallint as part,
    COUNT(p.part) as count
FROM parts p
RIGHT JOIN max_part mp ON p.part = mp.max_part_num
GROUP BY p.part;

-- name: GetTextOutCurrentPart :one
WITH client as (
    select id from clients where id = @clientId
),
parts as (
    SELECT extractions.part
        FROM client as c
        JOIN documents as docs on docs.clientId = c.id
        JOIN documentCleans as clean on docs.id = clean.documentId
        JOIN documentTextTriggerExtractions as triggers
            on triggers.cleanId = clean.id
        JOIN documentTextExtractionEntries as textEntries
            on textEntries.triggerId = triggers.id
        JOIN documentTextExtractions as extractions
            on extractions.id = textEntries.textId
        WHERE date(extractions.createdAt) = date(@queryDate)
),
max_part as (
    SELECT COALESCE(max(part), 0) as max_part_num FROM parts
)
SELECT
    COALESCE(p.part, 0)::unsignedsmallint as part,
    COUNT(p.part) as count
FROM parts p
RIGHT JOIN max_part mp ON p.part = mp.max_part_num
GROUP BY p.part;

-- 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, createdAt, part)
    VALUES ($1, $2, $3, $4, $5)
    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;
