/**
 * The five ways into a conversation.
 *
 * A brand with a finished tech pack, a sourcing manager looking for a second
 * supplier, a yarn trader, a facilities buyer kitting out a workforce and an
 * agent after a territory are five different people with five different
 * questions. Asking them all "what are you making?" serves none of them.
 *
 * This file is the single source of truth for those paths. The chooser, the
 * enquiry form and the API route all read it, so what the form renders and
 * what the endpoint accepts cannot drift apart.
 */

export type PathId =
  | "tech-pack"
  | "capacity"
  | "yarn"
  | "workforce"
  | "agent";

export interface PathField {
  name: string;
  label: string;
  placeholder?: string;
  /** Rendered as a select when options are present, otherwise a text input. */
  options?: readonly string[];
}

export interface EnquiryPath {
  id: PathId;
  /** First person, the way the visitor would put it rather than how we file it. */
  label: string;
  /** Two or three words, for the inline row under the hero. */
  short: string;
  blurb: string;
  /** Heading once they land on the form. */
  formTitle: string;
  formIntro: string;
  fields: readonly PathField[];
}

export const paths: readonly EnquiryPath[] = [
  {
    id: "tech-pack",
    label: "I have a tech pack and need a manufacturer",
    short: "a tech pack",
    blurb:
      "You know what you want made. We will cost it, sample it and give you a lead time.",
    formTitle: "Send us the tech pack.",
    formIntro:
      "Fabric, weight, colourways and quantities are the useful part. If you have a spec sheet or a reference garment, say so and we will tell you which of our lines it lands on.",
    fields: [
      {
        name: "division",
        label: "Which division",
        options: [
          "Knitwear",
          "Denim and woven",
          "Workwear and uniforms",
          "Not sure yet",
        ],
      },
      {
        name: "quantity",
        label: "Indicative quantity",
        placeholder: "e.g. 20,000 pcs per style",
      },
      {
        name: "delivery",
        label: "Delivery window",
        placeholder: "e.g. first shipment by March",
      },
    ],
  },
  {
    id: "capacity",
    label: "I need extra capacity or a second supplier",
    short: "extra capacity",
    blurb:
      "Already in production somewhere and want cover, or room to grow. Tell us what is running and how fast you need it moved.",
    formTitle: "How much capacity do you need, and by when?",
    formIntro:
      "Taking over a running programme is a different job from starting one. The more you can say about what is already in production, the more honestly we can answer on free capacity and ramp time.",
    fields: [
      {
        name: "category",
        label: "What is running now",
        placeholder: "e.g. jersey tees and fleece hoods",
      },
      {
        name: "volume",
        label: "Monthly volume needed",
        placeholder: "e.g. 60,000 pcs per month",
      },
      {
        name: "liveBy",
        label: "Needs to be live by",
        placeholder: "e.g. next season, or immediately",
      },
    ],
  },
  {
    id: "yarn",
    label: "I am buying yarn, not garments",
    short: "yarn",
    blurb:
      "Melange, CVC, top dye and denim yarn off two spinning units running 27,000 kg a day.",
    formTitle: "Which count, and which blend?",
    formIntro:
      "Spinning unit one runs 100% cotton and blends, unit two runs NE-10 to NE-40. Tell us the count and the blend and we will come back with availability and a price.",
    fields: [
      {
        name: "count",
        label: "Count range",
        placeholder: "e.g. NE-20 to NE-30",
      },
      {
        name: "blend",
        label: "Blend",
        options: [
          "100% cotton",
          "CVC",
          "CVS",
          "Melange",
          "Top dye",
          "Denim yarn",
          "Other",
        ],
      },
      {
        name: "shade",
        label: "Shade or melange reference",
        placeholder: "e.g. ecru, or a shade reference",
      },
      {
        name: "tonnage",
        label: "Monthly tonnage",
        placeholder: "e.g. 15 tons per month",
      },
    ],
  },
  {
    id: "workforce",
    label: "I am kitting out a workforce",
    short: "workwear",
    blurb:
      "Medical, kitchen, industrial and high visibility, built to survive industrial laundering.",
    formTitle: "Who is wearing it, and how often is it washed?",
    formIntro:
      "Workwear fails at the laundry and at the shoulder, not on the price list. Tell us the trade, the headcount and how the garments get washed, and we will spec cloth that survives it.",
    fields: [
      {
        name: "sector",
        label: "Trade or sector",
        options: [
          "Medical and healthcare",
          "Kitchen and hospitality",
          "Industrial",
          "High visibility",
          "Corporate uniform",
          "Other",
        ],
      },
      {
        name: "headcount",
        label: "Headcount to kit out",
        placeholder: "e.g. 400 staff across 6 sites",
      },
      {
        name: "laundering",
        label: "Laundering regime",
        placeholder: "e.g. industrial wash, 3 times a week",
      },
    ],
  },
  {
    id: "agent",
    label: "I want to represent you in my market",
    short: "an agency",
    blurb:
      "We are appointing agents and distributors, actively across South America and Latin America.",
    formTitle: "Which territory do you cover?",
    formIntro:
      "Tell us the territory, the buyers you already work with and the categories you sell into. We are appointing across South America and Latin America in particular.",
    fields: [
      {
        name: "territory",
        label: "Territory you cover",
        placeholder: "e.g. Brazil and Argentina",
      },
      {
        name: "represents",
        label: "Buyers you already represent",
        placeholder: "e.g. two national retail chains",
      },
      {
        name: "categories",
        label: "Categories you sell into",
        placeholder: "e.g. knitwear and denim bottoms",
      },
    ],
  },
];

export const pathIds = paths.map((p) => p.id);

export function getPath(id: string | null | undefined): EnquiryPath | null {
  if (!id) return null;
  return paths.find((p) => p.id === id) ?? null;
}

/**
 * Field names a given path is allowed to submit. The API route validates
 * against this rather than trusting whatever keys arrive in the body.
 */
export function allowedFieldsFor(id: string): string[] {
  const path = getPath(id);
  return path ? path.fields.map((f) => f.name) : [];
}
