import { useState, useEffect } from 'react';
import { execSync } from 'node:child_process';

export interface GitInfo {
  branch: string | null;
  cwd: string;
}

export function useGitInfo(): GitInfo {
  const [info, setInfo] = useState<GitInfo>({
    branch: null,
    cwd: process.cwd(),
  });

  useEffect(() => {
    try {
      const branch = execSync('git rev-parse --abbrev-ref HEAD', {
        encoding: 'utf-8',
        timeout: 3000,
        stdio: ['pipe', 'pipe', 'pipe'],
      }).trim();
      setInfo({ branch, cwd: process.cwd() });
    } catch {
      setInfo({ branch: null, cwd: process.cwd() });
    }
  }, []);

  return info;
}
