import { z } from 'zod'
import { defineDomainRecord } from '../helpers'

// ---------------------------------------------------------------------------
// Semantic class enum — one value per status category.
// Every status entry declares which category it belongs to via semanticClass.
// ---------------------------------------------------------------------------

export const StatusSemanticClassSchema = z.enum([
  'delivery.task',
  'delivery.project',
  'delivery.milestone',
  'queue',
  'execution',
  'schedule',
  'schedule.run',
  'request'
])

// ---------------------------------------------------------------------------
// Status entry schema
// ---------------------------------------------------------------------------

export const StatusEntrySchema = z.object({
  id: z.string().trim().min(1).max(100),
  /** Domain-map iteration order. Convention: multiples of 10 (10, 20, 30, ...) to allow easy insertion. */
  order: z.number(),
  label: z.string().trim().min(1).max(120),
  semanticClass: StatusSemanticClassSchema,
  category: z.string().trim().min(1).max(80).optional()
})

// ---------------------------------------------------------------------------
// Domain schema — id-keyed map of status entries
// ---------------------------------------------------------------------------

export const StatusesDomainSchema = z
  .record(z.string(), StatusEntrySchema)
  .refine((record) => Object.entries(record).every(([key, entry]) => entry.id === key), {
    message: 'Each status entry id must match its map key'
  })
  .default({})

// ---------------------------------------------------------------------------
// Seed — covers all status enums in scope (delivery, queue, execution,
// schedule, schedule.run, request). Delivery project/milestone statuses are
// included here as semantic references; they mirror delivery.ts but live in
// the statuses domain as the vibe-readable registry.
// ---------------------------------------------------------------------------

/** Validate and return a single status entry. */
export function defineStatus(entry: z.input<typeof StatusEntrySchema>): StatusEntry {
  return StatusEntrySchema.parse(entry)
}

/** Validate and return an id-keyed map of status entries. */
export function defineStatuses(entries: readonly z.input<typeof StatusEntrySchema>[]): StatusesDomain {
  return defineDomainRecord(StatusEntrySchema, entries)
}

export type StatusEntry = z.infer<typeof StatusEntrySchema>
export type StatusesDomain = z.infer<typeof StatusesDomainSchema>
export type StatusSemanticClass = z.infer<typeof StatusSemanticClassSchema>

