export function runSelectSql(whereClause) {
    return `SELECT id, workspace_id, provider, status, title, runtime_session_id, source_work_item_id, source_repository_id, source_preparation_id, source_run_intent_id, branch_plan_id, branch_name, base_branch, cap_policy_json, attempt_count, previous_run_id, created_at, updated_at, last_error
          FROM runs
          ${whereClause}`;
}
export function runEvidenceSelectSql(whereClause) {
    return `SELECT run_id, changed_files_json, validations_json, warnings_json, patch_files_json, created_at, updated_at
          FROM run_proof_evidence
          ${whereClause}`;
}
export function permissionRequestSelectSql(whereClause) {
    return `SELECT id, run_id, workspace_id, runtime_session_id, provider, provider_request_id, provider_permission_id, source_runtime_event_id, request_json, evaluation_json, status, selected_option, opencode_reply, decision_audit_id, last_error, created_at, updated_at, answered_at
          FROM permission_requests
          ${whereClause}`;
}
export function permissionDecisionSelectSql(whereClause) {
    return `SELECT id, workspace_id, run_id, runtime_session_id, scope, effect, option, capabilities_json, pattern, tool_name, tool_title, tool_risk, input_preview, affected_paths_json, source_permission_request_id, audit_id, status, created_at, updated_at, revoked_at, revoke_audit_id, revoke_reason, revoke_source
          FROM permission_decisions
          ${whereClause}`;
}
export function permissionActivityEventSelectSql(whereClause) {
    return `SELECT id, kind, activity_at, source, workspace_id, run_id, runtime_session_id, request_id, decision_id, status, selected_option, opencode_reply, tool_name, tool_title, tool_risk, input_preview, affected_paths_json, external_service_mutation, deny_only_diagnostics_json, error, revoke_reason, revoke_source, created_at
          FROM permission_activity_events
          ${whereClause}`;
}
export function debugRevealEventSelectSql(whereClause) {
    return `SELECT id, workspace_id, reveal_session_id, kind, event_at, expires_at, ttl_seconds, reason_label, surfaces_json, created_at
          FROM debug_reveal_events
          ${whereClause}`;
}
export function buildPermissionRequestActivitySelect(options, kind, idPrefix, atColumn, extraClauses = []) {
    const clauses = ["workspace_id = ?", ...extraClauses];
    const params = [options.workspaceId];
    if (options.runId) {
        clauses.push("run_id = ?");
        params.push(options.runId);
    }
    if (options.cursor) {
        clauses.push(`(${atColumn} > ? OR (${atColumn} = ? AND ('${idPrefix}' || id) > ?))`);
        params.push(options.cursor.at, options.cursor.at, options.cursor.id);
    }
    return {
        sql: `SELECT '${kind}' AS activity_kind,
                 '${idPrefix}' || id AS activity_id,
                 ${atColumn} AS activity_at,
                 id AS id,
                 workspace_id AS workspace_id,
                 run_id AS run_id,
                 runtime_session_id AS runtime_session_id,
                 status AS status,
                 created_at AS created_at,
                 updated_at AS updated_at,
                 provider AS provider,
                 provider_request_id AS provider_request_id,
                 provider_permission_id AS provider_permission_id,
                 source_runtime_event_id AS source_runtime_event_id,
                 request_json AS request_json,
                 evaluation_json AS evaluation_json,
                 selected_option AS selected_option,
                 opencode_reply AS opencode_reply,
                 decision_audit_id AS decision_audit_id,
                 last_error AS last_error,
                 answered_at AS answered_at,
                 NULL AS scope,
                 NULL AS effect,
                 NULL AS option,
                 NULL AS capabilities_json,
                 NULL AS pattern,
                 NULL AS tool_name,
                 NULL AS tool_title,
                 NULL AS tool_risk,
                 NULL AS input_preview,
                 NULL AS affected_paths_json,
                 NULL AS source_permission_request_id,
                 NULL AS audit_id,
                 NULL AS revoked_at,
                 NULL AS revoke_audit_id,
                 NULL AS revoke_reason,
                 NULL AS revoke_source
          FROM permission_requests
          WHERE ${clauses.join(" AND ")}`,
        params,
    };
}
export function buildPermissionDecisionActivitySelect(options, kind, idPrefix, atColumn, extraClauses = []) {
    const clauses = [
        "workspace_id = ?",
        "scope = 'session'",
        "effect = 'allow'",
        "option = 'allow_for_session'",
        ...extraClauses,
    ];
    const params = [options.workspaceId];
    if (options.runId) {
        clauses.push("run_id = ?");
        params.push(options.runId);
    }
    if (options.cursor) {
        clauses.push(`(${atColumn} > ? OR (${atColumn} = ? AND ('${idPrefix}' || id) > ?))`);
        params.push(options.cursor.at, options.cursor.at, options.cursor.id);
    }
    return {
        sql: `SELECT '${kind}' AS activity_kind,
                 '${idPrefix}' || id AS activity_id,
                 ${atColumn} AS activity_at,
                 id AS id,
                 workspace_id AS workspace_id,
                 run_id AS run_id,
                 runtime_session_id AS runtime_session_id,
                 status AS status,
                 created_at AS created_at,
                 updated_at AS updated_at,
                 NULL AS provider,
                 NULL AS provider_request_id,
                 NULL AS provider_permission_id,
                 NULL AS source_runtime_event_id,
                 NULL AS request_json,
                 NULL AS evaluation_json,
                 NULL AS selected_option,
                 NULL AS opencode_reply,
                 NULL AS decision_audit_id,
                 NULL AS last_error,
                 NULL AS answered_at,
                 scope AS scope,
                 effect AS effect,
                 option AS option,
                 capabilities_json AS capabilities_json,
                 pattern AS pattern,
                 tool_name AS tool_name,
                 tool_title AS tool_title,
                 tool_risk AS tool_risk,
                 input_preview AS input_preview,
                 affected_paths_json AS affected_paths_json,
                 source_permission_request_id AS source_permission_request_id,
                 audit_id AS audit_id,
                 revoked_at AS revoked_at,
                 revoke_audit_id AS revoke_audit_id,
                 revoke_reason AS revoke_reason,
                 revoke_source AS revoke_source
          FROM permission_decisions
          WHERE ${clauses.join(" AND ")}`,
        params,
    };
}
