import { useMemo } from 'react';
import { Plus, Minus } from 'lucide-react';
import { parseUnifiedDiff } from '@/components/review/parse-diff';
import { FileCard } from '@/components/review/FileCard';
import type { ReviewComment, DiffLine } from '@/components/review/types';

const emptyComments = new Map<string, ReviewComment[]>();
const noop = () => {};

interface ErrandDiffViewProps {
  diff: string;
}

export function ErrandDiffView({ diff }: ErrandDiffViewProps) {
  const files = useMemo(() => parseUnifiedDiff(diff), [diff]);

  const totals = useMemo(() => {
    let additions = 0;
    let deletions = 0;
    for (const f of files) {
      additions += f.additions;
      deletions += f.deletions;
    }
    return { additions, deletions };
  }, [files]);

  if (files.length === 0) {
    return (
      <p className="text-sm text-muted-foreground">No changes — branch has no commits.</p>
    );
  }

  return (
    <div className="space-y-3">
      {/* Summary */}
      <div className="flex items-center gap-3 text-xs text-muted-foreground">
        <span>{files.length} file{files.length !== 1 ? 's' : ''} changed</span>
        {totals.additions > 0 && (
          <span className="flex items-center gap-0.5 text-diff-add-fg">
            <Plus className="h-3 w-3" />
            {totals.additions}
          </span>
        )}
        {totals.deletions > 0 && (
          <span className="flex items-center gap-0.5 text-diff-remove-fg">
            <Minus className="h-3 w-3" />
            {totals.deletions}
          </span>
        )}
      </div>

      {/* File cards */}
      {files.map((file) => (
        <FileCard
          key={file.newPath}
          file={file}
          detail={file}
          phaseId=""
          commitMode={true}
          commentsByLine={emptyComments}
          onAddComment={noop as (filePath: string, lineNumber: number, lineType: DiffLine['type'], body: string) => void}
          onResolveComment={noop}
          onUnresolveComment={noop}
        />
      ))}
    </div>
  );
}
