import { EMPTY_MODEL_CATALOG } from "./constants.ts";
import type {
  MessageRecord,
  ModelCatalogView,
  NavigationView,
  ProjectDashboardView,
  SessionSummaryView,
} from "./types.ts";

export const HARNESS_MODEL_CATALOG: ModelCatalogView = {
  ...EMPTY_MODEL_CATALOG,
  providers: [
    {
      provider_id: "openai",
      provider_label: "OpenAI",
      latest_model_ref: "openai/gpt-5.5",
      models: EMPTY_MODEL_CATALOG.models,
    },
    {
      provider_id: "local",
      provider_label: "Local",
      latest_model_ref: "local/gemma-4-31b-it",
      models: [
        {
          provider_id: "local",
          provider_label: "Local",
          model_id: "gemma-4-31b-it",
          model_ref: "local/gemma-4-31b-it",
          display_name: "gemma 4 31B it",
          status: "available",
          context_window_tokens: 16_000,
          max_output_tokens: 8_000,
          default_reasoning_effort: "medium",
          reasoning_efforts: ["low", "medium", "high"],
          token_estimator: "openai_tiktoken_o200k",
          runtime_supported: true,
          api_type: "openai_compatible",
          platform: "llama_cpp",
          server_url: "http://127.0.0.1:8080",
          source: "manual",
          local_reasoning_budget_ratio: 0.3,
        },
      ],
    },
  ],
  models: [
    ...EMPTY_MODEL_CATALOG.models,
    {
      provider_id: "openai",
      provider_label: "OpenAI",
      model_id: "gpt-5.4-mini",
      model_ref: "openai/gpt-5.4-mini",
      display_name: "GPT-5.4 Mini",
      status: "available",
      context_window_tokens: 400_000,
      max_output_tokens: 128_000,
      default_reasoning_effort: "medium",
      reasoning_efforts: ["none", "low", "medium", "high", "xhigh"],
      token_estimator: "openai_tiktoken_o200k",
      runtime_supported: true,
    },
    {
      provider_id: "local",
      provider_label: "Local",
      model_id: "gemma-4-31b-it",
      model_ref: "local/gemma-4-31b-it",
      display_name: "gemma 4 31B it",
      status: "available",
      context_window_tokens: 16_000,
      max_output_tokens: 8_000,
      default_reasoning_effort: "medium",
      reasoning_efforts: ["low", "medium", "high"],
      token_estimator: "openai_tiktoken_o200k",
      runtime_supported: true,
      api_type: "openai_compatible",
      platform: "llama_cpp",
      server_url: "http://127.0.0.1:8080",
      source: "manual",
      local_reasoning_budget_ratio: 0.3,
    },
  ],
};

export const HARNESS_NAVIGATION: NavigationView = {
  chats: [
    {
      id: "general",
      kind: "chat",
      title: "버틀러 클라이언트 기능 점검",
      last_activity_at: "2026-05-01T00:00:00.000Z",
      active_turn_state: "delivered",
      pinned: false,
      archived: false,
    },
  ],
  projects: [
    {
      id: "butler",
      display_name: "butler",
      last_activity_at: "2026-05-01T00:10:00.000Z",
      pinned: true,
      archived: false,
      sessions: [
        {
          id: "butler-client",
          kind: "project",
          project_id: "butler",
          title: "Desktop client polish",
          last_activity_at: "2026-05-01T00:10:00.000Z",
          pinned: false,
          archived: false,
        },
      ],
    },
  ],
  automations_summary: { total_count: 1, enabled_count: 1 },
  settings_summary: { profile_label: "Local Butler" },
};

