-- Revert multi-library support.
-- Drops library_id/content_hash/size_bytes, renames rel_path back to the
-- original column names, and drops the libraries table. Rows originally
-- from non-primary libraries (id > 1) would be orphaned, so the rollback
-- keeps only rows from library_id=1.

PRAGMA foreign_keys=OFF;

-- tagged_photo: rel_path → photo_name.
DROP INDEX IF EXISTS idx_tagged_photo_relpath_tag;
DROP INDEX IF EXISTS idx_tagged_photo_rel_path;
ALTER TABLE tagged_photo RENAME COLUMN rel_path TO photo_name;
CREATE INDEX IF NOT EXISTS idx_tagged_photo_photo_name ON tagged_photo(photo_name);
CREATE INDEX IF NOT EXISTS idx_tagged_photo_count ON tagged_photo(photo_name, tag_id);

-- favorites: rel_path → path.
DROP INDEX IF EXISTS idx_favorites_unique;
DROP INDEX IF EXISTS idx_favorites_rel_path;
ALTER TABLE favorites RENAME COLUMN rel_path TO path;
CREATE INDEX IF NOT EXISTS idx_favorites_path ON favorites(path);
CREATE UNIQUE INDEX IF NOT EXISTS idx_favorites_unique ON favorites(userid, path);

-- video_preview_clips: drop library_id, rel_path → file_path.
CREATE TABLE video_preview_clips_old (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    file_path TEXT NOT NULL UNIQUE,
    status TEXT NOT NULL DEFAULT 'pending',
    duration_seconds REAL,
    file_size_bytes INTEGER,
    error_message TEXT,
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL
);

INSERT INTO video_preview_clips_old (
    id, file_path, status, duration_seconds, file_size_bytes,
    error_message, created_at, updated_at
)
SELECT
    id, rel_path, status, duration_seconds, file_size_bytes,
    error_message, created_at, updated_at
FROM video_preview_clips
WHERE library_id = 1;

DROP TABLE video_preview_clips;
ALTER TABLE video_preview_clips_old RENAME TO video_preview_clips;

CREATE INDEX idx_preview_clips_file_path ON video_preview_clips(file_path);
CREATE INDEX idx_preview_clips_status    ON video_preview_clips(status);

-- entity_photo_links: drop library_id, rel_path → file_path.
CREATE TABLE entity_photo_links_old (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    entity_id INTEGER NOT NULL,
    file_path TEXT NOT NULL,
    role TEXT NOT NULL,
    CONSTRAINT fk_epl_entity FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE,
    UNIQUE(entity_id, file_path, role)
);

INSERT INTO entity_photo_links_old (id, entity_id, file_path, role)
SELECT id, entity_id, rel_path, role
FROM entity_photo_links
WHERE library_id = 1;

DROP TABLE entity_photo_links;
ALTER TABLE entity_photo_links_old RENAME TO entity_photo_links;

CREATE INDEX idx_entity_photo_links_entity ON entity_photo_links(entity_id);
CREATE INDEX idx_entity_photo_links_photo  ON entity_photo_links(file_path);

-- photo_insights: drop library_id, rel_path → file_path.
CREATE TABLE photo_insights_old (
    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,
    training_messages TEXT,
    approved BOOLEAN
);

INSERT INTO photo_insights_old (
    id, file_path, title, summary, generated_at, model_version, is_current,
    training_messages, approved
)
SELECT
    id, rel_path, title, summary, generated_at, model_version, is_current,
    training_messages, approved
FROM photo_insights
WHERE library_id = 1;

DROP TABLE photo_insights;
ALTER TABLE photo_insights_old 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);

-- image_exif: drop library_id/content_hash/size_bytes, rel_path → file_path.
CREATE TABLE image_exif_old (
    id INTEGER PRIMARY KEY NOT NULL,
    file_path TEXT NOT NULL UNIQUE,
    camera_make TEXT,
    camera_model TEXT,
    lens_model TEXT,
    width INTEGER,
    height INTEGER,
    orientation INTEGER,
    gps_latitude REAL,
    gps_longitude REAL,
    gps_altitude REAL,
    focal_length REAL,
    aperture REAL,
    shutter_speed TEXT,
    iso INTEGER,
    date_taken BIGINT,
    created_time BIGINT NOT NULL,
    last_modified BIGINT NOT NULL
);

INSERT INTO image_exif_old (
    id, file_path,
    camera_make, camera_model, lens_model,
    width, height, orientation,
    gps_latitude, gps_longitude, gps_altitude,
    focal_length, aperture, shutter_speed, iso, date_taken,
    created_time, last_modified
)
SELECT
    id, rel_path,
    camera_make, camera_model, lens_model,
    width, height, orientation,
    gps_latitude, gps_longitude, gps_altitude,
    focal_length, aperture, shutter_speed, iso, date_taken,
    created_time, last_modified
FROM image_exif
WHERE library_id = 1;

DROP TABLE image_exif;
ALTER TABLE image_exif_old RENAME TO image_exif;

CREATE INDEX idx_image_exif_file_path  ON image_exif(file_path);
CREATE INDEX idx_image_exif_camera     ON image_exif(camera_make, camera_model);
CREATE INDEX idx_image_exif_gps        ON image_exif(gps_latitude, gps_longitude);
CREATE INDEX idx_image_exif_date_taken ON image_exif(date_taken);
CREATE INDEX idx_image_exif_date_path  ON image_exif(date_taken DESC, file_path);

-- Finally, drop the libraries registry.
DROP TABLE libraries;

PRAGMA foreign_keys=ON;

ANALYZE;
