import { z } from 'zod'

export const EntityAuditSpecSchema = z.object({
  /** Absolute path to the generated SmartStack project root. */
  projectPath: z.string().min(1),
  /** Override the connection string (else resolved from appsettings*.json in the API project). */
  connectionString: z.string().optional(),
  /** Schemas whose `*Id` columns are audited. Default `['extensions']` (the client/business schema). */
  schemas: z.array(z.string()).default(['extensions']),
  /**
   * Extra column-name patterns (case-insensitive regex sources) to treat as
   * intentionally-non-FK identity/audit columns, ON TOP of the built-in allowlist
   * (`lib/fk-allowlist.ts`). Use to tighten/loosen per project.
   */
  allowlist: z.array(z.string()).default([]),
  /** Write a Markdown verdict to `<projectPath>/.smartstack/_audit/entity-fk-audit.md`. Default true. */
  writeReport: z.boolean().default(true),
  /** Path to the `sqlcmd` executable. Default `sqlcmd` (must be on PATH). */
  sqlcmdPath: z.string().default('sqlcmd'),
})
export type EntityAuditSpec = z.infer<typeof EntityAuditSpecSchema>

export type Classification = 'critical' | 'exempt' | 'review' | 'ok'

export interface ColumnFinding {
  schema: string
  table: string
  column: string
  classification: Classification
  /** "schema.table" of the resolved principal table, when one exists. */
  candidatePrincipal: string | null
  hasFk: boolean
  reason: string
}

export interface EntityAuditReport {
  database: string
  server: string
  schemasAudited: string[]
  totalIdColumns: number
  criticalCount: number
  reviewCount: number
  exemptCount: number
  okCount: number
  /** Non-ok findings (critical / review / exempt), sorted critical-first. */
  findings: ColumnFinding[]
}
