import type { Selectable } from "kysely";
import type { LixEngine } from "../engine/boot.js";
/**
 * State history table interface for querying historical entity states.
 *
 * This view provides access to entity states at specific points in change set history,
 * with depth-based traversal for blame functionality and diff operations.
 */
export interface StateHistoryTable {
    /** Unique identifier of the entity */
    entity_id: string;
    /** Schema type identifier (e.g., 'paragraph', 'comment') */
    schema_key: string;
    /** File identifier where this entity is stored */
    file_id: string;
    /** Plugin identifier that manages this entity type */
    plugin_key: string;
    /** JSON content of the entity at this point in history */
    snapshot_content: Record<string, any>;
    /** Optional change metadata associated with this historical state. */
    metadata: Record<string, any> | null;
    /** Version of the schema used for this entity */
    schema_version: string;
    /**
     * ID of the change that created this entity state.
     * Join with the `change` table to get created_at, author info, and other change metadata.
     * Example: `JOIN change ON state_history.change_id = change.id`
     */
    change_id: string;
    /**
     * The actual commit ID where this entity state was originally created.
     *
     * This represents the true origin commit for each historical state, making it easy
     * to understand the provenance of each entity state without additional joins.
     *
     * For example, when viewing history you might see:
     * - depth 0: commit_id = 'commit-123' (current state created in commit-123)
     * - depth 2: commit_id = 'commit-100' (historical state created in commit-100)
     *
     * This tells you exactly where each state was created in the commit graph.
     */
    commit_id: string;
    /**
     * The root commit ID used as the starting point for traversing history.
     *
     * When querying history from a specific commit, this field contains that
     * commit ID for all returned rows. Used with `depth` to understand how
     * far back in history each entity state is from this root.
     *
     * For example, if you query `WHERE root_commit_id = 'commit-123'`,
     * all returned rows will have `root_commit_id = 'commit-123'`.
     */
    root_commit_id: string;
    /**
     * Depth of this entity state relative to the root_commit_id.
     * - depth = 0: Current state at the queried commit_id
     * - depth = 1: One commit back in history (parent)
     * - depth = 2: Two commits back in history (grandparent)
     * - etc.
     *
     * Depth is calculated by traversing the commit ancestry graph backwards
     * from the queried commit_id through parent commit edges.
     * Used for blame functionality to show how entities evolved over time.
     */
    depth: number;
}
export type StateHistoryView = Selectable<StateHistoryTable>;
export declare function applyStateHistoryDatabaseSchema(args: {
    engine: Pick<LixEngine, "sqlite">;
}): void;
export declare const STATE_HISTORY_VIEW_SQL = "\nCREATE VIEW IF NOT EXISTS state_history AS\nWITH\n\t-- Inline entity views to avoid preprocessor dependencies\n\t_commit_by_version AS (\n\t\tSELECT \n\t\t\tjson_extract(s.snapshot_content, '$.id') AS id,\n\t\t\tjson_extract(s.snapshot_content, '$.change_set_id') AS change_set_id,\n\t\t\ts.version_id AS lixcol_version_id\n\t\tFROM state_by_version AS s\n\t\tWHERE s.schema_key = 'lix_commit'\n\t\t\tAND s.snapshot_content IS NOT NULL\n\t),\n\t_change_set_element_by_version AS (\n\t\tSELECT\n\t\t\tjson_extract(s.snapshot_content, '$.change_set_id') AS change_set_id,\n\t\t\tjson_extract(s.snapshot_content, '$.change_id') AS change_id,\n\t\t\tjson_extract(s.snapshot_content, '$.entity_id') AS entity_id,\n\t\t\tjson_extract(s.snapshot_content, '$.schema_key') AS schema_key,\n\t\t\tjson_extract(s.snapshot_content, '$.file_id') AS file_id,\n\t\t\ts.version_id AS lixcol_version_id\n\t\tFROM state_by_version AS s\n\t\tWHERE s.schema_key = 'lix_change_set_element'\n\t\t\tAND s.snapshot_content IS NOT NULL\n\t),\n\t_commit_edge_by_version AS (\n\t\tSELECT\n\t\t\tjson_extract(s.snapshot_content, '$.parent_id') AS parent_id,\n\t\t\tjson_extract(s.snapshot_content, '$.child_id') AS child_id,\n\t\t\ts.version_id AS lixcol_version_id\n\t\tFROM state_by_version AS s\n\t\tWHERE s.schema_key = 'lix_commit_edge'\n\t\t\tAND s.snapshot_content IS NOT NULL\n\t),\n\n\t-- Get all changes with their snapshots\n\tall_changes_with_snapshots AS (\n\t\tSELECT ic.id, ic.entity_id, ic.schema_key, ic.file_id, ic.plugin_key,\n\t\t\t   ic.schema_version, ic.created_at,\n\t\t\t   CASE \n\t\t\t     WHEN ic.snapshot_id = 'no-content' THEN NULL\n\t\t\t     ELSE json(s.content)\n\t\t\t   END AS snapshot_content,\n\t\t\t   CASE\n\t\t\t     WHEN ic.metadata IS NULL THEN NULL\n\t\t\t     ELSE json(ic.metadata)\n\t\t\t   END AS metadata\n\t\tFROM lix_internal_change ic\n\t\tLEFT JOIN lix_internal_snapshot s ON ic.snapshot_id = s.id\n\t),\n\n\t-- Fast path for depth = 0 (no recursion, direct commit join)\n\tdepth0_entity_states AS (\n\t\tSELECT \n\t\t\tchg.entity_id,\n\t\t\tchg.schema_key,\n\t\t\tchg.file_id,\n\t\t\tchg.plugin_key,\n\t\t\tchg.snapshot_content,\n\t\t\tchg.metadata,\n\t\t\tchg.schema_version,\n\t\t\tcse.change_id AS target_change_id,\n\t\t\tc.id AS origin_commit_id,\n\t\t\tc.id AS root_commit_id,\n\t\t\t0 AS commit_depth\n\t\tFROM _change_set_element_by_version cse\n\t\tJOIN _commit_by_version c \n\t\t\tON cse.change_set_id = c.change_set_id \n\t\t\tAND c.lixcol_version_id = 'global'\n\t\tJOIN all_changes_with_snapshots chg \n\t\t\tON chg.id = cse.change_id\n\t\tWHERE cse.lixcol_version_id = 'global'\n\t),\n\n\t-- General path for depth > 0 (recursive, ancestors of requested commits)\n\trequested_commits AS (\n\t\tSELECT DISTINCT c.id as commit_id\n\t\tFROM _commit_by_version c\n\t),\n\treachable_commits_from_requested(id, root_commit_id, depth) AS (\n\t\tSELECT commit_id, commit_id as root_commit_id, 0 as depth \n\t\tFROM requested_commits\n\t\tUNION\n\t\tSELECT ce.parent_id, r.root_commit_id, r.depth + 1\n\t\tFROM _commit_edge_by_version ce \n\t\tJOIN reachable_commits_from_requested r ON ce.child_id = r.id\n\t\tWHERE ce.lixcol_version_id = 'global'\n\t),\n\tcommit_changesets AS (\n\t\tSELECT \n\t\t\tc.id as commit_id,\n\t\t\tc.change_set_id as change_set_id,\n\t\t\trc.root_commit_id,\n\t\t\trc.depth as commit_depth\n\t\tFROM _commit_by_version c\n\t\tJOIN reachable_commits_from_requested rc ON c.id = rc.id\n\t\tWHERE c.lixcol_version_id = 'global'\n\t),\n\tcse_in_reachable_commits AS (\n\t\tSELECT cse.entity_id AS target_entity_id,\n\t\t\t   cse.file_id AS target_file_id,\n\t\t\t   cse.schema_key AS target_schema_key, \n\t\t\t   cse.change_id AS target_change_id,\n\t\t\t   cse.change_set_id AS cse_origin_change_set_id,\n\t\t\t   cc.commit_id AS origin_commit_id,\n\t\t\t   cc.root_commit_id,\n\t\t\t   cc.commit_depth,\n\t\t\t   cc.commit_id as depth_commit_id\n\t\tFROM _change_set_element_by_version cse\n\t\tJOIN commit_changesets cc ON cse.change_set_id = cc.change_set_id\n\t\tWHERE cse.lixcol_version_id = 'global'\n\t),\n\tlatest_change_per_entity_per_depth AS (\n\t\tSELECT \n\t\t\tr.target_entity_id,\n\t\t\tr.target_file_id,\n\t\t\tr.target_schema_key,\n\t\t\tr.root_commit_id,\n\t\t\tr.commit_depth,\n\t\t\tr.depth_commit_id,\n\t\t\tMAX(target_change.created_at) as latest_created_at\n\t\tFROM cse_in_reachable_commits r \n\t\tINNER JOIN all_changes_with_snapshots target_change ON r.target_change_id = target_change.id\n\t\tGROUP BY r.target_entity_id, r.target_file_id, r.target_schema_key, r.root_commit_id, r.commit_depth\n\t),\n\tdepthN_entity_states AS (\n\t\tSELECT \n\t\t\ttarget_change.entity_id, \n\t\t\ttarget_change.schema_key, \n\t\t\ttarget_change.file_id,\n\t\t\ttarget_change.plugin_key, \n\t\t\ttarget_change.snapshot_content,\n\t\t\ttarget_change.metadata,\n\t\t\ttarget_change.schema_version,\n\t\t\tr.target_change_id,\n\t\t\tr.origin_commit_id,\n\t\t\tr.root_commit_id,\n\t\t\tlatest.commit_depth\n\t\tFROM latest_change_per_entity_per_depth latest\n\t\tINNER JOIN cse_in_reachable_commits r ON (\n\t\t\tlatest.target_entity_id = r.target_entity_id \n\t\t\tAND latest.target_file_id = r.target_file_id\n\t\t\tAND latest.target_schema_key = r.target_schema_key\n\t\t\tAND latest.root_commit_id = r.root_commit_id\n\t\t\tAND latest.commit_depth = r.commit_depth\n\t\t)\n\t\tINNER JOIN all_changes_with_snapshots target_change ON (\n\t\t\tr.target_change_id = target_change.id\n\t\t\tAND target_change.created_at = latest.latest_created_at\n\t\t)\n\t\tWHERE latest.commit_depth > 0\n\t)\n\nSELECT \n\tes.entity_id,\n\tes.schema_key,\n\tes.file_id,\n\tes.plugin_key,\n\tes.snapshot_content,\n\tes.metadata,\n\tes.schema_version,\n\tes.target_change_id as change_id,\n\tes.origin_commit_id as commit_id,\n\tes.root_commit_id as root_commit_id,\n\tes.commit_depth as depth,\n\t'global' as version_id\nFROM (\n\tSELECT * FROM depth0_entity_states\n\tUNION ALL\n\tSELECT * FROM depthN_entity_states\n) AS es\nWHERE es.snapshot_content IS NOT NULL  -- Exclude deletions for now\nORDER BY es.entity_id, es.commit_depth;\n";
//# sourceMappingURL=schema.d.ts.map