-- G3.2: Medication dose log
CREATE TABLE IF NOT EXISTS medication_doses (
  id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  medicine_id     UUID NOT NULL,
  family_id       UUID NOT NULL,
  administered_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  administered_by UUID,
  amount_given    TEXT,
  notes           TEXT,
  created_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS medication_doses_medicine_idx ON medication_doses (medicine_id);
CREATE INDEX IF NOT EXISTS medication_doses_family_idx   ON medication_doses (family_id);

-- G3.4: Log corrections (audit trail for edited doses)
CREATE TABLE IF NOT EXISTS log_corrections (
  id               UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  family_id        UUID NOT NULL,
  dose_id          UUID NOT NULL REFERENCES medication_doses(id) ON DELETE CASCADE,
  original_value   JSONB NOT NULL,
  corrected_value  JSONB NOT NULL,
  reason           TEXT,
  corrected_by     UUID,
  created_at       TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS log_corrections_dose_idx ON log_corrections (dose_id);
