import { Network } from "lucide-react";
import Link from "next/link";
import { Avatar } from "@/components/ui/Avatar";
import { LINK_BUTTON_CLASS } from "@/components/ui/Button";

type MiniEmployee = { id: string; first_name: string; last_name: string; job_title: string; status?: string };

type OrganisationTabProps = {
  employeeId: string;
  manager: MiniEmployee | null;
  /** Nur gesetzt, wenn die zuständige Leitung abwesend ist und vertreten wird. */
  formalManager: MiniEmployee | null;
  directReports: MiniEmployee[];
  breadcrumb: string;
};

export function OrganisationTab({ employeeId, manager, formalManager, directReports, breadcrumb }: OrganisationTabProps) {
  return (
    <div className="flex flex-col gap-6">
      <div className="flex flex-wrap items-start justify-between gap-3">
        <div>
          <h3 className="text-xs font-semibold uppercase tracking-wide text-ink-muted">Organisationseinheit</h3>
          <p className="mt-1 text-sm text-ink">{breadcrumb}</p>
        </div>
        {/* ?focus= drives the same highlight/auto-expand path the org chart
            search already uses, so the person is unfolded and centred on
            arrival instead of the user hunting for them. */}
        <Link href={`/orgchart?focus=${employeeId}`} className={LINK_BUTTON_CLASS}>
          <Network className="h-4 w-4" />
          Im Organigramm anzeigen
        </Link>
      </div>

      <div>
        <h3 className="mb-2 text-xs font-semibold uppercase tracking-wide text-ink-muted">
          {formalManager ? "Führungskraft (Vertretung)" : "Führungskraft"}
        </h3>
        {formalManager && (
          <p className="mb-2 text-xs text-ink-muted">
            Zuständig ist {formalManager.first_name} {formalManager.last_name}; während der Abwesenheit übernimmt die nächste
            besetzte Ebene.
          </p>
        )}
        {manager ? (
          <Link href={`/employees/${manager.id}`} className="flex w-fit items-center gap-3 rounded border border-border p-3 hover:bg-surface">
            <Avatar firstName={manager.first_name} lastName={manager.last_name} />
            <div>
              <div className="text-sm font-semibold text-ink">
                {manager.first_name} {manager.last_name}
              </div>
              <div className="text-xs text-ink-muted">{manager.job_title}</div>
            </div>
          </Link>
        ) : (
          <p className="text-sm text-ink-muted">Keine Führungskraft (Geschäftsführung)</p>
        )}
      </div>

      <div>
        <h3 className="mb-2 text-xs font-semibold uppercase tracking-wide text-ink-muted">Direkte Berichte ({directReports.length})</h3>
        {directReports.length > 0 ? (
          <div className="grid grid-cols-1 gap-2 sm:grid-cols-2 lg:grid-cols-3">
            {directReports.map((r) => (
              <Link key={r.id} href={`/employees/${r.id}`} className="flex items-center gap-3 rounded border border-border p-3 hover:bg-surface">
                <Avatar firstName={r.first_name} lastName={r.last_name} />
                <div>
                  <div className="text-sm font-semibold text-ink">
                    {r.first_name} {r.last_name}
                  </div>
                  <div className="text-xs text-ink-muted">{r.job_title}</div>
                </div>
              </Link>
            ))}
          </div>
        ) : (
          <p className="text-sm text-ink-muted">Keine direkten Berichte.</p>
        )}
      </div>
    </div>
  );
}
