// #region AssistantChatIntegrationTest:Module [TYPE Function]
// @SEMANTICS: assistant, integration-test, ux-contract, i18n
// @PURPOSE: Contract-level integration checks for assistant chat panel implementation and localization wiring.
// @LAYER UI (Tests)
// @RELATION DEPENDS_ON -> [EXT:frontend:AssistantChatPanel]
// @INVARIANT: Critical assistant UX states and action hooks remain present in component source.
// @TEST_CONTRACT: AssistantChatSourceArtifacts -> ContractAssertions
// @TEST_SCENARIO: assistant_contract_and_i18n_intact -> Component semantic/UX anchors and locale keys stay consistent.
// @TEST_FIXTURE: assistant_locales_en_ru -> file:src/lib/i18n/locales/en/assistant.json + file:src/lib/i18n/locales/ru/assistant.json
// @TEST_EDGE: missing_component_anchor -> Missing DEF/UX tags fails contract assertion.
// @TEST_EDGE: missing_action_hook -> Missing confirm/cancel/open_task hooks fails integration assertion.
// @TEST_EDGE: missing_locale_key -> Missing assistant locale key in en/ru fails dictionary assertion.
// @TEST_INVARIANT: assistant_ux_contract_visible -> VERIFIED_BY: [assistant_contract_and_i18n_intact]

import { describe, it, expect } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const COMPONENT_PATH = path.resolve(
  process.cwd(),
  'src/lib/components/assistant/AssistantChatPanel.svelte',
);
const EN_LOCALE_PATH = path.resolve(
  process.cwd(),
  'src/lib/i18n/locales/en/assistant.json',
);
const RU_LOCALE_PATH = path.resolve(
  process.cwd(),
  'src/lib/i18n/locales/ru/assistant.json',
);

// #region readJson:Function [TYPE Function]
// @PURPOSE: Read and parse JSON fixture file from disk.
// @PRE: filePath points to existing UTF-8 JSON file.
// @POST: Returns parsed object representation.
function readJson(filePath) {
  return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
}
// #endregion readJson:Function

// #region assistant_chat_contract_tests:Function [TYPE Function]
// @PURPOSE: Validate assistant chat component contract and locale integration without DOM runtime dependency.
// @PRE: Component and locale files exist in expected paths.
// @POST: Contract checks guarantee assistant UI anchors and i18n wiring remain intact.
describe('AssistantChatPanel integration contract', () => {
  it('contains semantic anchors and UX contract tags', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('<!-- #region AssistantChatPanel');
    expect(source).toContain('@UX_STATE: LoadingHistory');
    expect(source).toContain('@UX_STATE: Sending');
    expect(source).toContain('@UX_STATE: Error');
    expect(source).toContain('@UX_FEEDBACK: Started operation surfaces task_id');
    expect(source).toContain('@UX_RECOVERY: User can retry command');
    expect(source).toContain('<!-- #endregion AssistantChatPanel -->');
  });

  it('keeps confirmation/task-tracking action hooks in place', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('if (action.type === "confirm" && message.confirmation_id)');
    expect(source).toContain('if (action.type === "cancel" && message.confirmation_id)');
    expect(source).toContain('if (action.type === "open_task" && action.target)');
    expect(source).toContain('openDrawerForTask(action.target)');
    expect(source).toContain('goto("/reports")');
  });

  it('guards first-message optimistic flow from premature history overwrite', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('if (panelVisible && initialized && conversationId && !loading)');
    expect(source).toContain('function appendLocalUserMessage(text, targetConversationId = conversationId)');
    expect(source).toContain('conversation_id: targetConversationId || null');
    expect(source).toContain('historyLoadVersion += 1;');
  });

  it('uses i18n bindings for assistant UI labels', () => {
    const source = fs.readFileSync(COMPONENT_PATH, 'utf-8');

    expect(source).toContain('$t.assistant?.title');
    expect(source).toContain('$t.assistant?.input_placeholder');
    expect(source).toContain('$t.assistant?.send');
    expect(source).toContain('$t.assistant?.states?.[message.state]');
    expect(source).toContain('$t.assistant?.open_task_drawer');
  });

  it('provides assistant locale keys in both en and ru dictionaries', () => {
    const en = readJson(EN_LOCALE_PATH);
    const ru = readJson(RU_LOCALE_PATH);

    expect(en.title).toBeTruthy();
    expect(en.send).toBeTruthy();
    expect(en.states.needs_confirmation).toBeTruthy();

    expect(ru.title).toBeTruthy();
    expect(ru.send).toBeTruthy();
    expect(ru.states.needs_confirmation).toBeTruthy();
  });
});
// #endregion assistant_chat_contract_tests:Function
// #endregion AssistantChatIntegrationTest:Module
