/**
 * filekorolheader: 应用生成状态管理 - 模型应用生成中心状态管理
 * 功能：应用状态管理、生成流程控制、对话框状态管理
 * 路径：/ai-crop-model/model-application/generation/components/ApplicationGenerationReducer
 * 规范：遵循crop-x/docs/开发项目规范.md，使用TypeScript类型安全
 */
'use client';

import { Application } from '@/types/ai-model';

export interface ApplicationGenerationState {
  applications: Application[];
  showGenerateDialog: boolean;
  generateStep: number;
  newAppData: {
    name: string;
    type: string;
    description: string;
    modelName: string;
    modelVersion: string;
    inputFields: string[];
    outputFormat: string;
  };
  showRunDialog: boolean;
  runningApp: Application | null;
  inputData: Record<string, string>;
  isRunning: boolean;
  runResult: any;
  availableModels: Array<{
    id: string;
    name: string;
    version: string;
    type: string;
    status: string;
  }>;
  inputFieldOptions: string[];
  dataSourceOptions: string[];
  iotDeviceOptions: string[];
  showEditDialog: boolean;
  editingApp: Application | null;
  editStep: number;
  editAppData: {
    name: string;
    type: string;
    description: string;
    modelName: string;
    modelVersion: string;
    inputFields: string[];
    outputFormat: string;
  };
}

export type ApplicationGenerationAction =
  | { type: 'SET_APPLICATIONS'; payload: Application[] }
  | { type: 'ADD_APPLICATION'; payload: Application }
  | { type: 'UPDATE_APPLICATION'; payload: { id: string; updates: Partial<Application> } }
  | { type: 'SET_SHOW_GENERATE_DIALOG'; payload: boolean }
  | { type: 'SET_GENERATE_STEP'; payload: number }
  | { type: 'SET_NEW_APP_DATA'; payload: Partial<ApplicationGenerationState['newAppData']> }
  | { type: 'RESET_NEW_APP_DATA' }
  | { type: 'SET_SHOW_RUN_DIALOG'; payload: boolean }
  | { type: 'SET_RUNNING_APP'; payload: Application | null }
  | { type: 'SET_INPUT_DATA'; payload: Record<string, string> }
  | { type: 'SET_IS_RUNNING'; payload: boolean }
  | { type: 'SET_RUN_RESULT'; payload: any }
  | { type: 'TOGGLE_APPLICATION_STATUS'; payload: string }
  | { type: 'SET_SHOW_EDIT_DIALOG'; payload: boolean }
  | { type: 'SET_EDITING_APP'; payload: Application | null }
  | { type: 'SET_EDIT_STEP'; payload: number }
  | { type: 'SET_EDIT_APP_DATA'; payload: Partial<ApplicationGenerationState['editAppData']> }
  | { type: 'RESET_EDIT_APP_DATA' };

export function ApplicationGenerationReducer(
  state: ApplicationGenerationState,
  action: ApplicationGenerationAction
): ApplicationGenerationState {
  switch (action.type) {
    case 'SET_APPLICATIONS':
      return { ...state, applications: action.payload };

    case 'ADD_APPLICATION':
      return {
        ...state,
        applications: [...state.applications, action.payload]
      };

    case 'UPDATE_APPLICATION':
      return {
        ...state,
        applications: state.applications.map(app =>
          app.id === action.payload.id
            ? { ...app, ...action.payload.updates }
            : app
        )
      };

    case 'TOGGLE_APPLICATION_STATUS':
      return {
        ...state,
        applications: state.applications.map(app =>
          app.id === action.payload
            ? { ...app, status: app.status === '运行中' ? '已停止' : '运行中' }
            : app
        )
      };

    case 'SET_SHOW_GENERATE_DIALOG':
      return { ...state, showGenerateDialog: action.payload };

    case 'SET_GENERATE_STEP':
      return { ...state, generateStep: action.payload };

    case 'SET_NEW_APP_DATA':
      return {
        ...state,
        newAppData: { ...state.newAppData, ...action.payload }
      };

    case 'RESET_NEW_APP_DATA':
      return {
        ...state,
        newAppData: {
          name: '',
          type: '',
          description: '',
          modelName: '',
          modelVersion: '',
          inputFields: [],
          outputFormat: '',
        }
      };

    case 'SET_SHOW_RUN_DIALOG':
      return { ...state, showRunDialog: action.payload };

    case 'SET_RUNNING_APP':
      return { ...state, runningApp: action.payload };

    case 'SET_INPUT_DATA':
      return { ...state, inputData: action.payload };

    case 'SET_IS_RUNNING':
      return { ...state, isRunning: action.payload };

    case 'SET_RUN_RESULT':
      return { ...state, runResult: action.payload };

    case 'SET_SHOW_EDIT_DIALOG':
      return { ...state, showEditDialog: action.payload };

    case 'SET_EDITING_APP':
      return { ...state, editingApp: action.payload };

    case 'SET_EDIT_STEP':
      return { ...state, editStep: action.payload };

    case 'SET_EDIT_APP_DATA':
      return {
        ...state,
        editAppData: { ...state.editAppData, ...action.payload }
      };

    case 'RESET_EDIT_APP_DATA':
      return {
        ...state,
        editAppData: {
          name: '',
          type: '',
          description: '',
          modelName: '',
          modelVersion: '',
          inputFields: [],
          outputFormat: '',
        }
      };

    default:
      return state;
  }
}