import { computed } 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: 'Auto-detect', description: '分析并保持原文的语气、情绪和礼貌程度' },
  { 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}. (If set to 'Auto-detect', analyze the tone, formality, and emotional nuance of the source text and faithfully replicate it. Do not neutralize strong emotions or unique styles.)
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 interface HistoryItem {
  id: string;
  timestamp: string;
  sourceLang: Language;
  targetLang: Language;
  sourceText: string;
  targetText: string;
  context: string;
  speakerIdentity: string;
  toneRegister: string;
  modelName: string;
}

export interface Participant {
  name: string;
  gender: string;
  language: Language;
  tone: string;
}

export interface ChatMessage {
  id: string;
  sender: 'me' | 'partner';
  original: string;
  translated: string;
  timestamp: string;
  evaluation?: string;      // AI 审计结果 (JSON 字符串)
  isEvaluating?: boolean;   // 审计状态
  isRefining?: boolean;     // 润色状态
}

export interface ChatSession {
  id: string;
  title: string;
  me: Participant;
  partner: Participant;
  messages: ChatMessage[];
  lastActivity: string;
}

export const CONVERSATION_SYSTEM_PROMPT_TEMPLATE = `# Role: Professional Real-time Conversation Translator

# Participants:
- Participant A: [Name: {ME_NAME}, Gender: {ME_GENDER}, Language: {ME_LANG}]
- Participant B: [Name: {PART_NAME}, Gender: {PART_GENDER}, Language: {PART_LANG}]

# Recent Conversation Flow:
{HISTORY_BLOCK}

# Current Turn to Translate:
- Speaker: {SENDER_NAME}
- Source Language: {FROM_LANG}
- Target Language: {TO_LANG}
- Intended Tone/Register: {TARGET_TONE}

# Constraints
1. Contextual Awareness: Use the [Conversation History] to resolve pronouns (it, that, etc.) and maintain consistency.
2. Personalization & Tone: Ensure all grammatical agreements and self-referential terms reflect speaker's gender. Faithfully replicate the intended tone, maintaining the formality, emotional nuance, and unique style of the source text without neutralizing it.
3. Natural Flow: Keep the translation concise and natural for a chat environment. Avoid "translationese".
4. Strictly avoid over-translation: Do not add extra information not present in the source text.
5. Output ONLY the translated text, no explanations.`;

export const CONVERSATION_EVALUATION_PROMPT_TEMPLATE = `# Role: Expert Conversation Auditor

# Participants:
- Participant A: [Name: {ME_NAME}, Gender: {ME_GENDER}, Language: {ME_LANG}]
- Participant B: [Name: {PART_NAME}, Gender: {PART_GENDER}, Language: {PART_LANG}]

# Recent Conversation Flow:
{HISTORY_BLOCK}

# Current Turn to Audit:
- Speaker: {SENDER_NAME}
- Source Language: {FROM_LANG}
- Target Language: {TO_LANG}
- Intended Tone/Register: {TARGET_TONE}

# Audit Criteria
1. **Contextual Inaccuracy**: Failure to correctly reflect the meaning based on the [Recent Conversation Flow]
2. **Semantic Error**: Objective misalignment with the source meaning or complete hallucinations.
3. **Grammatical Error**: Clear violations of target language grammar or syntax rules.
4. **Tone Failure**: A tone that is the opposite or significantly different from the [Intended Tone/Register].
5. **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 [Translation] 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. "analysis" and "text" MUST be in Simplified Chinese:
{
  "score": number,
  "analysis": "string",
  "suggestions": [
    { "id": 1, "text": "suggestion text", "importance": number }
  ]
}`;

export const CONVERSATION_REFINEMENT_PROMPT_TEMPLATE = `# Role: Professional Conversation Editor

# Participants:
- Participant A: [Name: {ME_NAME}, Gender: {ME_GENDER}, Language: {ME_LANG}]
- Participant B: [Name: {PART_NAME}, Gender: {PART_GENDER}, Language: {PART_LANG}]

# Recent Conversation Flow:
{HISTORY_BLOCK}

# Current Turn to Refine:
- Speaker: {SENDER_NAME}
- Source Language: {FROM_LANG}
- Target Language: {TO_LANG}
- Intended Tone/Register: {TARGET_TONE}

# Instructions:
1. Carefully review the [Suggestions] and apply the requested improvements to the [Current Translation] while ensuring it fits naturally into the [Recent Conversation Flow].
2. Ensure that the refined translation remains semantically identical to the [Source Text].
3. Maintain the speaker's gender 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 {TO_LANG} translation, without any additional explanations, notes, or commentary.`;

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 chatSystemPromptTemplate = useLocalStorage('chat-system-prompt-template', CONVERSATION_SYSTEM_PROMPT_TEMPLATE);
  const chatEvaluationPromptTemplate = useLocalStorage('chat-evaluation-prompt-template', CONVERSATION_EVALUATION_PROMPT_TEMPLATE);
  const chatRefinementPromptTemplate = useLocalStorage('chat-refinement-prompt-template', CONVERSATION_REFINEMENT_PROMPT_TEMPLATE);

  // 存储整个对象以保持一致性
  const sourceLang = useLocalStorage<Language>('source-lang-v2', LANGUAGES[0]);
  const targetLang = useLocalStorage<Language>('target-lang-v2', LANGUAGES[4]);

  // 按源语言分别存储身份和语气，实现基于语言自动切换
  const speakerIdentityMap = useLocalStorage<Record<string, string>>('speaker-identity-map', {});
  const toneRegisterMap = useLocalStorage<Record<string, string>>('tone-register-map', {});

  const speakerIdentity = computed({
    get: () => speakerIdentityMap.value[sourceLang.value.code] || SPEAKER_IDENTITY_OPTIONS[0].value,
    set: (val) => { speakerIdentityMap.value[sourceLang.value.code] = val; }
  });

  const toneRegister = computed({
    get: () => toneRegisterMap.value[sourceLang.value.code] || TONE_REGISTER_OPTIONS[0].value,
    set: (val) => { toneRegisterMap.value[sourceLang.value.code] = val; }
  });

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