import { z } from 'zod';

// ---------------------------------------------------------------------------
// Blueprint item schemas — domain-level descriptions of what to build
// ---------------------------------------------------------------------------

export const blueprintWorkflowItemSchema = z.object({
	id: z.string().describe('Stable ID — preserved as task ID in the execution plan'),
	name: z.string().describe('Workflow display name'),
	purpose: z
		.string()
		.describe(
			'1-2 sentence description of what the workflow does and why. Include any data table names, columns, seed/import needs, or existing-table requirements the workflow depends on.',
		),
	integrations: z.array(z.string()).describe('Services/APIs this workflow connects'),
	triggerDescription: z
		.string()
		.optional()
		.describe('Trigger type in a few words (e.g. "Webhook POST", "Schedule hourly")'),
	existingWorkflowId: z
		.string()
		.optional()
		.describe('ID of existing workflow to modify (omit for new workflows)'),
	dependsOn: z
		.array(z.string())
		.default([])
		.describe('IDs of items that must complete before this one starts'),
});

export const blueprintDelegateItemSchema = z.object({
	id: z.string().describe('Stable ID — preserved as task ID'),
	title: z.string().describe('Short task title'),
	description: z.string().describe('Detailed task description'),
	requiredTools: z.array(z.string()).describe('Tool names the delegate needs'),
	dependsOn: z.array(z.string()).default([]),
});

export const blueprintCheckpointItemSchema = z.object({
	id: z.string().describe('Stable ID — preserved as task ID'),
	title: z
		.string()
		.describe(
			'User-readable verification goal (e.g., "Verify Daily Email workflow runs without errors")',
		),
	instructions: z
		.string()
		.describe(
			'Detailed verification steps the orchestrator must execute — which tools to call, the expected pass condition. The orchestrator runs this itself (no sub-agent).',
		),
	dependsOn: z
		.array(z.string())
		.min(1)
		.describe('IDs of items this checkpoint verifies. Must include at least one workflow item ID.'),
});

// ---------------------------------------------------------------------------
// Top-level blueprint schema
// ---------------------------------------------------------------------------

export const planningBlueprintSchema = z.object({
	summary: z.string().describe('1-2 sentence overview of the solution'),
	workflows: z.array(blueprintWorkflowItemSchema).default([]),
	delegateItems: z.array(blueprintDelegateItemSchema).default([]),
	checkpointItems: z.array(blueprintCheckpointItemSchema).default([]),
	assumptions: z.array(z.string()).default([]).describe('Assumptions the plan relies on'),
	openQuestions: z
		.array(z.string())
		.default([])
		.describe('Unresolved questions that may need user input before execution'),
});

export type PlanningBlueprint = z.infer<typeof planningBlueprintSchema>;
export type BlueprintWorkflowItem = z.infer<typeof blueprintWorkflowItemSchema>;
export type BlueprintDelegateItem = z.infer<typeof blueprintDelegateItemSchema>;
export type BlueprintCheckpointItem = z.infer<typeof blueprintCheckpointItemSchema>;
