-- 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: GetTextractOutputCurrentPart :one
WITH client as (
    select id from clients where id = @clientId
),
parts as (
    SELECT extract.part
        FROM client as c
        JOIN documents as docs on docs.clientId = c.id
        JOIN documentCleans as clean on docs.id = clean.documentId
        JOIN documentTextExtractions as extract
            on extract.cleanId = clean.id
        WHERE date(extract.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 extract.part
        FROM client as c
        JOIN documents as docs on docs.clientId = c.id
        JOIN documentCleans as clean on docs.id = clean.documentId
        JOIN documentTextExtractions extract ON extract.cleanId = clean.id
        WHERE date(extract.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: AddDocumentText :one
INSERT INTO documentTextExtractions
    (cleanId, bucket, key, hash, createdAt, part)
    VALUES ($1, $2, $3, $4, $5, $6)
    returning id;

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

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