/**
 * Task submission and status types for cowork-service.
 *
 * These mirror the Rust DispatchState types and are used for
 * the REST API that proxies to the Rust agent runtime via IPC.
 */

import { z } from 'zod';

// ── Request schemas ──

export const SubmitTaskSchema = z.object({
  goal: z.string().min(1).max(10_000),
  folder: z.string().min(1).max(1024),
  model: z.string().max(128).default('claude-sonnet-4-20250514'),
  plugins: z.array(z.string().max(128)).max(50).default([]),
  contextFiles: z.array(z.string().max(1024)).max(100).default([]),
  budget: z
    .object({
      maxCostUsd: z.number().min(0).max(1000).default(5),
      warnThreshold: z.number().min(0).max(1).default(0.8),
    })
    .optional(),
});

export type SubmitTaskInput = z.infer<typeof SubmitTaskSchema>;

// ── Response types ──

export type TaskStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';

export interface TaskResponse {
  id: string;
  productId: string;
  goal: string;
  folder: string;
  model: string;
  status: TaskStatus;
  createdAt: string;
  updatedAt: string;
  result?: string;
  error?: string;
  costUsd?: number;
  tokenCount?: number;
}

// ── IPC message types (JSON-RPC to Rust runtime) ──

export interface IpcRequest {
  jsonrpc: '2.0';
  id: number;
  method: string;
  params: Record<string, unknown>;
}

export interface IpcResponse {
  jsonrpc: '2.0';
  id: number;
  result?: unknown;
  error?: { code: number; message: string; data?: unknown };
}
