// #stock-adjustments — Task 5 of docs/superpowers/plans/2026-08-03-inventory.md.
//
// Two ways a quantity changes without a bill, an invoice or a build behind it,
// under one roof because they are the same idea at two moments in a company's
// life: what you already had (once, at the beginning) and what changed for a
// reason no document records (whenever the shelf and the books disagree).
//
// Tabs rather than two sidebar entries: the opening count is a one-time job
// that would otherwise sit in the nav forever, pointing at a screen nobody can
// use again. Same reasoning as the Reports screen's own tabs.

import { useState } from "react";
import { useCompany } from "../lib/company";
import type { NavTarget } from "../lib/useHashView";
import { AdjustmentForm } from "./AdjustmentForm";
import { OpeningQuantitiesForm } from "./OpeningQuantitiesForm";

type Tab = "adjust" | "opening";

export function StockAdjustmentsScreen({ navigate }: { navigate: (target: NavTarget) => void }) {
  const { selectedCompanyId } = useCompany();
  const [tab, setTab] = useState<Tab>("adjust");

  return (
    <div className="wrap">
      <header className="top">
        <h1>Stock adjustments</h1>
      </header>

      {!selectedCompanyId ? (
        <p className="msg-inline">Choose a company from the sidebar to adjust its stock.</p>
      ) : (
        <>
          <div className="tabs">
            <button
              type="button"
              aria-current={tab === "adjust" ? "page" : undefined}
              onClick={() => setTab("adjust")}
            >
              Correct a quantity
            </button>
            <button
              type="button"
              aria-current={tab === "opening" ? "page" : undefined}
              onClick={() => setTab("opening")}
            >
              What was already on the shelf
            </button>
          </div>

          {tab === "adjust" && <AdjustmentForm navigate={navigate} />}
          {tab === "opening" && <OpeningQuantitiesForm navigate={navigate} />}
        </>
      )}
    </div>
  );
}
