CREATE TABLE IF NOT EXISTS "eval_results_analytics" (
  "eval_result_id" text PRIMARY KEY NOT NULL,
  "created_at" timestamp with time zone NOT NULL,
  "project_id" text,
  "application_id" text,
  "workspace_id" text,
  "trace_id" text,
  "session_id" text,
  "trace_ids" jsonb DEFAULT '[]'::jsonb,
  "is_redteam" integer DEFAULT 0 NOT NULL,
  "test_run_id" text,
  "eval_id" text,
  "test_idx" integer,
  "failure_reason" integer DEFAULT 0 NOT NULL,
  "error" text,
  "grading_pass" integer DEFAULT 0 NOT NULL,
  "grading_score" real DEFAULT 0 NOT NULL,
  "grading_reason" text,
  "component_pass_count" integer DEFAULT 0 NOT NULL,
  "component_fail_count" integer DEFAULT 0 NOT NULL,
  "metric_keys" jsonb DEFAULT '[]'::jsonb,
  "failed_metric_keys" jsonb DEFAULT '[]'::jsonb,
  "plugin_id" text,
  "plugin_pass" integer,
  "denorm_version" integer DEFAULT 1 NOT NULL,
  CONSTRAINT "eval_results_analytics_eval_result_id_eval_results_id_fk"
    FOREIGN KEY ("eval_result_id") REFERENCES "eval_results"("id") ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS "eval_results_analytics_scope_created_idx"
  ON "eval_results_analytics" ("project_id", "application_id", "created_at");

CREATE INDEX IF NOT EXISTS "eval_results_analytics_trace_id_idx"
  ON "eval_results_analytics" ("trace_id");

CREATE INDEX IF NOT EXISTS "eval_results_analytics_session_id_idx"
  ON "eval_results_analytics" ("session_id");

CREATE INDEX IF NOT EXISTS "eval_results_analytics_plugin_id_idx"
  ON "eval_results_analytics" ("plugin_id");

CREATE TABLE IF NOT EXISTS "eval_result_components" (
  "id" text PRIMARY KEY NOT NULL,
  "eval_result_id" text NOT NULL,
  "component_index" integer NOT NULL,
  "assertion_type" text,
  "assertion_display_label" text,
  "metric_key" text,
  "component_pass" integer DEFAULT 0 NOT NULL,
  "component_score" real DEFAULT 0 NOT NULL,
  "component_reason" text,
  "assertion_expected" text,
  "assertion_actual" text,
  "project_id" text,
  "application_id" text,
  "workspace_id" text,
  "created_at" timestamp with time zone NOT NULL,
  "trace_id" text,
  "session_id" text,
  "trace_ids" jsonb DEFAULT '[]'::jsonb,
  "test_run_id" text,
  "eval_id" text,
  "is_redteam" integer DEFAULT 0 NOT NULL,
  "failure_reason" integer DEFAULT 0 NOT NULL,
  "error" text,
  "plugin_id" text,
  "denorm_version" integer DEFAULT 1 NOT NULL,
  CONSTRAINT "eval_result_components_eval_result_id_eval_results_id_fk"
    FOREIGN KEY ("eval_result_id") REFERENCES "eval_results"("id") ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS "eval_result_components_eval_result_id_idx"
  ON "eval_result_components" ("eval_result_id");

CREATE INDEX IF NOT EXISTS "eval_result_components_scope_created_idx"
  ON "eval_result_components" ("project_id", "created_at");

CREATE INDEX IF NOT EXISTS "eval_result_components_metric_key_idx"
  ON "eval_result_components" ("metric_key");

CREATE INDEX IF NOT EXISTS "eval_result_components_assertion_type_idx"
  ON "eval_result_components" ("assertion_type");

CREATE INDEX IF NOT EXISTS "eval_result_components_trace_id_idx"
  ON "eval_result_components" ("trace_id");

CREATE UNIQUE INDEX IF NOT EXISTS "eval_result_components_eval_result_component_idx"
  ON "eval_result_components" ("eval_result_id", "component_index");