export const HARNESS_PROJECT_DASHBOARD: ProjectDashboardView = {
  project: HARNESS_NAVIGATION.projects[0]!,
  stats: {
    active_sessions: 1,
    archived_sessions: 0,
    recent_messages_7d: 12,
    recent_messages_30d: 38,
    specs: 1,
    plans: 1,
  },
  activity: {
    days: Array.from({ length: 30 }, (_, index) => ({
      date: `2026-05-${String(index + 1).padStart(2, "0")}`,
      count: index % 5,
    })),
  },
  documents: [
    {
      id: "dashboard-spec",
      kind: "spec",
      document_type: "spec",
      title: "Desktop Client Experience Spec",
      safe_path_label:
        ".project-ledger/specs/butler-dedicated-client-app-experience.md",
      markdown:
        "## Desktop Client Experience\n\nThe dashboard renders Project Ledger specs as readable markdown.",
      updated_at: "2026-05-01T00:00:00.000Z",
    },
    {
      id: "dashboard-roadmap",
      kind: "plan",
      document_type: "roadmap",
      title: "Dedicated Client Roadmap",
      safe_path_label: ".project-ledger/roadmaps/dedicated-client.md",
      markdown:
        "## Dedicated Client Roadmap\n\nTrack navigation, composer, transport, and worker visibility slices.",
      updated_at: "2026-05-01T00:00:00.000Z",
    },
    {
      id: "dashboard-work",
      kind: "plan",
      document_type: "work",
      title: "Project Documents Work",
      safe_path_label: ".project-ledger/work/project-documents/work.md",
      status: "active",
      markdown:
        "## Project Documents Work\n\nExpose Project Ledger work as dashboard cards and composer attachments.",
      updated_at: "2026-05-01T00:00:00.000Z",
    },
    {
      id: "dashboard-task",
      kind: "plan",
      document_type: "task",
      title: "Composer Attachment Task",
      safe_path_label:
        ".project-ledger/work/project-documents/tasks/composer-attachments.md",
      status: "planned",
      markdown:
        "## Composer Attachment Task\n\nAttach selected Project Ledger documents to a draft chat.",
      updated_at: "2026-05-01T00:00:00.000Z",
    },
  ],
  generated_at: "2026-05-01T00:00:00.000Z",
};

export const HARNESS_MESSAGES: MessageRecord[] = [
  {
    id: "m1",
    chat_id: "butler-client",
    role: "user",
    text: "컴포저와 워커 상태가 겹치지 않도록 정리해줘.",
    status: "sent",
    turn_id: "turn-1",
    cursor: 1,
    created_at: "2026-05-01T00:07:12.000Z",
    updated_at: "2026-05-01T00:07:12.000Z",
  },
  {
    id: "m2",
    chat_id: "butler-client",
    role: "assistant",
    text: "컴포저는 대화 스크롤 바깥에 고정하고, 본문에는 컴포저 높이만큼 여백을 확보했습니다.\n\n- 좌측 사이드바는 0px까지 접힙니다.\n- 우측 패널은 세션 화면에서만 열립니다.\n- 워커 목록은 활성 세션의 비터미널 워커만 표시합니다.",
    status: "sent",
    turn_id: "turn-1",
    cursor: 2,
    created_at: "2026-05-01T00:08:05.000Z",
    updated_at: "2026-05-01T00:08:05.000Z",
    artifacts: [
      {
        id: "a1",
        file_id: "file-11111111-1111-4111-8111-111111111111",
        title: "butler-dedicated-client-composer.md",
        kind: "document",
        safe_path_label: "butler-dedicated-client-composer.md",
        url: "/message-files/file-11111111-1111-4111-8111-111111111111",
        size_bytes: 7800,
        created_at: "2026-05-01T00:08:05.000Z",
        open_action: "route",
      },
    ],
  },
  {
    id: "m3",
    chat_id: "butler-client",
    role: "user",
    text: "좋아. 진행 표시와 아이콘 상태도 실제 동작에 맞춰줘.",
    status: "sent",
    turn_id: "turn-2",
    cursor: 3,
    created_at: "2026-05-01T00:09:12.000Z",
    updated_at: "2026-05-01T00:09:12.000Z",
  },
  {
    id: "m4",
    chat_id: "butler-client",
    role: "assistant",
    text: "최신 응답에는 캔버스 마크를 사용하고, 이전 응답에는 같은 idle 형태의 SVG 마크를 유지하겠습니다.",
    status: "sent",
    turn_id: "turn-2",
    cursor: 4,
    created_at: "2026-05-01T00:09:42.000Z",
    updated_at: "2026-05-01T00:09:42.000Z",
  },
];

