<template>
  <div class="flex h-full flex-col">
    <p v-if="loading" class="p-3 text-xs text-zinc-500">{{ t('fs.loading') }}</p>
    <p v-else-if="error" class="p-3 text-xs text-rose-400">{{ error }}</p>
    <p v-else-if="binary" class="p-3 text-xs text-zinc-500">{{ t('diff.binary') }}</p>
    <p v-else-if="tooLarge" class="p-3 text-xs text-amber-500">{{ t('diff.tooLarge') }}</p>
    <p v-else-if="parsed.hunks.length === 0" class="p-3 text-xs text-zinc-500">{{ t('diff.noChanges') }}</p>
    <div v-else class="min-h-0 flex-1 overflow-auto">
      <div class="flex items-center gap-3 border-b border-zinc-800 px-3 py-1 text-[11px] text-zinc-500">
        <span class="text-emerald-500">+{{ parsed.additions }}</span>
        <span class="text-rose-500">−{{ parsed.deletions }}</span>
        <span v-if="staged" class="rounded bg-emerald-950 px-1 text-emerald-400">{{ t('git.staged') }}</span>
      </div>
      <table class="w-full border-collapse font-mono text-xs leading-5">
        <tbody>
          <template v-for="(h, hi) in parsed.hunks" :key="hi">
            <tr class="bg-zinc-900/60 text-sky-300/80">
              <td class="select-none px-2 text-right text-zinc-700" />
              <td class="select-none px-2 text-right text-zinc-700" />
              <td class="whitespace-pre px-2">{{ h.header }}</td>
            </tr>
            <tr v-for="(l, li) in h.lines" :key="`${hi}-${li}`" :class="rowClass(l.type)">
              <td class="w-10 select-none px-2 text-right text-zinc-600">{{ l.oldLine ?? '' }}</td>
              <td class="w-10 select-none px-2 text-right text-zinc-600">{{ l.newLine ?? '' }}</td>
              <td class="whitespace-pre px-2">{{ marker(l.type) }}{{ l.content }}</td>
            </tr>
          </template>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { gitApi } from '../../lib/git-api';
import { parseUnifiedDiff, type DiffLineType, type ParsedDiff } from '../../lib/diff-parse';

const props = defineProps<{ repoId: string; wt: string; file: string; staged: boolean; version: number }>();
const { t } = useI18n();

const loading = ref(false);
const error = ref<string | null>(null);
const binary = ref(false);
const tooLarge = ref(false);
const parsed = ref<ParsedDiff>({ hunks: [], additions: 0, deletions: 0 });

async function load(): Promise<void> {
  loading.value = true;
  error.value = null;
  binary.value = false;
  tooLarge.value = false;
  try {
    const res = await gitApi.diff(props.repoId, props.wt, props.file, props.staged);
    binary.value = res.binary;
    tooLarge.value = res.tooLarge;
    parsed.value = res.binary || res.tooLarge ? { hunks: [], additions: 0, deletions: 0 } : parseUnifiedDiff(res.diff);
  } catch (err) {
    error.value = err instanceof Error ? err.message : String(err);
  } finally {
    loading.value = false;
  }
}

watch(() => [props.repoId, props.wt, props.file, props.staged, props.version], load, { immediate: true });

function rowClass(type: DiffLineType): string {
  if (type === 'add') return 'bg-emerald-950/40 text-emerald-200';
  if (type === 'del') return 'bg-rose-950/40 text-rose-200';
  return 'text-zinc-300';
}
function marker(type: DiffLineType): string {
  return type === 'add' ? '+' : type === 'del' ? '-' : ' ';
}
</script>
