/**
 * Orchestrator API Types
 */

export interface OrchestratorWorkflowRequest {
    target_date?: string; // YYYY-MM-DD, defaults to tomorrow
    planning_horizon_days?: number; // Default: 14

    // Forecasting options
    forecast_days_ahead?: number; // Default: 7

    // Production options
    auto_schedule_production?: boolean; // Default: true
    production_planning_days?: number; // Default: 1

    // Procurement options
    auto_create_purchase_orders?: boolean; // Default: true
    auto_approve_purchase_orders?: boolean; // Default: false
    safety_stock_percentage?: number; // Default: 20.00

    // Orchestrator options
    skip_on_error?: boolean; // Continue to next step if one fails
    notify_on_completion?: boolean; // Send notification when done
}

export interface WorkflowStepResult {
    step: 'forecasting' | 'production' | 'procurement';
    status: 'success' | 'failed' | 'skipped';
    duration_ms: number;
    data?: any;
    error?: string;
    warnings?: string[];
}

export interface OrchestratorWorkflowResponse {
    success: boolean;
    workflow_id: string;
    tenant_id: string;
    target_date: string;
    execution_date: string;
    total_duration_ms: number;

    steps: WorkflowStepResult[];

    // Step-specific results
    forecast_result?: {
        forecast_id: string;
        total_forecasts: number;
        forecast_data: any;
    };

    production_result?: {
        schedule_id: string;
        total_batches: number;
        total_quantity: number;
    };

    procurement_result?: {
        plan_id: string;
        total_requirements: number;
        total_cost: string;
        purchase_orders_created: number;
        purchase_orders_auto_approved: number;
    };

    warnings?: string[];
    errors?: string[];
}

export interface WorkflowExecutionSummary {
    id: string;
    tenant_id: string;
    target_date: string;
    status: 'running' | 'completed' | 'failed' | 'cancelled';
    started_at: string;
    completed_at?: string;
    total_duration_ms?: number;
    steps_completed: number;
    steps_total: number;
    created_by?: string;
}

export interface WorkflowExecutionDetail extends WorkflowExecutionSummary {
    steps: WorkflowStepResult[];
    forecast_id?: string;
    production_schedule_id?: string;
    procurement_plan_id?: string;
    warnings?: string[];
    errors?: string[];
}

export interface OrchestratorStatus {
    is_leader: boolean;
    scheduler_running: boolean;
    next_scheduled_run?: string;
    last_execution?: {
        id: string;
        target_date: string;
        status: string;
        completed_at: string;
    };
    total_executions_today: number;
    total_successful_executions: number;
    total_failed_executions: number;
}

export interface OrchestratorConfig {
    enabled: boolean;
    schedule_cron: string; // Cron expression for daily run
    default_planning_horizon_days: number;
    auto_create_purchase_orders: boolean;
    auto_approve_purchase_orders: boolean;
    safety_stock_percentage: number;
    notify_on_completion: boolean;
    notify_on_failure: boolean;
    skip_on_error: boolean;
}