export const DEFAULT_ORGANIZATION_MODEL_STATUSES: StatusesDomain = {
  // --- delivery.task (TaskStatus — 9 values) ---
  'delivery.task.planned': {
    id: 'delivery.task.planned',
    order: 10,
    label: 'Planned',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.in_progress': {
    id: 'delivery.task.in_progress',
    order: 20,
    label: 'In Progress',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.blocked': {
    id: 'delivery.task.blocked',
    order: 30,
    label: 'Blocked',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.submitted': {
    id: 'delivery.task.submitted',
    order: 40,
    label: 'Submitted',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.approved': {
    id: 'delivery.task.approved',
    order: 50,
    label: 'Approved',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.revision_requested': {
    id: 'delivery.task.revision_requested',
    order: 60,
    label: 'Revision Requested',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.rejected': {
    id: 'delivery.task.rejected',
    order: 70,
    label: 'Rejected',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.cancelled': {
    id: 'delivery.task.cancelled',
    order: 80,
    label: 'Cancelled',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },
  'delivery.task.completed': {
    id: 'delivery.task.completed',
    order: 90,
    label: 'Completed',
    semanticClass: 'delivery.task',
    category: 'delivery'
  },

  // --- delivery.project (ProjectStatus — 6 values) ---
  'delivery.project.active': {
    id: 'delivery.project.active',
    order: 100,
    label: 'Active',
    semanticClass: 'delivery.project',
    category: 'delivery'
  },
  'delivery.project.on_track': {
    id: 'delivery.project.on_track',
    order: 110,
    label: 'On Track',
    semanticClass: 'delivery.project',
    category: 'delivery'
  },
  'delivery.project.at_risk': {
    id: 'delivery.project.at_risk',
    order: 120,
    label: 'At Risk',
    semanticClass: 'delivery.project',
    category: 'delivery'
  },
  'delivery.project.blocked': {
    id: 'delivery.project.blocked',
    order: 130,
    label: 'Blocked',
    semanticClass: 'delivery.project',
    category: 'delivery'
  },
  'delivery.project.paused': {
    id: 'delivery.project.paused',
    order: 140,
    label: 'Paused',
    semanticClass: 'delivery.project',
    category: 'delivery'
  },
  'delivery.project.completed': {
    id: 'delivery.project.completed',
    order: 150,
    label: 'Completed',
    semanticClass: 'delivery.project',
    category: 'delivery'
  },

  // --- delivery.milestone (MilestoneStatus — 5 values) ---
  'delivery.milestone.upcoming': {
    id: 'delivery.milestone.upcoming',
    order: 160,
    label: 'Upcoming',
    semanticClass: 'delivery.milestone',
    category: 'delivery'
  },
  'delivery.milestone.in_progress': {
    id: 'delivery.milestone.in_progress',
    order: 170,
    label: 'In Progress',
    semanticClass: 'delivery.milestone',
    category: 'delivery'
  },
  'delivery.milestone.blocked': {
    id: 'delivery.milestone.blocked',
    order: 180,
    label: 'Blocked',
    semanticClass: 'delivery.milestone',
    category: 'delivery'
  },
  'delivery.milestone.overdue': {
    id: 'delivery.milestone.overdue',
    order: 190,
    label: 'Overdue',
    semanticClass: 'delivery.milestone',
    category: 'delivery'
  },
  'delivery.milestone.completed': {
    id: 'delivery.milestone.completed',
    order: 200,
    label: 'Completed',
    semanticClass: 'delivery.milestone',
    category: 'delivery'
  },

  // --- queue (QueueTaskStatus — 5 values, maps hitl/command-queue tasks) ---
  'queue.pending': { id: 'queue.pending', order: 210, label: 'Pending', semanticClass: 'queue', category: 'queue' },
  'queue.processing': {
    id: 'queue.processing',
    order: 220,
    label: 'Processing',
    semanticClass: 'queue',
    category: 'queue'
  },
  'queue.completed': {
    id: 'queue.completed',
    order: 230,
    label: 'Completed',
    semanticClass: 'queue',
    category: 'queue'
  },
  'queue.failed': { id: 'queue.failed', order: 240, label: 'Failed', semanticClass: 'queue', category: 'queue' },
  'queue.expired': { id: 'queue.expired', order: 250, label: 'Expired', semanticClass: 'queue', category: 'queue' },

  // --- execution (ExecutionStatus — 5 values) ---
  'execution.pending': {
    id: 'execution.pending',
    order: 260,
    label: 'Pending',
    semanticClass: 'execution',
    category: 'execution'
  },
  'execution.running': {
    id: 'execution.running',
    order: 270,
    label: 'Running',
    semanticClass: 'execution',
    category: 'execution'
  },
  'execution.completed': {
    id: 'execution.completed',
    order: 280,
    label: 'Completed',
    semanticClass: 'execution',
    category: 'execution'
  },
  'execution.failed': {
    id: 'execution.failed',
    order: 290,
    label: 'Failed',
    semanticClass: 'execution',
    category: 'execution'
  },
  'execution.warning': {
    id: 'execution.warning',
    order: 300,
    label: 'Warning',
    semanticClass: 'execution',
    category: 'execution'
  },

  // --- schedule (schedule status — 4 values) ---
  'schedule.active': {
    id: 'schedule.active',
    order: 310,
    label: 'Active',
    semanticClass: 'schedule',
    category: 'schedule'
  },
  'schedule.paused': {
    id: 'schedule.paused',
    order: 320,
    label: 'Paused',
    semanticClass: 'schedule',
    category: 'schedule'
  },
  'schedule.completed': {
    id: 'schedule.completed',
    order: 330,
    label: 'Completed',
    semanticClass: 'schedule',
    category: 'schedule'
  },
  'schedule.cancelled': {
    id: 'schedule.cancelled',
    order: 340,
    label: 'Cancelled',
    semanticClass: 'schedule',
    category: 'schedule'
  },

  // --- schedule.run (schedule run status — 4 values) ---
  'schedule.run.running': {
    id: 'schedule.run.running',
    order: 350,
    label: 'Running',
    semanticClass: 'schedule.run',
    category: 'schedule'
  },
  'schedule.run.completed': {
    id: 'schedule.run.completed',
    order: 360,
    label: 'Completed',
    semanticClass: 'schedule.run',
    category: 'schedule'
  },
  'schedule.run.failed': {
    id: 'schedule.run.failed',
    order: 370,
    label: 'Failed',
    semanticClass: 'schedule.run',
    category: 'schedule'
  },
  'schedule.run.cancelled': {
    id: 'schedule.run.cancelled',
    order: 380,
    label: 'Cancelled',
    semanticClass: 'schedule.run',
    category: 'schedule'
  },

  // --- request (RequestStatus — 4 values, maps reported_requests) ---
  'request.open': { id: 'request.open', order: 390, label: 'Open', semanticClass: 'request', category: 'request' },
  'request.investigating': {
    id: 'request.investigating',
    order: 400,
    label: 'Investigating',
    semanticClass: 'request',
    category: 'request'
  },
  'request.resolved': {
    id: 'request.resolved',
    order: 410,
    label: 'Resolved',
    semanticClass: 'request',
    category: 'request'
  },
  'request.wont_fix': {
    id: 'request.wont_fix',
    order: 420,
    label: "Won't Fix",
    semanticClass: 'request',
    category: 'request'
  }
}
