import { ref } from 'vue';
import { defineStore } from 'pinia';
import { useLocalStorage } from '@vueuse/core';

export interface Language {
  displayName: string; // UI 显示的中文名，如 "英语（英国）"
  englishName: string; // 文件中的第二列，用于 {SOURCE_LANG}
  code: string;        // 文件中的第一列，用于 {SOURCE_CODE}
}

export const LANGUAGES: Language[] = [
  { displayName: '中文（简体）', englishName: 'Simplified Chinese', code: 'zh-Hans' },
  { displayName: '中文（繁体）', englishName: 'Traditional Chinese', code: 'zh-Hant' },
  { displayName: '英语（美国）', englishName: 'American English', code: 'en-US' },
  { displayName: '英语（英国）', englishName: 'British English', code: 'en-GB' },
  { displayName: '西班牙语', englishName: 'Spanish', code: 'es' },
  { displayName: '葡萄牙语', englishName: 'Portuguese', code: 'pt' },
  { displayName: '日语', englishName: 'Japanese', code: 'ja' },
  { displayName: '韩语', englishName: 'Korean', code: 'ko' },
  { displayName: '法语', englishName: 'French', code: 'fr' },
  { displayName: '德语', englishName: 'German', code: 'de' },
  { displayName: '意大利语', englishName: 'Italian', code: 'it' },
  { displayName: '俄语', englishName: 'Russian', code: 'ru' },
  { displayName: '越南语', englishName: 'Vietnamese', code: 'vi' },
  { displayName: '泰语', englishName: 'Thai', code: 'th' },
  { displayName: '阿拉伯语', englishName: 'Arabic', code: 'ar' },
];

export const SPEAKER_IDENTITY_OPTIONS = [
  { label: '男性', value: 'Male' },
  { label: '女性', value: 'Female' },
  { label: '中性', value: 'Gender-neutral' },
];

export const TONE_REGISTER_OPTIONS = [
  { label: '正式专业', value: 'Formal & Professional', description: '商务邮件、法律合同、官方报告' },
  { label: '礼貌客气', value: 'Polite & Respectful', description: '与长辈、客户或初次见面的人交流' },
  { label: '礼貌随和', value: 'Polite & Conversational', description: '得体但不刻板的日常对话' },
  { label: '中性标准', value: 'Neutral & Standard', description: '维基百科、说明书、客观的新闻报道' },
  { label: '非正式', value: 'Casual & Informal', description: '朋友聊天、社交媒体、非正式简讯' },
  { label: '亲切友好', value: 'Warm & Friendly', description: '社区信函、给朋友的建议、温馨提示' },
  { label: '严谨权威', value: 'Strict & Authoritative', description: '警示标志、强制规定、上级指令' },
  { label: '热情生动', value: 'Enthusiastic & Vivid', description: '广告文案、旅游推荐、博主推文' },
];

export const DEFAULT_TEMPLATE = `You are a professional {SOURCE_LANG} ({SOURCE_CODE}) to {TARGET_LANG} ({TARGET_CODE}) translator. Your goal is to accurately convey the meaning and nuances of the original {SOURCE_LANG} text while adhering to {TARGET_LANG} grammar, vocabulary, and cultural sensitivities.

[Constraints]
1. Speaker Identity: {SPEAKER_IDENTITY}. Ensure all grammatical agreements and self-referential terms in {TARGET_LANG} reflect this.
2. Tone & Register: {TONE_REGISTER}.
3. Produce ONLY the {TARGET_LANG} translation, without any additional explanations, notes, or commentary.
4. If [Context] is provided, use it strictly to disambiguate polysemous words. DO NOT add any factual information or descriptive details from the [Context] that are not present in the [Text to Translate].`;

