import { sql } from "drizzle-orm";
import { pgTable, text, varchar, timestamp, integer, real } from "drizzle-orm/pg-core";

// ========================================
// 8 additional pilot-readiness tables
// ========================================

export const chaosDrillRuns = pgTable("chaos_drill_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  scenario: text("scenario").notNull(),
  passed: integer("passed").notNull(),
  faultsInjected: integer("faults_injected").notNull(),
  faultsRecovered: integer("faults_recovered").notNull(),
  recoveryMs: integer("recovery_ms").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const dsarDrillRuns = pgTable("dsar_drill_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  requestType: text("request_type").notNull(),
  passed: integer("passed").notNull(),
  stepsCompleted: integer("steps_completed").notNull(),
  totalSteps: integer("total_steps").notNull(),
  within30Days: integer("within_30_days").notNull(),
  totalDurationMs: integer("total_duration_ms").notNull(),
  stepTimingsJson: text("step_timings_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const redteamProbeRuns = pgTable("redteam_probe_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  passed: integer("passed").notNull(),
  totalProbes: integer("total_probes").notNull(),
  blockedProbes: integer("blocked_probes").notNull(),
  bypassedProbes: integer("bypassed_probes").notNull(),
  scorePercent: real("score_percent").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const sovereigntyTestRuns = pgTable("sovereignty_test_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  passed: integer("passed").notNull(),
  totalChecks: integer("total_checks").notNull(),
  failedChecks: integer("failed_checks").notNull(),
  recordsExamined: integer("records_examined").notNull(),
  outOfBoundsRecords: integer("out_of_bounds_records").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const sloConformanceRuns = pgTable("slo_conformance_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  passed: integer("passed").notNull(),
  endpointsChecked: integer("endpoints_checked").notNull(),
  endpointsPassing: integer("endpoints_passing").notNull(),
  totalRequests: integer("total_requests").notNull(),
  errorRatePercent: real("error_rate_percent").notNull(),
  latencyP50Ms: real("latency_p50_ms").notNull(),
  latencyP95Ms: real("latency_p95_ms").notNull(),
  latencyP99Ms: real("latency_p99_ms").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const pentestRuns = pgTable("pentest_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  passed: integer("passed").notNull(),
  totalProbes: integer("total_probes").notNull(),
  blockedProbes: integer("blocked_probes").notNull(),
  failedProbes: integer("failed_probes").notNull(),
  highSeverityFindings: integer("high_severity_findings").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const cryptoRotationRuns = pgTable("crypto_rotation_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  passed: integer("passed").notNull(),
  stepsCompleted: integer("steps_completed").notNull(),
  totalSteps: integer("total_steps").notNull(),
  encryptionWorking: integer("encryption_working").notNull(),
  decryptionWorking: integer("decryption_working").notNull(),
  oldKeyRetired: integer("old_key_retired").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export const exitPortabilityRuns = pgTable("exit_portability_runs", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  passed: integer("passed").notNull(),
  tablesExported: integer("tables_exported").notNull(),
  totalRecords: integer("total_records").notNull(),
  bundleSizeBytes: integer("bundle_size_bytes").notNull(),
  bundleSha256: text("bundle_sha256").notNull(),
  withinSlaWindow: integer("within_sla_window").notNull(),
  slaWindowSeconds: integer("sla_window_seconds").notNull(),
  durationMs: integer("duration_ms").notNull(),
  detailsJson: text("details_json").notNull(),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`NOW()`),
});

export type ChaosDrillRun = typeof chaosDrillRuns.$inferSelect;
export type DsarDrillRun = typeof dsarDrillRuns.$inferSelect;
export type RedteamProbeRun = typeof redteamProbeRuns.$inferSelect;
export type SovereigntyTestRun = typeof sovereigntyTestRuns.$inferSelect;
export type SloConformanceRun = typeof sloConformanceRuns.$inferSelect;
export type PentestRun = typeof pentestRuns.$inferSelect;
export type CryptoRotationRun = typeof cryptoRotationRuns.$inferSelect;
export type ExitPortabilityRun = typeof exitPortabilityRuns.$inferSelect;
