import type { ZodType } from 'zod'
import type { UiGridColumn } from '../ui/adapter/contracts'
export type CrudConcurrencyMode = 'NONE' | 'ETAG_IF_MATCH'
export type CrudIdempotencyMode = 'NONE' | 'IDEMPOTENCY_KEY'
export interface CrudResourceDefinition<TQuery, TRow, TForm> {
  resourceId: string
  querySchemaVersion: string
  responseSchemaVersion: string
  querySchema: ZodType<TQuery>
  rowSchema: ZodType<TRow>
  formSchema: ZodType<TForm>
  columns: readonly UiGridColumn[]
  sensitiveFields: readonly string[]
  permissionPolicy: string
  concurrencyMode: CrudConcurrencyMode
  idempotencyMode: CrudIdempotencyMode
}
export function assertCrudResourceDefinition(definition: CrudResourceDefinition<unknown, unknown, unknown>): void {
  if (!definition.resourceId.trim()) throw new Error('resourceId is required')
  const fields = new Set(definition.columns.map(x => x.field))
  for (const sensitive of definition.sensitiveFields) if (!fields.has(sensitive)) throw new Error(`Sensitive field '${sensitive}' has no grid column contract`)
}