export const HARNESS_SUMMARY: SessionSummaryView = {
  session_id: "butler-client",
  turn_state: "thinking",
  latest_progress: {
    turn_id: "turn-2",
    safe_progress_rows: [
      {
        id: "p1",
        kind: "searched",
        state: "running",
        safe_label: "Explored 21 files, 3 searches, ran 1 command",
        safe_tool_name: "Search",
        safe_input_label: "UI contract scan",
        safe_detail_rows: [
          {
            id: "p1-d1",
            kind: "read",
            safe_label: "Read",
            safe_value: "Butler Ship Feature skill",
            state: "delivered",
          },
          {
            id: "p1-d2",
            kind: "read",
            safe_label: "Read",
            safe_value: "Frontend Component Design skill",
            state: "delivered",
          },
          {
            id: "p1-d3",
            kind: "searched",
            safe_label: "Searched",
            safe_value: "permission and composer activity components",
            state: "delivered",
          },
        ],
      },
      {
        id: "p2",
        kind: "edited",
        state: "pending",
        safe_label: "Conversation.tsx",
        safe_tool_name: "Edit",
        safe_input_label: "Conversation timeline activity rows",
      },
      {
        id: "p3",
        kind: "dispatch",
        state: "running",
        safe_label: "Visual smoke verifier",
        safe_tool_name: "Dispatch",
        safe_input_label: "Visual smoke verifier",
      },
    ],
  },
  branch_info: { branch_name: "main", safe_status: "workspace changed" },
  artifacts: [
    {
      id: "a1",
      file_id: "file-11111111-1111-4111-8111-111111111111",
      title: "butler-dedicated-client-composer.md",
      kind: "document",
      safe_path_label: "butler-dedicated-client-composer.md",
      url: "/message-files/file-11111111-1111-4111-8111-111111111111",
      size_bytes: 7800,
      created_at: "2026-05-01T00:00:00.000Z",
      open_action: "route",
    },
  ],
  skills_used: ["project-ledger", "butler-ship-feature"],
  automation_targets: [
    {
      automation_id: "auto-1",
      title: "Daily project pulse",
      interval_label: "1 hour",
    },
  ],
  context_details: {
    session_id: "butler-client",
    model_ref: "openai/gpt-5.5",
    provider_id: "openai",
    model_id: "gpt-5.5",
    token_count_source: "openai_tiktoken_o200k",
    used_tokens: 18400,
    budget_tokens: 258_000,
    max_output_tokens: 128_000,
    available_working_context_tokens: 218_400,
    used_working_context_tokens: 9_900,
    usable_user_message_tokens: 218_400,
    auto_compact_at_tokens: 196_560,
    hard_pressure_at_tokens: 214_032,
    ratio: 0.0713,
    status: "low",
    categories: [
      {
        id: "output-reserve",
        label: "Output Reserve",
        used_tokens: 8000,
        budget_tokens: 258_000,
        ratio: 0.031,
        safe_description: "Reserved for the assistant response.",
        source_kind: "output_reserve",
      },
      {
        id: "working",
        label: "Working Context",
        used_tokens: 3800,
        budget_tokens: 258_000,
        ratio: 0.0147,
        safe_description:
          "Recent conversation suffix and current turn material.",
        source_kind: "working_context",
      },
      {
        id: "role-contract",
        label: "Role Contract",
        used_tokens: 800,
        budget_tokens: 258_000,
        ratio: 0.0031,
        safe_description: "Stable runtime and role contract.",
        source_kind: "static_context",
      },
      {
        id: "safety-contract",
        label: "Safety Contract",
        used_tokens: 800,
        budget_tokens: 258_000,
        ratio: 0.0031,
        safe_description: "Stable guardrails and tool-use policy.",
        source_kind: "static_context",
      },
      {
        id: "settings",
        label: "Live Settings",
        used_tokens: 1100,
        budget_tokens: 258_000,
        ratio: 0.0043,
        safe_description: "Latest local settings and profile projection.",
        source_kind: "live_configuration",
      },
      {
        id: "runtime-rules",
        label: "Runtime Rules",
        used_tokens: 1100,
        budget_tokens: 258_000,
        ratio: 0.0043,
        safe_description: "Current runtime and model routing rules.",
        source_kind: "live_configuration",
      },
      {
        id: "ledger-specs",
        label: "Project Ledger Specs",
        used_tokens: 300,
        budget_tokens: 258_000,
        ratio: 0.0012,
        safe_description: "Canonical behavior requirements.",
        source_kind: "project_ledger",
      },
      {
        id: "ledger-plans",
        label: "Project Ledger Plans",
        used_tokens: 300,
        budget_tokens: 258_000,
        ratio: 0.0012,
        safe_description: "Current implementation plan context.",
        source_kind: "project_ledger",
      },
      {
        id: "ledger-reports",
        label: "Project Ledger Reports",
        used_tokens: 300,
        budget_tokens: 258_000,
        ratio: 0.0012,
        safe_description: "Recent validation and closeout notes.",
        source_kind: "project_ledger",
      },
      {
        id: "source-files",
        label: "Source Files",
        used_tokens: 350,
        budget_tokens: 258_000,
        ratio: 0.0014,
        safe_description: "Relevant source snippets.",
        source_kind: "codebase_snapshot",
      },
      {
        id: "test-files",
        label: "Test Files",
        used_tokens: 350,
        budget_tokens: 258_000,
        ratio: 0.0014,
        safe_description: "Regression coverage and smoke harness snippets.",
        source_kind: "codebase_snapshot",
      },
      {
        id: "conversation-tail",
        label: "Conversation Tail",
        used_tokens: 600,
        budget_tokens: 258_000,
        ratio: 0.0023,
        safe_description: "Recent user and assistant exchange history.",
        source_kind: "conversation_tail",
      },
      {
        id: "terminal-results",
        label: "Terminal Results",
        used_tokens: 150,
        budget_tokens: 258_000,
        ratio: 0.0006,
        safe_description: "Condensed command evidence.",
        source_kind: "tool_results",
      },
      {
        id: "browser-results",
        label: "Browser Results",
        used_tokens: 150,
        budget_tokens: 258_000,
        ratio: 0.0006,
        safe_description: "Condensed visual and interaction evidence.",
        source_kind: "tool_results",
      },
      {
        id: "validation-notes",
        label: "Validation Notes",
        used_tokens: 150,
        budget_tokens: 258_000,
        ratio: 0.0006,
        safe_description: "Latest test and lint observations.",
        source_kind: "tool_results",
      },
      {
        id: "user-profile",
        label: "User Profile",
        used_tokens: 150,
        budget_tokens: 258_000,
        ratio: 0.0006,
        safe_description: "Stable collaboration preferences.",
        source_kind: "user_profile",
      },
    ],
    updated_at: "2026-05-01T00:00:00.000Z",
  },
  worker_activity: [
    {
      worker_id: "worker-1",
      worker_label: "Worker A",
      phase: "executing",
      objective: "Refine desktop layout",
      status_line: "Executing: Aligning composer controls",
      terminal: false,
      supported_controls: ["cancel"],
    },
    {
      worker_id: "worker-2",
      worker_label: "Worker B",
      phase: "complete",
      objective: "Review visual harness",
      status_line: "Complete: Harness screenshot passed",
      terminal: true,
      supported_controls: [],
    },
  ],
};
