/**
 * Postgres enum types, derived from the shared ontology in `@pig/core`.
 *
 * Defining them here from the same constant arrays the application uses means a
 * stage can never mean one thing in the database and another in the UI. Adding
 * a value is a migration, which is the correct amount of friction for changing
 * the vocabulary of the business.
 */
import { pgEnum } from 'drizzle-orm/pg-core';
import {
  ACCOUNT_SIDES,
  ACTIVITY_TYPES,
  AFFILIATION_KINDS,
  AGENT_TASK_KINDS,
  AGENT_TASK_OUTCOMES,
  CONFIDENCE_GRADES,
  CONTRACT_STATUSES,
  CONTRACT_TYPES,
  CUSTOMER_SEGMENTS,
  DEMAND_STAGES,
  FACT_BANDS,
  FACT_STATUSES,
  GPU_SOCKETS,
  INTERCONNECT_TYPES,
  PRODUCT_LINES,
  RECORD_SOURCES,
  SECURITY_TIERS,
  SLA_KINDS,
  SLA_METRICS,
  STOCK_STATUSES,
  SUPPLIER_TYPES,
  SUPPLY_STAGES,
  TEAM_ROLES,
  TEAMS,
} from '@pig/core';

// Drizzle's pgEnum wants a mutable, non-empty tuple; the ontology exports
// readonly arrays. The cast is safe because every ontology constant is a
// non-empty literal array — but TypeScript cannot prove non-emptiness from the
// array type alone, hence the widening step.
const mut = <T extends readonly [string, ...string[]]>(v: T) =>
  [...v] as [T[number], ...T[number][]];

export const teamEnum = pgEnum('pig_team', mut(TEAMS));
export const teamRoleEnum = pgEnum('pig_team_role', mut(TEAM_ROLES));

export const accountSideEnum = pgEnum('pig_account_side', mut(ACCOUNT_SIDES));
export const supplierTypeEnum = pgEnum('pig_supplier_type', mut(SUPPLIER_TYPES));
export const customerSegmentEnum = pgEnum('pig_customer_segment', mut(CUSTOMER_SEGMENTS));
export const productLineEnum = pgEnum('pig_product_line', mut(PRODUCT_LINES));

export const demandStageEnum = pgEnum('pig_demand_stage', mut(DEMAND_STAGES));
export const supplyStageEnum = pgEnum('pig_supply_stage', mut(SUPPLY_STAGES));

export const gpuSocketEnum = pgEnum('pig_gpu_socket', mut(GPU_SOCKETS));
export const interconnectTypeEnum = pgEnum('pig_interconnect_type', mut(INTERCONNECT_TYPES));
export const securityTierEnum = pgEnum('pig_security_tier', mut(SECURITY_TIERS));
export const stockStatusEnum = pgEnum('pig_stock_status', mut(STOCK_STATUSES));

export const contractTypeEnum = pgEnum('pig_contract_type', mut(CONTRACT_TYPES));
export const contractStatusEnum = pgEnum('pig_contract_status', mut(CONTRACT_STATUSES));
export const slaKindEnum = pgEnum('pig_sla_kind', mut(SLA_KINDS));
export const slaMetricEnum = pgEnum('pig_sla_metric', mut(SLA_METRICS));

export const factBandEnum = pgEnum('pig_fact_band', mut(FACT_BANDS));
export const factStatusEnum = pgEnum('pig_fact_status', mut(FACT_STATUSES));

export const activityTypeEnum = pgEnum('pig_activity_type', mut(ACTIVITY_TYPES));
export const agentTaskKindEnum = pgEnum('pig_agent_task_kind', mut(AGENT_TASK_KINDS));
export const agentTaskOutcomeEnum = pgEnum('pig_agent_task_outcome', mut(AGENT_TASK_OUTCOMES));

export const recordSourceEnum = pgEnum('pig_record_source', mut(RECORD_SOURCES));
export const confidenceGradeEnum = pgEnum('pig_confidence_grade', mut(CONFIDENCE_GRADES));
export const affiliationKindEnum = pgEnum('pig_affiliation_kind', mut(AFFILIATION_KINDS));
