-- name: ListQueryRequirementValues :many
WITH latest_versions AS (
    SELECT 
        r.queryId,
        MAX(r.cleanVersion) as max_clean_version,
        MAX(r.textVersion) as max_text_version
    FROM results r
    WHERE r.documentId = $3
        AND r.queryVersion = $2
    GROUP BY r.queryId
)
SELECT rq.requiredQueryId as queryId, r.value, q.type
    FROM requiredQueries as rq
    JOIN results as r on r.queryId = rq.requiredQueryId
    JOIN queries as q on q.id = rq.requiredQueryId
    JOIN latest_versions lv ON lv.queryId = r.queryId
    WHERE rq.queryId = $1 and r.documentId = $3
        and $2 >= rq.addedVersion
        and $2 < COALESCE(rq.removedVersion, $2 + 1)
        and r.queryVersion = $2
        and r.cleanVersion >= $4 and r.textVersion >= $5
        AND r.cleanVersion = lv.max_clean_version 
        AND r.textVersion = lv.max_text_version;

-- name: SetResult :one
INSERT INTO results (queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id;

-- name: GetResultValueWithVersion :one
SELECT id, value FROM results WHERE queryId = $1 and queryVersion = $2 and documentId = $3 and cleanVersion >= $4 and textVersion >= $5;

-- name: ListUnsyncedQueriesByDocId :many
WITH RECURSIVE unsyncedQueries AS (
    SELECT dt.queryId, dt.requiredIds
        from documents as d
        JOIN fullActiveCollectors as c on d.jobId = c.jobId
        JOIN collectorQueryDependencyTree as dt on c.id = dt.collectorId
        LEFT JOIN results as r on r.queryId = dt.queryId
            and r.queryVersion = dt.queryVersion
            and r.cleanVersion >= c.minCleanVersion and r.textVersion >= c.minTextVersion
        where d.id = $1 and r.value is null

    UNION

    SELECT DISTINCT dt.queryId, dt.requiredIds
        FROM unsyncedQueries as u
        JOIN collectorQueryDependencyTree as dt ON u.queryId = any(dt.requiredIds)
)
SELECT * FROM fullActiveQueries 
WHERE id in (SELECT queryId FROM unsyncedQueries);