// recommendations.js — Clothing recommendations + tip generation

(function () {
  // ───── Clothing logic based on feels-like temp + weather conditions
  function clothing(t, ctx) {
    // ctx: { feelsAvg, popMax, precipSum, windAvg, visibility, uvMax, isNight, riderProfile }
    const { feelsAvg, popMax, precipSum, windAvg, visibility, uvMax, isNight } = ctx;
    const list = [];

    // Jacket
    let jacket, base;
    if (feelsAvg >= 28) { jacket = t.j_mesh; base = t.i_tshirt; }
    else if (feelsAvg >= 20) { jacket = t.j_3season; base = t.i_tshirt; }
    else if (feelsAvg >= 12) { jacket = t.j_3season; base = t.i_long; }
    else if (feelsAvg >= 5) { jacket = t.j_leather; base = t.i_thermal; }
    else { jacket = t.j_winter; base = t.i_heattech; }
    list.push({ kind: t.gear_jacket, name: jacket, icon: "jacket" });
    list.push({ kind: t.gear_inner, name: base, icon: "shirt" });

    // Pants
    let pants;
    if (feelsAvg >= 22) pants = t.p_jeans;
    else if (feelsAvg >= 10) pants = t.p_riding;
    else pants = t.p_winter;
    list.push({ kind: t.gear_pants, name: pants, icon: "pants" });

    // Gloves
    let gloves;
    if (feelsAvg >= 25) gloves = t.g_summer;
    else if (feelsAvg >= 12) gloves = t.g_3season;
    else if (feelsAvg >= 2) gloves = t.g_winter;
    else gloves = t.g_heated;
    list.push({ kind: t.gear_gloves, name: gloves, icon: "glove" });

    // Helmet
    let helmet;
    if (feelsAvg >= 25 && !isNight) helmet = t.h_open;
    else if (popMax > 40 || (ctx.humidity && ctx.humidity > 80)) helmet = t.h_full_anti;
    else helmet = t.h_full;
    list.push({ kind: t.gear_helmet, name: helmet, icon: "helmet" });

    // Neck (windy or cold)
    if (feelsAvg < 15 || windAvg > 25) {
      const neck = feelsAvg < 8 ? t.n_warm : t.n_buff;
      list.push({ kind: t.gear_neck, name: neck, icon: "neck" });
    }

    // Rain
    if (popMax >= 60 || precipSum > 1) {
      list.push({ kind: t.gear_rain, name: t.r_required, icon: "rain" });
    } else if (popMax >= 30) {
      list.push({ kind: t.gear_rain, name: t.r_carry, icon: "rain" });
    }

    // Eyewear
    if (isNight) {
      list.push({ kind: t.gear_eye, name: t.e_clear, icon: "eye" });
    } else if (uvMax >= 7) {
      list.push({ kind: t.gear_eye, name: t.e_sun, icon: "eye" });
    }

    return list;
  }

  // ───── Tip generation
  function tips(t, ctx) {
    const { feelsAvg, popMax, gustMax, windAvg, visibility, uvMax, roadStatus, moonIllum, dayHours } = ctx;
    const out = [];

    if (uvMax >= 8) out.push({ text: t.tip_uv_high, level: "warn" });
    if (windAvg >= 25) out.push({ text: t.tip_wind_high, level: "warn" });
    else if (gustMax >= 45) out.push({ text: t.tip_gust, level: "warn" });
    if (popMax >= 60) out.push({ text: t.tip_rain_soon, level: "bad" });
    if (feelsAvg <= 5) out.push({ text: t.tip_cold, level: "warn" });
    else if (feelsAvg >= 32) out.push({ text: t.tip_hot, level: "warn" });
    if (visibility < 5000) out.push({ text: t.tip_fog, level: "bad" });
    if (roadStatus === "wet" || roadStatus === "damp") out.push({ text: t.tip_road_wet, level: "warn" });
    if (moonIllum != null && moonIllum < 25) out.push({ text: t.tip_moon_dark, level: "warn" });
    if (dayHours < 10.5) out.push({ text: t.tip_short_day, level: "warn" });

    return out;
  }

  // ───── Simple inline gear icons (line art)
  function GearIcon({ kind }) {
    const stroke = "currentColor";
    const sw = 1.6;
    const common = { fill: "none", stroke, strokeWidth: sw, strokeLinecap: "round", strokeLinejoin: "round" };
    const Wrap = ({ children }) => (
      <svg viewBox="0 0 24 24" width="20" height="20" {...common}>{children}</svg>
    );
    switch (kind) {
      case "jacket":
        return <Wrap><path d="M6 6l-2 4 2 1v9h12v-9l2-1-2-4-3-2-3 1-3-1z"/><path d="M9 5l3 4 3-4"/></Wrap>;
      case "shirt":
        return <Wrap><path d="M6 6l-2 3 3 2v9h10v-9l3-2-2-3-3-1-3 1-3-1z"/></Wrap>;
      case "pants":
        return <Wrap><path d="M7 4h10l1 7-2 9h-3l-1-9-1 9H8L6 11z"/></Wrap>;
      case "glove":
        return <Wrap><path d="M9 4v6M11 4v7M13 4v7M15 5v6M7 10v8a2 2 0 002 2h6a2 2 0 002-2v-7"/></Wrap>;
      case "helmet":
        return <Wrap><path d="M4 14a8 8 0 0116 0v3h-3l-1 2H8l-1-2H4z"/><path d="M9 14h6"/></Wrap>;
      case "neck":
        return <Wrap><path d="M6 8a6 6 0 0012 0M5 10c2 4 12 4 14 0M6 13c2 5 10 5 12 0"/></Wrap>;
      case "rain":
        return <Wrap><path d="M5 12a4 4 0 014-4 5 5 0 019-1 4 4 0 011 7H7a4 4 0 01-2-2zM8 18l-1 2M12 18l-1 2M16 18l-1 2"/></Wrap>;
      case "eye":
        return <Wrap><circle cx="7" cy="13" r="3"/><circle cx="17" cy="13" r="3"/><path d="M10 13h4M4 13l1-3h2M20 13l-1-3h-2"/></Wrap>;
      default:
        return null;
    }
  }

  window.REC = { clothing, tips, GearIcon };
})();
