export function applyStateHistoryDatabaseSchema(args) {
    args.engine.sqlite.exec(STATE_HISTORY_VIEW_SQL);
}
// Optimized to keep the generic history view, but add a fast path for depth=0
// to avoid whole-graph recursion for common queries like "WHERE depth = 0".
export const STATE_HISTORY_VIEW_SQL = `
CREATE VIEW IF NOT EXISTS state_history AS
WITH
	-- Inline entity views to avoid preprocessor dependencies
	_commit_by_version AS (
		SELECT 
			json_extract(s.snapshot_content, '$.id') AS id,
			json_extract(s.snapshot_content, '$.change_set_id') AS change_set_id,
			s.version_id AS lixcol_version_id
		FROM state_by_version AS s
		WHERE s.schema_key = 'lix_commit'
			AND s.snapshot_content IS NOT NULL
	),
	_change_set_element_by_version AS (
		SELECT
			json_extract(s.snapshot_content, '$.change_set_id') AS change_set_id,
			json_extract(s.snapshot_content, '$.change_id') AS change_id,
			json_extract(s.snapshot_content, '$.entity_id') AS entity_id,
			json_extract(s.snapshot_content, '$.schema_key') AS schema_key,
			json_extract(s.snapshot_content, '$.file_id') AS file_id,
			s.version_id AS lixcol_version_id
		FROM state_by_version AS s
		WHERE s.schema_key = 'lix_change_set_element'
			AND s.snapshot_content IS NOT NULL
	),
	_commit_edge_by_version AS (
		SELECT
			json_extract(s.snapshot_content, '$.parent_id') AS parent_id,
			json_extract(s.snapshot_content, '$.child_id') AS child_id,
			s.version_id AS lixcol_version_id
		FROM state_by_version AS s
		WHERE s.schema_key = 'lix_commit_edge'
			AND s.snapshot_content IS NOT NULL
	),

	-- Get all changes with their snapshots
	all_changes_with_snapshots AS (
		SELECT ic.id, ic.entity_id, ic.schema_key, ic.file_id, ic.plugin_key,
			   ic.schema_version, ic.created_at,
			   CASE 
			     WHEN ic.snapshot_id = 'no-content' THEN NULL
			     ELSE json(s.content)
			   END AS snapshot_content,
			   CASE
			     WHEN ic.metadata IS NULL THEN NULL
			     ELSE json(ic.metadata)
			   END AS metadata
		FROM lix_internal_change ic
		LEFT JOIN lix_internal_snapshot s ON ic.snapshot_id = s.id
	),

	-- Fast path for depth = 0 (no recursion, direct commit join)
	depth0_entity_states AS (
		SELECT 
			chg.entity_id,
			chg.schema_key,
			chg.file_id,
			chg.plugin_key,
			chg.snapshot_content,
			chg.metadata,
			chg.schema_version,
			cse.change_id AS target_change_id,
			c.id AS origin_commit_id,
			c.id AS root_commit_id,
			0 AS commit_depth
		FROM _change_set_element_by_version cse
		JOIN _commit_by_version c 
			ON cse.change_set_id = c.change_set_id 
			AND c.lixcol_version_id = 'global'
		JOIN all_changes_with_snapshots chg 
			ON chg.id = cse.change_id
		WHERE cse.lixcol_version_id = 'global'
	),

	-- General path for depth > 0 (recursive, ancestors of requested commits)
	requested_commits AS (
		SELECT DISTINCT c.id as commit_id
		FROM _commit_by_version c
	),
	reachable_commits_from_requested(id, root_commit_id, depth) AS (
		SELECT commit_id, commit_id as root_commit_id, 0 as depth 
		FROM requested_commits
		UNION
		SELECT ce.parent_id, r.root_commit_id, r.depth + 1
		FROM _commit_edge_by_version ce 
		JOIN reachable_commits_from_requested r ON ce.child_id = r.id
		WHERE ce.lixcol_version_id = 'global'
	),
	commit_changesets AS (
		SELECT 
			c.id as commit_id,
			c.change_set_id as change_set_id,
			rc.root_commit_id,
			rc.depth as commit_depth
		FROM _commit_by_version c
		JOIN reachable_commits_from_requested rc ON c.id = rc.id
		WHERE c.lixcol_version_id = 'global'
	),
	cse_in_reachable_commits AS (
		SELECT cse.entity_id AS target_entity_id,
			   cse.file_id AS target_file_id,
			   cse.schema_key AS target_schema_key, 
			   cse.change_id AS target_change_id,
			   cse.change_set_id AS cse_origin_change_set_id,
			   cc.commit_id AS origin_commit_id,
			   cc.root_commit_id,
			   cc.commit_depth,
			   cc.commit_id as depth_commit_id
		FROM _change_set_element_by_version cse
		JOIN commit_changesets cc ON cse.change_set_id = cc.change_set_id
		WHERE cse.lixcol_version_id = 'global'
	),
	latest_change_per_entity_per_depth AS (
		SELECT 
			r.target_entity_id,
			r.target_file_id,
			r.target_schema_key,
			r.root_commit_id,
			r.commit_depth,
			r.depth_commit_id,
			MAX(target_change.created_at) as latest_created_at
		FROM cse_in_reachable_commits r 
		INNER JOIN all_changes_with_snapshots target_change ON r.target_change_id = target_change.id
		GROUP BY r.target_entity_id, r.target_file_id, r.target_schema_key, r.root_commit_id, r.commit_depth
	),
	depthN_entity_states AS (
		SELECT 
			target_change.entity_id, 
			target_change.schema_key, 
			target_change.file_id,
			target_change.plugin_key, 
			target_change.snapshot_content,
			target_change.metadata,
			target_change.schema_version,
			r.target_change_id,
			r.origin_commit_id,
			r.root_commit_id,
			latest.commit_depth
		FROM latest_change_per_entity_per_depth latest
		INNER JOIN cse_in_reachable_commits r ON (
			latest.target_entity_id = r.target_entity_id 
			AND latest.target_file_id = r.target_file_id
			AND latest.target_schema_key = r.target_schema_key
			AND latest.root_commit_id = r.root_commit_id
			AND latest.commit_depth = r.commit_depth
		)
		INNER JOIN all_changes_with_snapshots target_change ON (
			r.target_change_id = target_change.id
			AND target_change.created_at = latest.latest_created_at
		)
		WHERE latest.commit_depth > 0
	)

SELECT 
	es.entity_id,
	es.schema_key,
	es.file_id,
	es.plugin_key,
	es.snapshot_content,
	es.metadata,
	es.schema_version,
	es.target_change_id as change_id,
	es.origin_commit_id as commit_id,
	es.root_commit_id as root_commit_id,
	es.commit_depth as depth,
	'global' as version_id
FROM (
	SELECT * FROM depth0_entity_states
	UNION ALL
	SELECT * FROM depthN_entity_states
) AS es
WHERE es.snapshot_content IS NOT NULL  -- Exclude deletions for now
ORDER BY es.entity_id, es.commit_depth;
`;
//# sourceMappingURL=schema.js.map