import { pgTable, varchar, text, integer, bigint, timestamp, boolean, jsonb } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
import { createInsertSchema } from "drizzle-zod";
import type { z } from "zod";

export const breachNotificationDrills = pgTable("breach_notification_drills", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  scenario: text("scenario").notNull(),
  channelsTested: text("channels_tested").notNull(),
  recipientCount: integer("recipient_count").notNull().default(0),
  deliveredCount: integer("delivered_count").notNull().default(0),
  failedCount: integer("failed_count").notNull().default(0),
  avgLatencyMs: integer("avg_latency_ms").notNull().default(0),
  pdpoSlaMinutes: integer("pdpo_sla_minutes").notNull().default(4320),
  passed: integer("passed").notNull().default(0),
  detailsJson: text("details_json"),
  triggeredBy: text("triggered_by").notNull(),
  createdAt: timestamp("created_at").notNull().default(sql`now()`),
});

export const crossBorderTransfers = pgTable("cross_border_transfers", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  sourceCountry: text("source_country").notNull().default("UG"),
  destinationCountry: text("destination_country").notNull(),
  destinationVendor: text("destination_vendor").notNull(),
  dataCategories: text("data_categories").notNull(),
  legalBasis: text("legal_basis").notNull(),
  encryptionMethod: text("encryption_method").notNull(),
  retentionDays: integer("retention_days").notNull(),
  bytesTransferred: bigint("bytes_transferred", { mode: "number" }).notNull().default(0),
  recordCount: integer("record_count").notNull().default(0),
  pdpoSection29Compliant: integer("pdpo_section29_compliant").notNull().default(1),
  notes: text("notes"),
  loggedBy: text("logged_by").notNull(),
  transferredAt: timestamp("transferred_at").notNull().default(sql`now()`),
});

export const slaBreachRecords = pgTable("sla_breach_records", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  tenantId: text("tenant_id").notNull(),
  sloName: text("slo_name").notNull(),
  sloTarget: text("slo_target").notNull(),
  actualValue: text("actual_value").notNull(),
  breachSeverity: text("breach_severity").notNull(),
  breachStartedAt: timestamp("breach_started_at").notNull(),
  breachEndedAt: timestamp("breach_ended_at"),
  durationMinutes: integer("duration_minutes").notNull().default(0),
  creditPercent: integer("credit_percent").notNull().default(0),
  creditAmountUsdMicros: bigint("credit_amount_usd_micros", { mode: "number" }).notNull().default(0),
  creditIssued: integer("credit_issued").notNull().default(0),
  detailsJson: text("details_json"),
  detectedAt: timestamp("detected_at").notNull().default(sql`now()`),
}).enableRLS();

export const serviceCredits = pgTable("service_credits", {
  id: varchar("id").primaryKey().default(sql`gen_random_uuid()`),
  tenantId: text("tenant_id").notNull(),
  breachId: text("breach_id"),
  reason: text("reason").notNull(),
  amountUsdMicros: bigint("amount_usd_micros", { mode: "number" }).notNull(),
  status: text("status").notNull().default("issued"),
  issuedBy: text("issued_by").notNull(),
  issuedAt: timestamp("issued_at").notNull().default(sql`now()`),
  appliedAt: timestamp("applied_at"),
}).enableRLS();

export const insertBreachNotificationDrillSchema = createInsertSchema(breachNotificationDrills).omit({ id: true, createdAt: true });
export const insertCrossBorderTransferSchema = createInsertSchema(crossBorderTransfers).omit({ id: true, transferredAt: true });
export const insertSlaBreachRecordSchema = createInsertSchema(slaBreachRecords).omit({ id: true, detectedAt: true });
export const insertServiceCreditSchema = createInsertSchema(serviceCredits).omit({ id: true, issuedAt: true });

export type BreachNotificationDrill = typeof breachNotificationDrills.$inferSelect;
export type CrossBorderTransfer = typeof crossBorderTransfers.$inferSelect;
export type SlaBreachRecord = typeof slaBreachRecords.$inferSelect;
export type ServiceCredit = typeof serviceCredits.$inferSelect;
export type InsertBreachNotificationDrill = z.infer<typeof insertBreachNotificationDrillSchema>;
export type InsertCrossBorderTransfer = z.infer<typeof insertCrossBorderTransferSchema>;
export type InsertSlaBreachRecord = z.infer<typeof insertSlaBreachRecordSchema>;
export type InsertServiceCredit = z.infer<typeof insertServiceCreditSchema>;
