import type { ExtensionContext, Theme } from "@singularity-forge/pi-coding-agent";
import { truncateToWidth } from "@singularity-forge/pi-tui";
import { basename } from "node:path";
import { refreshGitStatus } from "./git.js";

export function renderHeader(theme: Theme, ctx: ExtensionContext, width: number): string[] {
  const th = theme;
  const git = refreshGitStatus(process.cwd());

  const projectName = basename(process.cwd());

  const leftParts: string[] = [];
  leftParts.push(th.bold(th.fg("accent", "SF")));
  leftParts.push(th.fg("text", projectName));

  if (git.branch) {
    leftParts.push(th.fg("dim", "on") + " " + th.fg("accent", git.branch));
  }

  const left = leftParts.join(" " + th.fg("dim", "│") + " ");

  const rightParts: string[] = [];
  if (ctx.model) {
    rightParts.push(th.fg("dim", ctx.model.provider) + " " + th.fg("text", ctx.model.id));
  }

  const right = rightParts.join(" " + th.fg("dim", "│") + " ");

  const line = left + " ".repeat(Math.max(1, width - left.length - right.length)) + right;
  return [truncateToWidth(line, width, th.fg("dim", "…"))];
}
