import { type MetaFunction } from "@remix-run/node";
import { useState } from "react";
import { BasicInfo } from "~/components/rules/new/BasicInfo";
import { ExtractionSettings } from "~/components/rules/new/ExtractionSettings";
import { ReviewSettings, RuleContext } from "~/components/rules/new/ReviewSettings";
import { ActionButtons } from "~/components/rules/new/ActionButtons";
import { PageHeader } from "~/components/rules/new/PageHeader";
import rulesStyles from "~/styles/rules.css?url";
import { useNavigate } from "@remix-run/react";

export const meta: MetaFunction = () => {
  return [
    { title: "新增评查点 - 中国烟草AI合同及卷宗审核系统" },
    { 
      name: "description", 
      content: "创建新的评查点，设置规则参数" 
    }
  ];
};

export function links() {
  return [{ rel: "stylesheet", href: rulesStyles }];
}

export const handle = {
  breadcrumb: "新增评查点"
};

// 定义类型
interface RegexField {
  fieldName: string;
  regex: string;
}

interface PromptSetting {
  type: string;
  template: string;
}

interface ExtactionConfigType {
  llm_ocr: {
    fields: string[];
    prompt_setting: PromptSetting;
  };
  llm_vl: {
    fields: string[];
    prompt_setting: PromptSetting;
  };
  ocr_regex: {
    fields: RegexField[];
  };
}

interface RuleConfigType {
  [key: string]: any;
}

interface Rule {
  id: string;
  type: string;
  config: RuleConfigType;
}

interface EvaluationConfigType {
  logicType: string;
  customLogic: string;
  rules: Rule[];
}

interface FormDataType {
  name: string;
  code: string;
  risk: string;
  is_enabled: boolean;
  description: string;
  references_laws: {
    name: string;
    articles: string[];
    content: string;
  };
  evaluation_point_groups_id: number | null;
  extraction_config: ExtactionConfigType;
  evaluation_config: EvaluationConfigType;
  pass_message: string;
  fail_message: string;
  suggestion_message: string;
  suggestion_message_type: string;
  post_action: string;
  action_config: string;
}

