import { z } from 'zod';
import { NodeType, NodeStatus, VideoGenerationMode } from './node';

export const InputAssetSchema = z.object({
    id: z.string(),
    type: z.enum(['image', 'video', 'audio']),
    src: z.string(), // Relaxed to allow relative paths or non-standard URIs
    label: z.string().optional(),
    gender: z.string().optional(),
    logicalRole: z.string().optional(),
    mentionAlias: z.string().optional(),
});

export const AppNodeDataSchema = z.object({
    title: z.string().default('New Node'),
    type: z.nativeEnum(NodeType),
    status: z.nativeEnum(NodeStatus).default(NodeStatus.IDLE),
    content: z.string().optional(),
    prompt: z.string().optional(),
    userPrompt: z.string().optional(),
    inputPrompt: z.string().optional(),
    model: z.string().optional(),
    aspectRatio: z.string().optional(),
    resolution: z.string().optional(),
    template: z.string().optional(),
    targetType: z.enum(['image', 'video']).optional(),
    imageCount: z.number().int().min(1).max(10).optional(),
    videoCount: z.number().int().min(1).max(5).optional(),
    analysis: z.string().optional(),
    audioUri: z.string().optional(),
    voice: z.string().optional(),
    videoUri: z.string().optional(),
    image: z.string().optional(),
    images: z.array(z.string()).optional(),
    videoUris: z.array(z.string()).optional(),
    inputAssets: z.array(InputAssetSchema).optional(),
    assetInfo: z.any().optional(),
    generationMode: z.enum(['DEFAULT', 'CONTINUE', 'FIRST_LAST_FRAME', 'CUT', 'CHARACTER_REF']).optional(),
    duration: z.string().optional(),
    error: z.string().optional(),
    croppedFrame: z.string().optional(),
    width: z.number().optional(),
    autoExecute: z.boolean().optional(),
    executed: z.boolean().optional(),
});

export type AppNodeDataValidated = z.infer<typeof AppNodeDataSchema>;

// API Response Schemas
export const ImageGenerationResponseSchema = z.array(z.string());

export const VideoGenerationResponseSchema = z.object({
    uri: z.string(),
    isFallbackImage: z.boolean().optional(),
    videoMetadata: z.any().optional(),
    uris: z.array(z.string()).optional(),
});

export const AudioGenerationResponseSchema = z.string();

export const StoryboardResponseSchema = z.array(z.string());

export const PromptOptimizationResponseSchema = z.string();

/**
 * Helper to validate node data and provide default values if needed
 */
export function validateNodeData(data: any): AppNodeDataValidated {
    return AppNodeDataSchema.parse(data);
}

/**
 * Helper for partial updates
 */
export function validatePartialNodeData(data: any): Partial<AppNodeDataValidated> {
    return AppNodeDataSchema.partial().parse(data);
}
