-- name: ListQueryRequirementValues :many
WITH reqQueries as (
    SELECT av.queryId, av.activeVersion, q.queryType
    FROM requiredQueries as rq
    JOIN queryCurrentActiveVersions as av on av.queryId = rq.requiredQueryId
    JOIN queries as q on q.queryId = av.queryId
    WHERE rq.queryId = @queryId
        and isInVersion(@version, rq.addedVersion, rq.removedVersion)
),
docs as (
    SELECT id, clientId
    FROM documents
    WHERE id = @documentId
),
codeVersions as (
    SELECT
        d.id as documentId,
        mcv.minCleanVersion,
        mtv.minTextVersion
    FROM docs as d
    JOIN currentCollectorMinTextVersions as mtv on mtv.clientId = d.clientId
    JOIN currentCollectorMinCleanVersions as mcv on mcv.clientId = d.clientId
),
latestVersions AS (
    SELECT
        r.id,
        rq.queryId,
        rq.queryType,
        r.queryVersion,
        r.textEntryId,
        r.value,
        ROW_NUMBER() OVER (
            PARTITION BY r.queryId
            ORDER BY r.id DESC
        ) as rowNumber
    FROM reqQueries as rq
    JOIN currentTextEntries as cte on cte.documentId = @documentId
    LEFT JOIN results as r ON rq.queryId = r.queryId
        and r.queryVersion = rq.activeVersion
        and cte.id = r.textEntryId
)
SELECT DISTINCT id, queryId, queryType, value
FROM latestVersions
WHERE rowNumber = 1;

-- name: AddResult :one
INSERT INTO results (queryId, value, textEntryId, queryVersion) VALUES ($1, $2, $3, $4) returning id;

-- name: AddResultDependency :exec
INSERT INTO resultDependencies (resultId, requiredResultId) VALUES ($1, $2);

-- name: GetResultValueWithVersion :one
WITH doc as (
    SELECT id, clientId
    FROM documents
    WHERE id = @documentId
)
SELECT r.id, r.value
    FROM doc as d
    JOIN currentTextEntries as cte on cte.documentId = d.id
    LEFT JOIN results as r
        on r.queryId = @queryId
        and r.queryVersion = @queryVersion
        and r.textEntryId = cte.id;

-- name: ListUnsyncedNoDepsQueriesByDocId :many
WITH docs as (
    SELECT id, clientId from documents where id = $1
),
dependency_tree as (
    select q.queryId, q.requiredIds, q.clientId, q.queryVersion
        from docs,
            LATERAL collectorQueryDependencyTreeByClient(docs.clientId) AS q
),
text_entries as (
    select id, documentId from currentTextEntries where documentId = $1
),
unsyncedQueries AS (
    SELECT DISTINCT dt.queryId, dt.requiredIds
        from docs as d
        JOIN dependency_tree as dt on d.clientId = dt.clientId
        JOIN text_entries as cte on cte.documentId = d.id
        LEFT JOIN results as r
            on r.queryId = dt.queryId
            and r.queryVersion = dt.queryVersion
            and cte.id = r.textEntryId
        where r.value is null
)
SELECT DISTINCT queryId FROM unsyncedQueries as baseuq
    WHERE NOT EXISTS (
        SELECT 1 FROM unsyncedQueries as uq WHERE uq.queryId = any(baseuq.requiredIds)
    );
