-- Extend ai_corrections with image reference and add a local, versioned training bank.
-- BUILD_TRAINING_SAMPLE banks here instead of calling external AAMOS/Gemini runTask.

ALTER TABLE ai_corrections
  ADD COLUMN image_s3_key TEXT,
  ADD COLUMN proposal JSONB;

CREATE TABLE ai_training_bank (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  correction_id UUID NOT NULL REFERENCES ai_corrections(id) ON DELETE CASCADE,
  scan_job_id UUID REFERENCES scan_jobs(id) ON DELETE SET NULL,
  version TEXT NOT NULL DEFAULT 'v1',
  task_type TEXT NOT NULL,
  image_s3_key TEXT,
  proposal JSONB NOT NULL,
  action TEXT NOT NULL,
  corrected JSONB,
  model_version TEXT,
  prompt_version TEXT,
  consent_snapshot JSONB NOT NULL DEFAULT '{}',
  exported_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX ai_training_bank_version_idx ON ai_training_bank(version, task_type);
CREATE INDEX ai_training_bank_scan_job_idx ON ai_training_bank(scan_job_id);
CREATE INDEX ai_training_bank_created_at_idx ON ai_training_bank(created_at);
