-- Evaluation Rules: Admin-configurable quality gate rules per consulting stage
-- Zero rows = zero checks = current behavior preserved

BEGIN;

CREATE TABLE IF NOT EXISTS evaluation_rules (
  id              uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  tenant_id       uuid,
  stage           varchar(50) NOT NULL,        -- consulting stage (greeting, info_collection, etc.) or '*' for all stages
  rule_type       varchar(50) NOT NULL,        -- FIELD_COMPLETENESS, ASSESSMENT_QUALITY, RESPONSE_LENGTH, MUST_CONTAIN, STAGE_MIN_TURNS, CONVERSION_SIGNAL
  name            varchar(255) NOT NULL,
  description     text,
  config          jsonb NOT NULL DEFAULT '{}', -- rule-specific parameters
  enabled         boolean DEFAULT true,
  priority        integer DEFAULT 0,           -- lower = run first
  failure_action  varchar(20) NOT NULL DEFAULT 'WARN_AND_PASS', -- RETRY, SUPPLEMENT, WARN_AND_PASS, ESCALATE
  created_by      uuid,
  updated_by      uuid,
  created_at      timestamptz DEFAULT now(),
  updated_at      timestamptz DEFAULT now()
);

CREATE INDEX idx_evaluation_rules_tenant ON evaluation_rules(tenant_id);
CREATE INDEX idx_evaluation_rules_tenant_stage ON evaluation_rules(tenant_id, stage, enabled);

COMMIT;
