-- Convert photo_insights to an append-only history table.
-- SQLite cannot drop a UNIQUE constraint via ALTER TABLE, so we recreate the table.
-- This preserves existing insight IDs so that future entity_facts.source_insight_id
-- FK references remain valid.

CREATE TABLE photo_insights_new (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    file_path TEXT NOT NULL,
    title TEXT NOT NULL,
    summary TEXT NOT NULL,
    generated_at BIGINT NOT NULL,
    model_version TEXT NOT NULL,
    is_current BOOLEAN NOT NULL DEFAULT 0
);

-- Migrate existing rows; mark them all as current (one row per path currently).
INSERT INTO photo_insights_new (id, file_path, title, summary, generated_at, model_version, is_current)
    SELECT id, file_path, title, summary, generated_at, model_version, 1
    FROM photo_insights;

DROP TABLE photo_insights;
ALTER TABLE photo_insights_new RENAME TO photo_insights;

CREATE INDEX idx_photo_insights_file_path ON photo_insights(file_path);
CREATE INDEX idx_photo_insights_current ON photo_insights(file_path, is_current);
