-- Entity-relationship knowledge memory tables.
-- Entities are the nodes (people, places, events, things).
-- entity_facts are typed claims about or between entities.
-- entity_photo_links connect entities to specific photos.

CREATE TABLE entities (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    name TEXT NOT NULL,
    entity_type TEXT NOT NULL,          -- 'person' | 'place' | 'event' | 'thing'
    description TEXT NOT NULL DEFAULT '',
    embedding BLOB,                     -- 768-dim f32 vector; nullable if embedding service was unavailable
    confidence REAL NOT NULL DEFAULT 0.5,
    status TEXT NOT NULL DEFAULT 'active',  -- 'active' | 'reviewed' | 'rejected'
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    UNIQUE(name, entity_type)
);

CREATE INDEX idx_entities_type ON entities(entity_type);
CREATE INDEX idx_entities_status ON entities(status);
CREATE INDEX idx_entities_name ON entities(name);

CREATE TABLE entity_facts (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    subject_entity_id INTEGER NOT NULL,
    predicate TEXT NOT NULL,
    object_entity_id INTEGER,           -- nullable: entity-to-entity relationship target
    object_value TEXT,                  -- nullable: free-text attribute value
    source_photo TEXT,                  -- photo path that prompted extraction (injected server-side)
    source_insight_id INTEGER,          -- backfilled after insight is stored
    confidence REAL NOT NULL DEFAULT 0.6,
    status TEXT NOT NULL DEFAULT 'active',  -- 'active' | 'reviewed' | 'rejected'
    created_at BIGINT NOT NULL,
    CONSTRAINT fk_ef_subject FOREIGN KEY (subject_entity_id) REFERENCES entities(id) ON DELETE CASCADE,
    CONSTRAINT fk_ef_object  FOREIGN KEY (object_entity_id)  REFERENCES entities(id) ON DELETE SET NULL,
    CONSTRAINT fk_ef_insight FOREIGN KEY (source_insight_id) REFERENCES photo_insights(id) ON DELETE SET NULL,
    CHECK (object_entity_id IS NOT NULL OR object_value IS NOT NULL)
);

CREATE INDEX idx_entity_facts_subject ON entity_facts(subject_entity_id);
CREATE INDEX idx_entity_facts_predicate ON entity_facts(predicate);
CREATE INDEX idx_entity_facts_status ON entity_facts(status);
CREATE INDEX idx_entity_facts_source_photo ON entity_facts(source_photo);

CREATE TABLE entity_photo_links (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    entity_id INTEGER NOT NULL,
    file_path TEXT NOT NULL,
    role TEXT NOT NULL,                 -- 'subject' | 'location' | 'event' | 'thing'
    CONSTRAINT fk_epl_entity FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE,
    UNIQUE(entity_id, file_path, role)
);

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);