export const DEFAULT_EVALUATION_TEMPLATE = `# Role
You are an **Objective Translation Auditor**. Your task is to evaluate translation quality based on accuracy, grammar, and fundamental readability. Avoid pedantic nitpicking over synonyms, but do point out issues that hinder professional quality.

# Context Info
- **Source Language**: {SOURCE_LANG}
- **Target Language**: {TARGET_LANG}
- **Speaker Identity**: {SPEAKER_IDENTITY}
- **Intended Tone/Register**: {TONE_REGISTER}
- **Context**: {CONTEXT}

# Audit Criteria
Only penalize and provide improvements if the translation meets one of these criteria:
1. **Semantic Error**: Objective misalignment with the source meaning or complete hallucinations.
2. **Grammatical Error**: Clear violations of target language grammar or syntax rules.
3. **Tone Failure**: A tone that is the opposite or significantly different from the [Intended Tone/Register].
4. **Poor Readability**: The phrasing is so stiff, unnatural, or convoluted that it hinders smooth comprehension (e.g., obvious "translationese" or broken logic).

**Note**: Do NOT penalize if the translation is simply "not the most elegant" or if there's a subjective preference for a different synonym. If it's natural enough for a native speaker to understand without effort, it's acceptable.

# Instructions
1. **Evaluation**: Compare the [Source Text] and [Translated Text] based on the [Audit Criteria].
2. **Scoring Strategy**: 
   - **90-100**: Accurate, grammatically sound, and flows naturally.
   - **75-89**: Accurate meaning, but suffers from "stiff" phrasing or minor flow issues that need adjustment.
   - **Below 75**: Contains semantic errors, severe grammar issues, or tone mismatches.
3. **Analysis**: Provide a concise explanation in Simplified Chinese. Focus on *why* the error matters (e.g., "meaning is reversed" or "too awkward to read").
4. **Suggestions**: Provide a list of specific, actionable suggestions. For each, assign an "importance" from 0 to 100 (0 = unnecessary/optional, 100 = critical error).

# Output Format
Respond ONLY in JSON format. The "analysis" and "text" within "suggestions" MUST be in Simplified Chinese:
{
  "score": number,
  "analysis": "string",
  "suggestions": [
    { "id": 1, "text": "suggestion text", "importance": number }
  ]
}`;

export const DEFAULT_REFINEMENT_TEMPLATE = `You are a senior translation editor. Your task is to refine the [Current Translation] based on specific [User Feedback], while strictly maintaining the original meaning of the [Source Text] and adhering to the established context.

[Context Info]
- Source Language: {SOURCE_LANG}
- Target Language: {TARGET_LANG}
- Speaker Identity: {SPEAKER_IDENTITY}
- Intended Tone/Register: {TONE_REGISTER}
- Context: {CONTEXT}

[Instructions]
1. Carefully review the [User Feedback] and apply the requested improvements to the [Current Translation].
2. Ensure that the refined translation remains semantically identical to the [Source Text].
3. Maintain the [Speaker Identity] and [Intended Tone/Register] as specified.
4. If a piece of feedback contradicts the [Source Text], prioritize accuracy and provide a balanced refinement.
5. Produce ONLY the refined {TARGET_LANG} translation, without any additional explanations, notes, or commentary.`;

export interface ApiProfile {
  id: string;
  name: string;
  apiBaseUrl: string;
  apiKey: string;
  modelName: string;
}

export const useSettingsStore = defineStore('settings', () => {
  const isDark = useLocalStorage('is-dark', false);
  const apiBaseUrl = useLocalStorage('api-base-url', 'http://localhost:11434/v1');
  const apiKey = useLocalStorage('api-key', '');
  const modelName = useLocalStorage('model-name', 'translategemma:12b');
  const profiles = useLocalStorage<ApiProfile[]>('api-profiles', []);
  const enableStreaming = useLocalStorage('enable-streaming', true);
  const systemPromptTemplate = useLocalStorage('system-prompt-template', DEFAULT_TEMPLATE);
  
  const enableEvaluation = useLocalStorage('enable-evaluation', true);
  const evaluationPromptTemplate = useLocalStorage('evaluation-prompt-template', DEFAULT_EVALUATION_TEMPLATE);
  const evaluationProfileId = useLocalStorage<string | null>('evaluation-profile-id', null);
  const refinementPromptTemplate = useLocalStorage('refinement-prompt-template', DEFAULT_REFINEMENT_TEMPLATE);
  
  // 存储整个对象以保持一致性
  const sourceLang = useLocalStorage<Language>('source-lang-v2', LANGUAGES[0]);
  const targetLang = useLocalStorage<Language>('target-lang-v2', LANGUAGES[4]);

  const speakerIdentity = useLocalStorage('speaker-identity', SPEAKER_IDENTITY_OPTIONS[0].value);
  const toneRegister = useLocalStorage('tone-register', TONE_REGISTER_OPTIONS[2].value);

  const logs = ref<{ timestamp: string; type: 'request' | 'response' | 'error'; content: any }[]>([]);
  
  const addLog = (type: 'request' | 'response' | 'error', content: any) => {
    const now = new Date();
    const timestamp = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
    
    logs.value.unshift({
      timestamp,
      type,
      content
    });
    if (logs.value.length > 20) logs.value.pop();
  };

  return {
    isDark,
    apiBaseUrl,
    apiKey,
    modelName,
    profiles,
    enableStreaming,
    systemPromptTemplate,
    enableEvaluation,
    evaluationPromptTemplate,
    evaluationProfileId,
    refinementPromptTemplate,
    sourceLang,
    targetLang,
    speakerIdentity,
    toneRegister,
    logs,
    addLog
  };
});