export default function RuleNew() {
  const navigate = useNavigate();
  const [extractionFields, setExtractionFields] = useState<string[]>([]);
  const [formData, setFormData] = useState<FormDataType>({
    // 基本信息字段
    name: '',
    code: '',
    risk: 'medium',
    is_enabled: true,
    description: '',
    references_laws: {
      name: '',
      articles: [],
      content: ''
    },
    evaluation_point_groups_id: null,
    
    // 抽取设置
    extraction_config: {
      llm_ocr: {
        fields: [],
        prompt_setting: {
          type: 'system',
          template: ''
        }
      },
      llm_vl: {
        fields: [],
        prompt_setting: {
          type: 'system',
          template: ''
        }
      },
      ocr_regex: {
        fields: []
      }
    },
    
    // 评查设置
    evaluation_config: {
      logicType: 'and',
      customLogic: '',
      rules: []
    },
    
    // 评查结果消息
    pass_message: '文档检查通过，符合规范要求。',
    fail_message: '文档存在以下问题，请修改后重新提交。',
    suggestion_message: '',
    suggestion_message_type: 'warning',
    
    // 评查后动作
    post_action: 'none',
    action_config: ''
  });

  // 更新抽取字段列表，用于在评查规则中选择
  const updateExtractionFields = (fields: string[]) => {
    setExtractionFields(fields);
  };

  // 处理BasicInfo组件数据变更
  const handleBasicInfoChange = (data: Record<string, unknown>) => {
    setFormData(prevData => ({
      ...prevData,
      ...data
    }));
  };

  // 处理ExtractionSettings组件数据变更
  const handleExtractionSettingsChange = (data: Record<string, unknown>) => {
    setFormData(prevData => {
      // 获取数据
      const extractionMethod = data.extractionMethod as string;
      const regexFields = data.regexFields as Array<{ id: string; fieldName: string; regex: string }>;
      const fields = data.fields as Record<string, string[]>;
      
      // 根据抽取方法更新对应字段
      const updatedExtractionConfig = { ...prevData.extraction_config };
      
      // 更新正则抽取字段
      if (regexFields) {
        updatedExtractionConfig.ocr_regex.fields = regexFields
          .filter(field => field.fieldName && field.regex)
          .map(field => ({
            fieldName: field.fieldName,
            regex: field.regex
          }));
      }
      
      // 更新LLM字段
      if (fields) {
        if (fields.llm_ocr) {
          updatedExtractionConfig.llm_ocr.fields = fields.llm_ocr;
        }
        if (fields.llm) {
          updatedExtractionConfig.llm_vl.fields = fields.llm;
        }
      }
      
      // 更新提示词设置
      if (data.promptSettings) {
        const promptSettings = data.promptSettings as Record<string, any>;
        
        // 确定当前是处理哪种类型的提示词
        if (extractionMethod === 'llm_ocr') {
          updatedExtractionConfig.llm_ocr.prompt_setting.type = promptSettings.type || 'system';
          updatedExtractionConfig.llm_ocr.prompt_setting.template = promptSettings.content || '';
        } else if (extractionMethod === 'llm') {
          updatedExtractionConfig.llm_vl.prompt_setting.type = promptSettings.type || 'system';
          updatedExtractionConfig.llm_vl.prompt_setting.template = promptSettings.content || '';
        }
      } else {
        // 兼容旧的API
        if (data.promptType) {
          const promptType = data.promptType as Record<string, string>;
          if (promptType.llm_ocr) {
            updatedExtractionConfig.llm_ocr.prompt_setting.type = promptType.llm_ocr;
          }
          if (promptType.llm) {
            updatedExtractionConfig.llm_vl.prompt_setting.type = promptType.llm;
          }
        }
        
        if (data.promptContent) {
          const promptContent = data.promptContent as Record<string, string>;
          if (promptContent.llm_ocr) {
            updatedExtractionConfig.llm_ocr.prompt_setting.template = promptContent.llm_ocr;
          }
          if (promptContent.llm) {
            updatedExtractionConfig.llm_vl.prompt_setting.template = promptContent.llm;
          }
        }
      }
      
      console.log('更新的抽取设置:', updatedExtractionConfig);
      
      return {
        ...prevData,
        extraction_config: updatedExtractionConfig
      };
    });
  };

  // 处理ReviewSettings组件数据变更
  const handleReviewSettingsChange = (data: Record<string, unknown>) => {
    setFormData(prevData => {
      const updatedData = { ...prevData };
      
      // 更新规则
      if (data.rules) {
        updatedData.evaluation_config.rules = data.rules as Rule[];
      }
      
      // 更新组合逻辑
      if (data.combinationLogic !== undefined) {
        updatedData.evaluation_config.logicType = data.combinationLogic as string;
      }
      
      // 更新自定义逻辑
      if (data.customLogic !== undefined) {
        updatedData.evaluation_config.customLogic = data.customLogic as string;
      }
      
      // 更新通过/不通过/建议消息
      if (data.pass_message !== undefined) {
        updatedData.pass_message = data.pass_message as string;
      }
      
      if (data.fail_message !== undefined) {
        updatedData.fail_message = data.fail_message as string;
      }
      
      if (data.suggestion_message !== undefined) {
        updatedData.suggestion_message = data.suggestion_message as string;
      }
      
      if (data.suggestion_message_type !== undefined) {
        updatedData.suggestion_message_type = data.suggestion_message_type as string;
      }
      
      // 更新评查后动作
      if (data.post_action !== undefined) {
        updatedData.post_action = data.post_action as string;
      }
      
      if (data.action_config !== undefined) {
        updatedData.action_config = data.action_config as string;
      }
      
      return updatedData;
    });
  };

  // 保存评查点
  const handleSave = async () => {
    try {
      // 转换提取配置为符合数据库格式的JSON
      const extractionConfig = {
        llm: {
          fields: formData.extraction_config.llm_ocr.fields || [],
          prompt_setting: {
            type: formData.extraction_config.llm_ocr.prompt_setting.type || 'system',
            template: formData.extraction_config.llm_ocr.prompt_setting.template || ''
          }
        },
        vlm: {
          fields: (formData.extraction_config.llm_vl.fields || []).map((field: any) => {
            // 处理带有类型后缀的字段名 (如 "字段名_类型")
            if (typeof field === 'string' && field.includes('_')) {
              const [name, type] = field.split('_');
              return {
                name,
                type: type || 'default'
              };
            }
            return { name: field, type: 'default' };
          }),
          prompt_setting: {
            type: formData.extraction_config.llm_vl.prompt_setting.type || 'system',
            template: formData.extraction_config.llm_vl.prompt_setting.template || ''
          }
        },
        regex: {
          fields: (formData.extraction_config.ocr_regex.fields || []).map((field: any) => {
            if (typeof field === 'object') {
              return {
                field: field.fieldName || '',
                pattern: field.regex || ''
              };
            }
            return { field: '', pattern: '' };
          })
        }
      };
      
      console.log('提交的提取配置:', extractionConfig);
      console.log('原始提取配置:', formData.extraction_config);
      
      // 转换评查配置为符合数据库格式的JSON
      const evaluationConfig = {
        logicType: formData.evaluation_config.logicType || 'and',
        customLogic: formData.evaluation_config.customLogic || '',
        rules: (formData.evaluation_config.rules || []).map((rule: any) => {
          let config = {};
          
          // 根据不同的规则类型生成对应的配置
          switch (rule.type) {
            case 'exists': 
              config = {
                fields: rule.config?.selectedFields || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'consistency':
              config = {
                pairs: rule.config?.pairs || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'format':
              config = {
                field: rule.config?.field || '',
                formatType: rule.config?.formatType || '',
                parameters: rule.config?.parameters || ''
              };
              break;
            case 'logic':
              config = {
                conditions: rule.config?.conditions || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'regex':
              config = {
                field: rule.config?.field || '',
                pattern: rule.config?.pattern || '',
                matchType: rule.config?.matchType || 'match'
              };
              break;
            case 'ai':
              config = {
                model: rule.config?.model || 'qwen14b',
                temperature: rule.config?.temperature || 0.1,
                prompt: rule.config?.prompt || ''
              };
              break;
            case 'code':
              config = {
                language: rule.config?.language || 'javascript',
                code: rule.config?.code || ''
              };
              break;
            default:
              config = rule.config || {};
          }
          
          return {
            id: rule.id,
            type: rule.type,
            config
          };
        })
      };
      
      // 构建完整的评查点数据
      const evaluationPointData = {
        code: formData.code,
        name: formData.name,
        evaluation_point_groups_id: formData.evaluation_point_groups_id,
        risk: formData.risk,
        description: formData.description,
        is_enabled: formData.is_enabled,
        references_laws: formData.references_laws,
        extraction_config: extractionConfig,
        evaluation_config: evaluationConfig,
        pass_message: formData.pass_message,
        fail_message: formData.fail_message,
        suggestion_message: formData.suggestion_message,
        suggestion_message_type: formData.suggestion_message_type,
        post_action: formData.post_action,
        action_config: formData.action_config
      };
      
      // 发送数据到API
      const response = await fetch('/api/evaluation-points', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(evaluationPointData)
      });
      
      if (!response.ok) {
        throw new Error(`API响应错误: ${response.status}`);
      }
      
      const result = await response.json();
      console.log('保存成功:', result);
      
      // 保存成功后跳转到评查点列表页面
      navigate('/rules');
    } catch (error) {
      console.error('保存失败:', error);
      alert(`保存失败: ${error instanceof Error ? error.message : '未知错误'}`);
    }
  };
  
  // 保存为草稿
  const handleSaveDraft = async () => {
    try {
      // 转换提取配置为符合数据库格式的JSON
      const extractionConfig = {
        llm: {
          fields: formData.extraction_config.llm_ocr.fields || [],
          prompt_setting: {
            type: formData.extraction_config.llm_ocr.prompt_setting.type || 'system',
            template: formData.extraction_config.llm_ocr.prompt_setting.template || ''
          }
        },
        vlm: {
          fields: (formData.extraction_config.llm_vl.fields || []).map((field: any) => {
            // 处理带有类型后缀的字段名 (如 "字段名_类型")
            if (typeof field === 'string' && field.includes('_')) {
              const [name, type] = field.split('_');
              return {
                name,
                type: type || 'default'
              };
            }
            return { name: field, type: 'default' };
          }),
          prompt_setting: {
            type: formData.extraction_config.llm_vl.prompt_setting.type || 'system',
            template: formData.extraction_config.llm_vl.prompt_setting.template || ''
          }
        },
        regex: {
          fields: (formData.extraction_config.ocr_regex.fields || []).map((field: any) => {
            if (typeof field === 'object') {
              return {
                field: field.fieldName || '',
                pattern: field.regex || ''
              };
            }
            return { field: '', pattern: '' };
          })
        }
      };
      
      console.log('草稿提交的提取配置:', extractionConfig);
      console.log('草稿原始提取配置:', formData.extraction_config);
      
      // 转换评查配置为符合数据库格式的JSON
      const evaluationConfig = {
        logicType: formData.evaluation_config.logicType || 'and',
        customLogic: formData.evaluation_config.customLogic || '',
        rules: (formData.evaluation_config.rules || []).map((rule: any) => {
          let config = {};
          
          // 根据不同的规则类型生成对应的配置
          switch (rule.type) {
            case 'exists': 
              config = {
                fields: rule.config?.selectedFields || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'consistency':
              config = {
                pairs: rule.config?.pairs || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'format':
              config = {
                field: rule.config?.field || '',
                formatType: rule.config?.formatType || '',
                parameters: rule.config?.parameters || ''
              };
              break;
            case 'logic':
              config = {
                conditions: rule.config?.conditions || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'regex':
              config = {
                field: rule.config?.field || '',
                pattern: rule.config?.pattern || '',
                matchType: rule.config?.matchType || 'match'
              };
              break;
            case 'ai':
              config = {
                model: rule.config?.model || 'qwen14b',
                temperature: rule.config?.temperature || 0.1,
                prompt: rule.config?.prompt || ''
              };
              break;
            case 'code':
              config = {
                language: rule.config?.language || 'javascript',
                code: rule.config?.code || ''
              };
              break;
            default:
              config = rule.config || {};
          }
          
          return {
            id: rule.id,
            type: rule.type,
            config
          };
        })
      };

      // 构建带草稿标记的评查点数据
      const draftData = {
        code: formData.code,
        name: formData.name,
        evaluation_point_groups_id: formData.evaluation_point_groups_id,
        risk: formData.risk,
        description: formData.description,
        is_enabled: false, // 草稿默认不启用
        is_draft: true, // 标记为草稿
        references_laws: formData.references_laws,
        extraction_config: extractionConfig,
        evaluation_config: evaluationConfig,
        pass_message: formData.pass_message,
        fail_message: formData.fail_message,
        suggestion_message: formData.suggestion_message,
        suggestion_message_type: formData.suggestion_message_type,
        post_action: formData.post_action,
        action_config: formData.action_config
      };
      
      // 发送数据到API
      const response = await fetch('/api/evaluation-points/draft', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(draftData)
      });
      
      if (!response.ok) {
        throw new Error(`API响应错误: ${response.status}`);
      }
      
      const result = await response.json();
      console.log('保存草稿成功:', result);
      
      // 保存成功后跳转到评查点列表页面
      navigate('/rules');
    } catch (error) {
      console.error('保存草稿失败:', error);
      alert(`保存草稿失败: ${error instanceof Error ? error.message : '未知错误'}`);
    }
  };

  return (
    <div className="container">
      <PageHeader 
        title="新增评查点" 
        onSave={handleSave} 
      />
      
      <div className="mb-8">
        <BasicInfo onChange={handleBasicInfoChange} />
      </div>
      
      <div className="mb-8">
        <RuleContext.Provider value={{ extractionFields, updateFields: updateExtractionFields }}>
          <ExtractionSettings onChange={handleExtractionSettingsChange} />
        </RuleContext.Provider>
      </div>
      
      <div className="mb-8">
        <RuleContext.Provider value={{ extractionFields, updateFields: updateExtractionFields }}>
          <ReviewSettings onChange={handleReviewSettingsChange} />
        </RuleContext.Provider>
      </div>
      
      <ActionButtons 
        onSave={handleSave}
        onSaveDraft={handleSaveDraft}
      />
    </div>
  );
}
