/* Tab 4: Demographic Details */
function TabDemographic({ c, mode }) {
  const kv = (rows) => (
    <div style={{display:"flex", flexDirection:"column", gap:0}}>
      {rows.map(([k,v], i)=>(
        <div key={i} style={{display:"grid", gridTemplateColumns:"40% 1fr", padding:"8px 0", borderBottom: i<rows.length-1?"1px dashed var(--divider)":"none", fontSize:12}}>
          <span style={{color:"var(--ink-3)"}}>{k}</span>
          <span style={{color:"var(--ink)", fontWeight:500}}>{v || "—"}</span>
        </div>
      ))}
    </div>
  );

  return (
    <div className="l-split3" style={{display:"grid", gridTemplateColumns:"minmax(0,1fr) minmax(0,1fr) minmax(0,1fr)", gap:12}}>
      <Card title="Personal">
        {kv([
          ["Full name", c.full_name],
          ["Gender", c.gender==="F"?"Female":"Male"],
          ["Date of birth", fmtDate(c.dob) + " · " + c.age + "y"],
          ["PAN", <span key="pan" className="mono">{c.pan || c.nic}</span>],
          ["Nationality", c.nationality],
          ["Marital status", c.marital_status],
          ["Dependents", c.number_of_dependents],
          ["Education", c.education]
        ])}
      </Card>

      <Card title="Contact & Address">
        {kv([
          ["Mobile", <span key="p" className="mono">{c.phone}</span>],
          ["Email", c.email],
          ["Address 1", c.address_line1],
          ["Address 2", c.address_line2],
          ["City", c.city],
          ["Region", c.region],
          ["Residence type", c.residence_type],
          ["Residence stability", c.residence_category || "—"]
        ])}
      </Card>

      <Card title="Employment">
        {kv([
          ["Status", c.employment_status],
          ["Employer", c.employer_name],
          ["Designation", c.designation],
          ["Years at current job", (c.years_at_current_job||0)+"y"],
          ["Profession category", c.profession_category || "—"],
          ["Annual income", fmtINR(c.annual_income)],
          ["Monthly income", fmtINR(c.monthly_income)],
          ["Income band", c.income_band]
        ])}
      </Card>

      <Card title="Relationship" style={{gridColumn:"span 1"}}>
        {mode==="ETB" ? kv([
          ["Customer ID", <span key="id" className="mono">{c.customer_id}</span>],
          ["Customer type", "Existing-to-Bank (ETB)"],
          ["Customer since", c.customer_since],
          ["Tenure", Math.floor((c.relationship_tenure_months||0)/12)+"y "+((c.relationship_tenure_months||0)%12)+"m"],
          ["Branch", c.branch],
          ["Relationship manager", c.relationship_manager],
          ["Persona", c.assigned_persona],
          ["Behaviour cluster", "Cluster " + (c.behavior_cluster ?? "—")]
        ]) : kv([
          ["Applicant ID", <span key="id" className="mono">{c.applicant_id}</span>],
          ["Customer type", "New-to-Bank (NTB)"],
          ["Application date", fmtDate(c.application_date)],
          ["Source", c.source],
          ["KYC status", c.kyc_status],
          ["Requested product", c.requested_product],
          ["Requested amount", fmtINR(c.requested_amount)],
          ["Risk grade", c.risk_grade]
        ])}
      </Card>

      <Card title="Segment Indicators" style={{gridColumn:"span 2"}}>
        <div className="l-stats" style={{display:"grid", gridTemplateColumns:"repeat(4, 1fr)", gap:10}}>
          <Seg label="Income band" value={c.income_band} options={["low","mid","high"]}/>
          <Seg label="City tier" value={c.city_tier} options={["tier_3","tier_2","tier_1"]}/>
          <Seg label="Credit score band" value={c.credit_score_band} options={["low","low-mid","mid","high"]}/>
          <Seg label="FOIR band" value={c.foir_band} options={["low","moderate","high"]}/>
          <Seg label="Residence stability" value={c.residence_category||"moderate"} options={["unstable","moderate","stable"]}/>
          <Seg label="Job stability" value={c.job_stability_flag===0 && c.employment_status!=="employed" ? "n/a" : "stable"} options={["n/a","unstable","stable"]}/>
          <Seg label="Affluence" value={c.affluence_indicator?"affluent":"regular"} options={["regular","affluent"]}/>
          <Seg label="Life stage" value={c.age<30?"young":c.age<45?"mid":c.age<60?"mature":"senior"} options={["young","mid","mature","senior"]}/>
        </div>
      </Card>
    </div>
  );
}

function Seg({ label, value, options }) {
  return (
    <div style={{background:"var(--fill-4)", borderRadius:"var(--r-tile)", padding:"10px 12px"}}>
      <div className="eyebrow" style={{marginBottom:6}}>{label}</div>
      <div style={{display:"flex", gap:2, marginBottom:4}}>
        {options.map(o => {
          const on = o === value;
          return (
            <div key={o} style={{
              flex:1, height:6, borderRadius:"var(--r-pill)",
              background: on ? "var(--accent)" : "var(--fill-2)"
            }}/>
          );
        })}
      </div>
      <div style={{fontSize:11.5, fontWeight:600, color:"var(--ink)"}}>{value || "—"}</div>
    </div>
  );
}

window.TabDemographic = TabDemographic;
