-- Enable pgvector if not already
CREATE EXTENSION IF NOT EXISTS vector;

-- Memories table (photos with vision metadata)
CREATE TABLE IF NOT EXISTS memories (
  id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  family_id     UUID NOT NULL,
  child_id      UUID,
  title         TEXT,
  description   TEXT,
  taken_at      TIMESTAMPTZ,
  r2_key        TEXT NOT NULL,
  r2_thumbnail_key TEXT,
  mime_type     TEXT,
  size_bytes    INTEGER,
  width         INTEGER,
  height        INTEGER,
  vision_caption TEXT,
  vision_tags   TEXT[],
  vision_embedding VECTOR(1536),
  is_private    BOOLEAN NOT NULL DEFAULT FALSE,
  processing_status TEXT NOT NULL DEFAULT 'uploading'
    CHECK (processing_status IN ('uploading', 'processing', 'ready', 'failed')),
  uploaded_by   UUID,
  created_at    TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS memories_family_idx ON memories (family_id);
CREATE INDEX IF NOT EXISTS memories_child_idx  ON memories (child_id);

-- Attachments table (files linked to log entries, no vision)
CREATE TABLE IF NOT EXISTS attachments (
  id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  family_id       UUID NOT NULL,
  log_entry_id    UUID,
  r2_key          TEXT NOT NULL,
  r2_thumbnail_key TEXT,
  mime_type       TEXT,
  size_bytes      INTEGER,
  uploaded_by     UUID,
  created_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS attachments_family_idx ON attachments (family_id);
