<script lang="ts">
  // 홈 대시보드 — 데일리 홈 cockpit (확정 시안 dashboard-sage-3 안1 골격 + 안2 검토/파이프라인 위젯 + 안3 도메인 분포 한 줄).
  // 정보 흐름: 인사 → 오늘 요약 띠(검토 대기 + 디제스트 + 스탯) → 2열(좌: 빠른 캡처·활동 / 우: 학습·도메인 분포·고정).
  // 데이터는 전부 기존 엔드포인트 wiring(백엔드 변경 0). 학습 streak/복습 마감은 전용 엔드포인트 부재라 링크형으로 degrade.
  import { onMount } from 'svelte';
  import {
    dashboardSummary,
    refresh,
    type DashboardSummary,
    type PipelineStatus,
    type QueueLag,
  } from '$lib/stores/system';
  import { domainBgClass, domainLabel } from '$lib/utils/domainSlug';
  import { user } from '$lib/stores/auth';
  import { api } from '$lib/api';
  import { queueOverview, refreshQueueOverview } from '$lib/stores/queueOverview';
  import ProcessingFlowBoard from '$lib/components/ProcessingFlowBoard.svelte';
  import type { QueueOverview } from '$lib/types/queue';
  import EmptyState from '$lib/components/ui/EmptyState.svelte';
  import Skeleton from '$lib/components/ui/Skeleton.svelte';
  import {
    Scale, FileText, Pin, GraduationCap, Upload, Newspaper,
  } from 'lucide-svelte';
  import { addToast } from '$lib/stores/toast';

  let summary = $derived<DashboardSummary | null>($dashboardSummary);
  let loading = $derived(summary === null);

  // ─── 인사 헤더 ───
  const greetingName = $derived($user?.username ?? 'hyungi');
  const todayLabel = new Intl.DateTimeFormat('ko-KR', {
    year: 'numeric', month: 'long', day: 'numeric', weekday: 'long',
  }).format(new Date());

  // ─── 디제스트 헤드라인 (best-effort, 기존 /digest) ───
  interface DigestLead {
    topic_label: string;
    article_count: number;
    importance_score: number;
    country: string;
    date: string;
  }
  let digestLead = $state<DigestLead | null>(null);

  const COUNTRY_KO: Record<string, string> = {
    KR: '한국', JP: '일본', US: '미국', CN: '중국', DE: '독일',
    FR: '프랑스', GB: '영국', TW: '대만',
  };
  function countryKo(c: string): string {
    return COUNTRY_KO[c?.toUpperCase?.()] ?? c ?? '';
  }

  // ─── 도메인 분포 (best-effort, 기존 /documents/tree) ───
  interface DomainDist { name: string; count: number; }
  let domainDist = $state<DomainDist[]>([]);
  let domainTotal = $derived(domainDist.reduce((s, d) => s + d.count, 0));
  function domainCount(slugLike: string): number {
    // domainBgClass 와 동일 매핑 기준으로 특정 도메인 건수 추출 (스탯 띠용)
    const target = domainBgClass(slugLike);
    return domainDist.find((d) => domainBgClass(d.name) === target)?.count ?? 0;
  }

  // ─── 빠른 캡처 (기존 POST /memos) ───
  let captureText = $state('');
  let capturing = $state(false);
  async function quickCapture() {
    const content = captureText.trim();
    if (!content || capturing) return;
    capturing = true;
    try {
      await api('/memos/', { method: 'POST', body: JSON.stringify({ content }) });
      captureText = '';
      addToast('success', '메모 저장됨');
      void refresh(); // 메모 수 등 요약 즉시 갱신(60s 폴 기다리지 않음)
    } catch {
      addToast('error', '메모 저장 실패');
    } finally {
      capturing = false;
    }
  }
  function onCaptureKeydown(e: KeyboardEvent) {
    if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); void quickCapture(); }
  }

  // ─── 핀 고정 메모 (기존 /memos?pinned) ───
  let pinnedMemos = $state<any[]>([]);
  function pinTitle(memo: any): string {
    const firstLine = memo.content?.split('\n')[0]?.replace(/^#+\s*/, '').trim();
    return memo.title || firstLine || '메모';
  }

  onMount(async () => {
    // 핀 메모
    try {
      const res = await api<any>('/memos/?pinned=true&page_size=4&archived=false');
      pinnedMemos = res.items || [];
    } catch { /* 빈 배열 유지 */ }
    // 디제스트 최신 — countries→topics flatten 후 중요도 desc(동률 시 기사수 desc) top
    try {
      const d = await api<any>('/digest');
      const topics = (d.countries || []).flatMap((c: any) =>
        (c.topics || []).map((t: any) => ({ ...t, country: c.country })));
      topics.sort((a: any, b: any) =>
        (b.importance_score - a.importance_score) || (b.article_count - a.article_count));
      if (topics[0]) {
        digestLead = {
          topic_label: topics[0].topic_label,
          article_count: topics[0].article_count,
          importance_score: topics[0].importance_score,
          country: topics[0].country,
          date: d.digest_date,
        };
      }
    } catch { /* 디제스트 없으면 블록 자동 생략 */ }
    // 도메인 분포 — 트리 top-level 노드 건수
    try {
      const tree = await api<any[]>('/documents/tree');
      domainDist = (tree || [])
        .map((n) => ({ name: n.name as string, count: n.count as number }))
        .sort((a, b) => b.count - a.count);
    } catch { /* 분포 없으면 카드 자동 생략 */ }
  });

  // ─── 파이프라인 (기존 로직 재사용, 칩 요약 + 상세 접힘) ───
  const STAGE_ORDER = ['extract', 'stt', 'classify', 'embed', 'preview', 'thumbnail'] as const;
  const STAGE_LABEL: Record<string, string> = {
    extract: '추출', stt: '전사', classify: '분류', embed: '임베딩',
    preview: '미리보기', thumbnail: '썸네일',
  };

  // ─── 처리 머신 보드 (안2) + ETA (안5 라이트) — GET /api/queue/overview ───
  // 홈은 30s 폴링 (store 기본 60s 위에 추가 — inFlight 합치기로 중복 호출 0).
  // 백엔드 미배포/실패 시 store=null → 보드 자체가 조용히 생략 (silent 비차단).
  let queue = $derived<QueueOverview | null>($queueOverview);

  onMount(() => {
    void refreshQueueOverview();
    const handle = setInterval(() => void refreshQueueOverview(), 30_000);
    return () => clearInterval(handle);
  });

  interface PipelineRow {
    stage: string; label: string;
    pending: number; processing: number; failed: number; total: number;
    oldestPendingAgeSec: number | null;
  }

  function buildPipelineRows(items: PipelineStatus[], lag: QueueLag[]): PipelineRow[] {
    const lagMap = new Map(lag.map((l) => [l.stage, l]));
    const grouped = new Map<string, { pending: number; processing: number; failed: number; ageSec: number | null }>();
    for (const it of items) {
      const cur = grouped.get(it.stage) ?? { pending: 0, processing: 0, failed: 0, ageSec: null };
      if (it.status === 'pending') cur.pending += it.count;
      else if (it.status === 'processing') cur.processing += it.count;
      else if (it.status === 'failed') cur.failed += it.count;
      grouped.set(it.stage, cur);
    }
    for (const l of lag) {
      grouped.set(l.stage, {
        pending: l.pending, processing: l.processing, failed: l.failed,
        ageSec: l.oldest_pending_age_sec,
      });
    }
    const allStages = new Set([...grouped.keys(), ...lagMap.keys()]);
    const orderedStages = [
      ...STAGE_ORDER.filter((s) => allStages.has(s)),
      ...[...allStages].filter((s) => !STAGE_ORDER.includes(s as never)),
    ];
    return orderedStages.map((stage) => {
      const r = grouped.get(stage)!;
      return {
        stage, label: STAGE_LABEL[stage] ?? stage,
        pending: r.pending, processing: r.processing, failed: r.failed,
        total: r.pending + r.processing + r.failed,
        oldestPendingAgeSec: r.ageSec,
      };
    });
  }

  let pipelineRows = $derived(
    summary ? buildPipelineRows(summary.pipeline_status, summary.queue_lag ?? []) : []
  );
  let totalFailed = $derived(summary?.failed_count ?? 0);
  let totalPending = $derived(pipelineRows.reduce((s, r) => s + r.pending, 0));
  let totalProcessing = $derived(pipelineRows.reduce((s, r) => s + r.processing, 0));

  function formatTime(dateStr: string) {
    const d = new Date(dateStr);
    if (isNaN(d.getTime())) return ''; // 빈 문자열/유효하지 않은 created_at → 'Invalid Date' 회피
    const diff = Date.now() - d.getTime();
    if (diff < 60000) return '방금';
    if (diff < 3600000) return `${Math.floor(diff / 60000)}분 전`;
    if (diff < 86400000) return `${Math.floor(diff / 3600000)}시간 전`;
    if (diff < 604800000) return `${Math.floor(diff / 86400000)}일 전`;
    return d.toLocaleDateString('ko-KR', { month: 'short', day: 'numeric' });
  }
</script>

<div class="p-4 lg:p-8">
  <div class="max-w-[1680px] mx-auto">

    <!-- ═══ 인사 헤더 ═══ -->
    <div class="flex items-baseline gap-2.5 flex-wrap">
      <h1 class="text-2xl font-bold text-text tracking-tight">안녕하세요, {greetingName}</h1>
      <span class="text-sm text-dim">오늘도 지식 쌓는 날.</span>
    </div>
    <div class="text-xs text-faint mt-1 mb-6 tracking-wide">{todayLabel}</div>

    {#if loading}
      <!-- 스켈레톤 -->
      <div class="bg-surface border border-default rounded-card p-5 mb-5">
        <Skeleton w="w-40" h="h-10" />
        <Skeleton w="w-full" h="h-4" class="mt-4" />
        <Skeleton w="w-2/3" h="h-4" class="mt-2" />
      </div>
      <div class="grid grid-cols-1 lg:grid-cols-[1fr_360px] gap-5">
        <div class="space-y-5">
          <div class="bg-surface border border-default rounded-card p-5"><Skeleton w="w-24" h="h-4" /><Skeleton w="w-full" h="h-10" class="mt-3" /></div>
          <div class="bg-surface border border-default rounded-card p-5"><Skeleton w="w-24" h="h-4" /><Skeleton w="w-full" h="h-40" class="mt-3" /></div>
        </div>
        <div class="space-y-5">
          <div class="bg-surface border border-default rounded-card p-5"><Skeleton w="w-full" h="h-24" /></div>
          <div class="bg-surface border border-default rounded-card p-5"><Skeleton w="w-full" h="h-32" /></div>
        </div>
      </div>

    {:else if summary}

      <!-- ═══ 오늘 요약 띠 ═══ -->
      <div class="bg-surface border border-default rounded-card p-5 lg:p-6 mb-5">
        <!-- 검토 대기 + 디제스트 -->
        <div class="flex flex-col sm:flex-row items-stretch gap-5">
          <!-- 검토 대기 강조 -->
          <div class="flex flex-col justify-center sm:pr-6 sm:border-r border-default sm:min-w-[150px]">
            <span class="text-4xl font-extrabold tracking-tight leading-none {summary.inbox_count > 0 ? 'text-warning' : 'text-success'}">
              {summary.inbox_count.toLocaleString()}
            </span>
            <span class="text-[11px] text-dim mt-1.5 uppercase tracking-wide">검토 대기 문서</span>
            {#if summary.inbox_count > 0}
              <a href="/inbox" class="text-[11px] text-accent font-semibold mt-2 hover:underline">검토 시작 →</a>
            {:else}
              <span class="text-[11px] text-dim mt-2">미분류 없음</span>
            {/if}
          </div>

          <!-- 디제스트 톱 (best-effort) -->
          {#if digestLead}
            <a href="/digest" class="flex-1 flex flex-col justify-center gap-1.5 group">
              <div class="flex items-center gap-2">
                <span class="text-[10px] font-bold text-error bg-error/10 rounded px-1.5 py-0.5 uppercase tracking-wide">속보</span>
                <span class="text-[11px] text-faint">{digestLead.date} 브리핑</span>
              </div>
              <div class="text-[15px] font-semibold text-text leading-snug group-hover:text-accent transition-colors">
                {digestLead.topic_label}
              </div>
              <div class="text-[11px] text-dim">
                관련 기사 <strong class="text-text">{digestLead.article_count}건</strong>
                · 중요도 {digestLead.importance_score.toFixed(2)}
                · {countryKo(digestLead.country)}
              </div>
            </a>
          {:else}
            <a href="/news" class="flex-1 flex items-center gap-2 text-sm text-dim hover:text-accent transition-colors">
              <Newspaper size={16} /> 오늘의 뉴스 브리핑 보기 →
            </a>
          {/if}
        </div>

        <!-- 스탯 띠 -->
        <div class="flex flex-nowrap overflow-x-auto border-t border-default mt-4 pt-4">
          {@render stat((summary.documents_count ?? 0).toLocaleString(), '문서', 'text-accent')}
          {@render stat((summary.news_count ?? 0).toLocaleString(), '뉴스')}
          {#if domainTotal > 0}
            {@render stat(domainCount('Industrial_Safety').toLocaleString(), '산업안전', 'text-domain-safety')}
            {@render stat(domainCount('Engineering').toLocaleString(), '엔지니어링', 'text-domain-engineering')}
          {/if}
          {#if summary.category_counts?.library}
            {@render stat(summary.category_counts.library.toLocaleString(), '자료실')}
          {/if}
          {@render stat((summary.memos_count ?? 0).toLocaleString(), '메모')}
        </div>
      </div>

      <!-- ═══ 2열 본문 ═══ -->
      <div class="grid grid-cols-1 lg:grid-cols-[1fr_360px] gap-5 items-start">

        <!-- ─── 왼쪽 ─── -->
        <div class="space-y-5">

          <!-- 빠른 캡처 -->
          <div class="bg-surface border border-default rounded-card p-5">
            <div class="text-[11px] font-bold text-dim uppercase tracking-wider mb-3">빠른 캡처</div>
            <div class="flex gap-2 items-center">
              <input
                class="flex-1 h-9 border border-default rounded-md bg-bg text-text text-sm px-3.5 outline-none focus:border-accent transition-colors placeholder:text-faint"
                type="text"
                placeholder="메모 한 줄 남기기…"
                bind:value={captureText}
                onkeydown={onCaptureKeydown}
                disabled={capturing}
              />
              <button
                class="h-9 px-4 rounded-md bg-accent text-white text-xs font-semibold hover:bg-accent-hover transition-colors disabled:opacity-50 shrink-0"
                onclick={quickCapture}
                disabled={capturing || !captureText.trim()}
              >저장</button>
            </div>
            <div class="flex gap-2 mt-2.5">
              <a href="/documents" class="inline-flex items-center gap-1.5 text-[11px] text-accent-hover bg-accent/10 rounded-md px-2.5 py-1 hover:bg-accent/20 transition-colors">
                <Upload size={11} /> 파일 업로드
              </a>
            </div>
          </div>

          <!-- 최근 활동 타임라인 -->
          <div class="bg-surface border border-default rounded-card p-5">
            <div class="flex items-baseline justify-between mb-3">
              <span class="text-[11px] font-bold text-dim uppercase tracking-wider">최근 활동</span>
              <div class="flex items-center gap-3">
                {#if summary.law_alerts > 0}
                  <a href="/documents?source=law_monitor"
                    class="text-[11px] flex items-center gap-1 px-2.5 py-1 rounded-full bg-warning/10 text-warning border border-warning/20 hover:bg-warning/20 transition-colors">
                    <Scale size={11} /> 법령 {summary.law_alerts}
                  </a>
                {/if}
                <a href="/documents" class="text-[11px] text-accent hover:underline">전체 보기 →</a>
              </div>
            </div>

            {#if summary.recent_documents.length > 0}
              <div class="flex flex-col">
                {#each summary.recent_documents as doc, i (doc.id)}
                  <a href="/documents/{doc.id}"
                    class="grid grid-cols-[auto_14px_minmax(0,1fr)] gap-x-3 py-2.5 {i > 0 ? 'border-t border-default' : ''} group">
                    <div class="text-[10px] text-faint text-right pt-1 whitespace-nowrap tabular-nums w-14">{formatTime(doc.created_at)}</div>
                    <div class="flex flex-col items-center">
                      <span class="w-2 h-2 rounded-full mt-1.5 shrink-0 {domainBgClass(doc.ai_domain)}"></span>
                      {#if i < summary.recent_documents.length - 1}<span class="flex-1 w-px bg-default mt-1"></span>{/if}
                    </div>
                    <div class="pb-1 min-w-0">
                      <div class="text-[10px] font-bold uppercase tracking-wide text-dim mb-0.5">{domainLabel(doc.ai_domain)}</div>
                      <div class="text-[13px] text-text leading-snug group-hover:text-accent transition-colors truncate">{doc.title || '제목 없음'}</div>
                    </div>
                  </a>
                {/each}
              </div>
            {:else}
              <EmptyState
                icon={FileText}
                title="아직 문서가 없습니다"
                description="NAS PKM 폴더에 파일을 추가하면 자동으로 인덱싱됩니다."
              />
            {/if}
          </div>
        </div>

        <!-- ─── 오른쪽 ─── -->
        <div class="space-y-5">

          <!-- 학습 (streak/복습 마감은 백엔드 부재로 링크형 degrade) -->
          <a href="/study" class="block bg-surface border border-default rounded-card p-5 hover:bg-surface-hover transition-colors group">
            <div class="flex items-center justify-between">
              <span class="text-[11px] font-bold text-dim uppercase tracking-wider">학습</span>
              <GraduationCap size={16} class="text-faint" />
            </div>
            <div class="text-[15px] font-semibold text-text mt-3 group-hover:text-accent transition-colors">암기 노트 학습 시작 →</div>
            <div class="text-[11px] text-dim mt-1">검수함 · 복습함 · 암기카드</div>
          </a>

          <!-- 도메인 분포 + 파이프라인 -->
          {#if domainDist.length > 0}
            <div class="bg-surface border border-default rounded-card p-5">
              <div class="text-[11px] font-bold text-dim uppercase tracking-wider mb-2">도메인 분포</div>
              <div class="text-[11px] text-faint mb-3">전체 <strong class="text-base font-bold text-text tracking-tight align-baseline">{domainTotal.toLocaleString()}</strong>건</div>

              <!-- 분포 막대 -->
              <div class="flex gap-0.5 h-2 rounded mb-4 overflow-hidden">
                {#each domainDist as d (d.name)}
                  <div class="h-full rounded-sm {domainBgClass(d.name)}" style="width:{domainTotal > 0 ? (d.count / domainTotal) * 100 : 0}%"></div>
                {/each}
              </div>

              <div class="flex flex-col gap-1.5">
                {#each domainDist.slice(0, 6) as d (d.name)}
                  <a href="/documents?domain={encodeURIComponent(d.name)}" class="flex items-center gap-2 text-xs hover:text-accent transition-colors group">
                    <span class="w-2.5 h-2.5 rounded-sm shrink-0 {domainBgClass(d.name)}"></span>
                    <span class="flex-1 min-w-0 text-text truncate group-hover:text-accent">{domainLabel(d.name)}</span>
                    <span class="font-semibold text-dim tabular-nums">{d.count.toLocaleString()}</span>
                  </a>
                {/each}
              </div>

              <!-- 파이프라인 칩 (안2 흡수) -->
              <div class="flex items-center gap-1.5 flex-wrap mt-4 pt-3.5 border-t border-default">
                <span class="text-[10px] text-faint uppercase tracking-wide mr-1">파이프라인</span>
                {#if totalFailed > 0}<span class="text-[10px] font-bold rounded px-2 py-0.5 text-error bg-error/10">실패 {totalFailed}</span>{/if}
                {#if totalPending > 0}<span class="text-[10px] font-bold rounded px-2 py-0.5 text-warning bg-warning/10">대기 {totalPending}</span>{/if}
                {#if totalProcessing > 0}<span class="text-[10px] font-bold rounded px-2 py-0.5 text-success bg-success/10">처리중 {totalProcessing}</span>{/if}
                {#if totalFailed === 0 && totalPending === 0 && totalProcessing === 0}<span class="text-[10px] font-bold rounded px-2 py-0.5 text-success bg-success/10">정상</span>{/if}
              </div>
            </div>
          {/if}

          <!-- 고정 항목 -->
          {#if pinnedMemos.length > 0}
            <div class="bg-surface border border-default rounded-card p-5">
              <div class="flex items-baseline justify-between mb-3">
                <span class="text-[11px] font-bold text-dim uppercase tracking-wider">고정 항목</span>
                <a href="/memos" class="text-[11px] text-accent hover:underline">관리 →</a>
              </div>
              <div class="flex flex-col gap-2">
                {#each pinnedMemos as memo (memo.id)}
                  <a href="/memos" class="flex items-start gap-2.5 px-3 py-2.5 rounded-lg bg-bg hover:bg-surface-hover transition-colors">
                    <span class="text-[9px] font-bold rounded px-1.5 py-0.5 uppercase tracking-wide shrink-0 mt-0.5 text-accent-hover bg-accent/10">메모</span>
                    <span class="text-xs text-text leading-snug flex-1 min-w-0 break-words">{pinTitle(memo)}</span>
                    <Pin size={11} class="text-faint shrink-0 mt-0.5" />
                  </a>
                {/each}
              </div>
            </div>
          {/if}
        </div>
      </div>

      <!-- ═══ 처리 머신 보드 v2 — 파이프라인 흐름 + 상세 패널 + 실패 드로어 (ds-board-engines-1) ═══ -->
      {#if queue}
        <ProcessingFlowBoard overview={queue} />
      {/if}

    {/if}
  </div>
</div>

{#snippet stat(value: string, label: string, colorClass = 'text-text')}
  <div class="flex flex-col items-start px-4 first:pl-0 border-l border-default first:border-l-0 min-w-[64px]">
    <span class="text-xl font-bold tracking-tight leading-none {colorClass}">{value}</span>
    <span class="text-[10px] text-faint mt-1 uppercase tracking-wide">{label}</span>
  </div>
{/snippet}

