-- Inou Old Database Schema (DICOM tables)
-- Deprecated: 2025-12-29
-- Migrated to entries table

--------------------------------------------------------------------------------
-- PROFILES (now: dossiers)
--------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS profiles (
    guid TEXT PRIMARY KEY,
    name TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

--------------------------------------------------------------------------------
-- STUDIES (now: entries with category='imaging', type='study')
--------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS studies (
    guid TEXT PRIMARY KEY,
    profile_guid TEXT REFERENCES profiles(guid),
    study_instance_uid TEXT UNIQUE,
    patient_name TEXT,
    patient_dob TEXT,
    patient_sex TEXT,
    patient_age TEXT,
    study_date TEXT,
    study_time TEXT,
    study_desc TEXT,
    institution TEXT,
    accession_number TEXT,
    referring_physician TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_studies_profile ON studies(profile_guid);

--------------------------------------------------------------------------------
-- SERIES (now: entries with category='series', parent_id=study)
--------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS series (
    guid TEXT PRIMARY KEY,
    study_guid TEXT REFERENCES studies(guid),
    series_instance_uid TEXT,
    series_number INTEGER,
    series_desc TEXT,
    modality TEXT,
    body_part TEXT,
    protocol_name TEXT,
    manufacturer TEXT,
    model TEXT,
    magnetic_field_strength TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(series_instance_uid, series_desc)
);

CREATE INDEX IF NOT EXISTS idx_series_study ON series(study_guid);

--------------------------------------------------------------------------------
-- SLICES (now: entries with category='slice', parent_id=series)
--------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS slices (
    guid TEXT PRIMARY KEY,
    series_guid TEXT REFERENCES series(guid),
    sop_instance_uid TEXT,
    instance_number INTEGER,           -- maps to: ordinal
    slice_location REAL,
    slice_thickness REAL,
    rows INTEGER,
    cols INTEGER,
    pixel_spacing_row REAL,
    pixel_spacing_col REAL,
    bits_allocated INTEGER,
    bits_stored INTEGER,
    high_bit INTEGER,
    pixel_representation INTEGER,
    window_center REAL,
    window_width REAL,
    window_center_2 REAL,
    window_width_2 REAL,
    rescale_intercept REAL,
    rescale_slope REAL,
    image_position_x REAL,
    image_position_y REAL,
    image_position_z REAL,
    image_orientation TEXT,
    orientation_type TEXT,             -- maps to: type (axial/sagittal/coronal)
    tr REAL,
    te REAL,
    ti REAL,
    flip_angle REAL,
    echo_number INTEGER,
    spacing_between_slices REAL,
    acquisition_time TEXT,
    content_time TEXT,
    photometric_interpretation TEXT,
    samples_per_pixel INTEGER,
    pixel_min INTEGER,
    pixel_max INTEGER,
    raw_path TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_slices_series ON slices(series_guid);

--------------------------------------------------------------------------------
-- MIGRATION MAPPING
--------------------------------------------------------------------------------
-- studies.guid              -> entries.entry_id (hash of guid)
-- studies.profile_guid      -> entries.dossier_id
-- studies.study_instance_uid-> entries.value (for dedup)
-- studies.patient_name      -> entries.tags
-- studies.*                 -> entries.data (JSON)
-- 
-- series.guid               -> entries.entry_id
-- series.study_guid         -> entries.parent_id
-- series.series_number      -> entries.ordinal
-- series.modality           -> entries.type
-- series.series_desc        -> entries.tags
-- series.*                  -> entries.data (JSON)
--
-- slices.guid               -> entries.entry_id
-- slices.series_guid        -> entries.parent_id
-- slices.instance_number    -> entries.ordinal (CRITICAL for ordering)
-- slices.orientation_type   -> entries.type
-- slices.sop_instance_uid   -> entries.value
-- slices.*                  -> entries.data (JSON)
