// Form schemas for Salaried + SEP flows
// Each field: { key, label, type, options, icon, monospace, required, section, placeholder, mask }

const SECTIONS_SALARIED = [
  { id: "identity",   title: "Identity",         subtitle: "Who the applicant is — PAN, Aadhaar, basic KYC",     icon: "user" },
  { id: "family",     title: "Family",           subtitle: "Parents, spouse, dependents",                             icon: "user" },
  { id: "contact",    title: "Contact & Address",subtitle: "Mobile, email, residence",                                icon: "phone" },
  { id: "employment", title: "Employment",       subtitle: "Employer, designation, experience",                       icon: "briefcase" },
  { id: "financials", title: "Financials",       subtitle: "Salary, obligations, bank",                               icon: "coins" },
  { id: "loan",       title: "Loan Request",     subtitle: "Amount, purpose, tenure",                                 icon: "creditCard" },
];

const FIELDS_SALARIED = [
  // Identity
  { key: "salutation",     label: "Salutation",      section: "identity",   options: ["Mr.", "Mrs.", "Ms."], required: true, derived: true },
  { key: "full_name",      label: "Full Name",       section: "identity",   icon: "user", required: true },
  { key: "pan",            label: "PAN",             section: "identity",   icon: "hash", monospace: true, required: true, mask: "AAAAA0000A" },
  { key: "aadhaar_masked", label: "Aadhaar (masked)",section: "identity",   icon: "lock", monospace: true, required: true, readOnly: true },
  { key: "dob",            label: "Date of Birth",   section: "identity",   icon: "clock", monospace: true, required: true, placeholder: "DD/MM/YYYY" },
  { key: "gender",         label: "Gender",          section: "identity",   options: ["Male", "Female", "Other"], required: true },
  { key: "nationality",    label: "Nationality",     section: "identity",   required: true },
  { key: "marital_status", label: "Marital Status",  section: "identity",   options: ["Single", "Married", "Divorced", "Widowed"], required: true },
  { key: "religion",       label: "Religion",        section: "identity" },

  // Family
  { key: "father_name",    label: "Father's Name",   section: "family", required: true },
  { key: "mother_name",    label: "Mother's Name",   section: "family", required: true },
  { key: "spouse_name",    label: "Spouse's Name",   section: "family", conditional: "married" },
  { key: "dependents",     label: "Dependents",      section: "family", type: "number", required: true, icon: "hash" },

  // Contact
  { key: "primary_mobile", label: "Primary Mobile",  section: "contact", icon: "phone", monospace: true, required: true },
  { key: "email",          label: "Email",           section: "contact", icon: "mail", required: true },
  { key: "current_address",label: "Current Address", section: "contact", icon: "home", required: true, fullWidth: true },
  { key: "residence_type", label: "Residence Type",  section: "contact", options: ["Owned", "Rented", "Company-provided", "Family-owned"], required: true },
  { key: "residing_since_months", label: "Residing Since (months)", section: "contact", type: "number" },
  { key: "permanent_address_same_as_current", label: "Permanent = Current", section: "contact", options: ["Yes", "No"], required: true },

  // Employment
  { key: "employer_name",     label: "Employer Name",   section: "employment", icon: "building", required: true },
  { key: "employer_type",     label: "Employer Type",   section: "employment", options: ["Government", "PSU", "Private", "MNC", "Listed"], required: true },
  { key: "designation",       label: "Designation",     section: "employment", required: true },
  { key: "experience_total_years",   label: "Total Experience (yrs)", section: "employment", type: "number", required: true, icon: "clock" },
  { key: "experience_current_years", label: "Current Experience (yrs)", section: "employment", type: "number", required: true, icon: "clock" },

  // Financials
  { key: "mode_of_salary",       label: "Mode of Salary",      section: "financials", options: ["Bank credit", "Cash", "Cheque"], required: true },
  { key: "salary_bank",          label: "Salary Bank",         section: "financials", icon: "building", required: true },
  { key: "gross_monthly_salary", label: "Gross Monthly (₹)",section: "financials", type: "number", required: true, icon: "coins", monospace: true },
  { key: "net_take_home_salary", label: "Net Take-Home (₹)",section: "financials", type: "number", required: true, icon: "coins", monospace: true },
  { key: "monthly_emis",         label: "Existing EMIs (₹)",section: "financials", type: "number", required: true, icon: "creditCard", monospace: true },

  // Loan
  { key: "loan_amount",   label: "Loan Amount (₹)", section: "loan", type: "number", required: true, icon: "coins", monospace: true },
  { key: "loan_purpose",  label: "Loan Purpose",         section: "loan",
    options: ["Personal", "Home", "Vehicle", "Education", "Medical", "Wedding", "Home Renovation", "Debt Consolidation", "Business", "Other"], required: true },
  { key: "tenure_months", label: "Tenure (months)",      section: "loan", type: "number", required: true, icon: "clock" },
];

const SECTIONS_SEP = [
  { id: "identity",   title: "Applicant Identity", subtitle: "Individual KYC — the person, not the business", icon: "user" },
  { id: "business",   title: "Business Entity",     subtitle: "Firm details, constitution, nature of business",    icon: "building" },
  { id: "family",     title: "Family",              subtitle: "Parents, spouse, dependents",                       icon: "user" },
  { id: "contact",    title: "Contact & Addresses", subtitle: "Residence + principal place of business",           icon: "phone" },
  { id: "business2",  title: "Business Registrations", subtitle: "GST, Udyam, trade licence",                      icon: "shield" },
  { id: "experience", title: "Experience",          subtitle: "Professional + business vintage",                   icon: "clock" },
  { id: "financials", title: "Financials",          subtitle: "Turnover, income, obligations, ITR",                icon: "trendingUp" },
  { id: "loan",       title: "Loan Request",        subtitle: "Amount, purpose, tenure, collateral",               icon: "creditCard" },
];

const FIELDS_SEP = [
  // Identity
  { key: "salutation",     label: "Salutation",      section: "identity", options: ["Mr.", "Mrs.", "Ms."], required: true, derived: true },
  { key: "full_name",      label: "Full Name",       section: "identity", icon: "user", required: true },
  { key: "pan",            label: "PAN (Individual)",section: "identity", icon: "hash", monospace: true, required: true },
  { key: "aadhaar_masked", label: "Aadhaar (masked)",section: "identity", icon: "lock", monospace: true, required: true, readOnly: true },
  { key: "dob",            label: "Date of Birth",   section: "identity", icon: "clock", monospace: true, required: true, placeholder: "DD/MM/YYYY" },
  { key: "gender",         label: "Gender",          section: "identity", options: ["Male", "Female", "Other"], required: true },
  { key: "nationality",    label: "Nationality",     section: "identity", required: true },
  { key: "marital_status", label: "Marital Status",  section: "identity", options: ["Single", "Married", "Divorced", "Widowed"], required: true },
  { key: "religion",       label: "Religion",        section: "identity" },

  // Business
  { key: "entity_name",    label: "Entity / Firm Name", section: "business", icon: "building", required: true },
  { key: "constitution",   label: "Constitution",    section: "business", options: ["Proprietorship", "Partnership", "Private Limited", "LLP", "HUF", "One Person Company"], required: true },
  { key: "entity_type",    label: "Entity Type",     section: "business", options: ["Manufacturer", "Trader", "Service Provider", "Retailer", "Wholesaler", "Professional Practice"], required: true },
  { key: "nature_of_business", label: "Nature of Business", section: "business", required: true },
  { key: "industry_sector",    label: "Industry Sector",    section: "business",
    options: ["Trading", "Manufacturing", "Services", "Retail", "Wholesale", "Healthcare", "Professional Services", "Hospitality", "Construction", "Transport & Logistics", "Agriculture", "Education", "Other"], required: true },
  { key: "sub_industry_sector", label: "Sub-sector",  section: "business" },

  // Family
  { key: "father_name",  label: "Father's Name", section: "family", required: true },
  { key: "mother_name",  label: "Mother's Name", section: "family", required: true },
  { key: "spouse_name",  label: "Spouse's Name", section: "family", conditional: "married" },
  { key: "dependents",   label: "Dependents",    section: "family", type: "number", required: true, icon: "hash" },

  // Contact
  { key: "primary_mobile",    label: "Primary Mobile",   section: "contact", icon: "phone", monospace: true, required: true },
  { key: "alternate_mobile",  label: "Alternate Mobile", section: "contact", icon: "phone", monospace: true },
  { key: "email",             label: "Email",            section: "contact", icon: "mail", required: true },
  { key: "current_address",   label: "Residential Address", section: "contact", icon: "home", required: true, fullWidth: true },
  { key: "residence_type",    label: "Residence Type",   section: "contact", options: ["Owned", "Rented", "Family-owned"], required: true },
  { key: "residing_since_months", label: "Residing Since (months)", section: "contact", type: "number" },
  { key: "permanent_address_same_as_current", label: "Permanent = Current", section: "contact", options: ["Yes", "No"], required: true },
  { key: "business_address",  label: "Business Address", section: "contact", icon: "building", required: true, fullWidth: true },

  // Registrations
  { key: "gst_number",           label: "GST Number", section: "business2", icon: "hash", monospace: true },
  { key: "gst_registered_since", label: "GST Registered Since", section: "business2", icon: "clock", monospace: true, placeholder: "DD/MM/YYYY" },
  { key: "udyam_msme_registration", label: "Udyam / MSME", section: "business2", monospace: true },
  { key: "business_pan",         label: "Business PAN", section: "business2", icon: "hash", monospace: true },
  { key: "professional_trade_licence_number", label: "Trade / Prof. Licence", section: "business2", monospace: true },
  { key: "number_of_employees",  label: "Employees", section: "business2", type: "number", icon: "user" },

  // Experience
  { key: "business_vintage_years",          label: "Business Vintage (yrs)",           section: "experience", type: "number", required: true, icon: "clock" },
  { key: "total_professional_experience_years", label: "Total Prof. Experience (yrs)", section: "experience", type: "number", required: true, icon: "clock" },
  { key: "current_business_experience_years",  label: "Current Business Exp. (yrs)",   section: "experience", type: "number", required: true, icon: "clock" },
  { key: "prior_business_experience",       label: "Prior Experience (note)",          section: "experience", fullWidth: true },

  // Financials
  { key: "annual_turnover",       label: "Annual Turnover (₹)", section: "financials", type: "number", required: true, icon: "trendingUp", monospace: true },
  { key: "gross_annual_income",   label: "Gross Annual Income (₹)", section: "financials", type: "number", required: true, icon: "coins", monospace: true },
  { key: "net_monthly_income",    label: "Net Monthly Income (₹)",  section: "financials", type: "number", required: true, icon: "coins", monospace: true },
  { key: "monthly_emis",          label: "Existing EMIs (₹)",       section: "financials", type: "number", required: true, icon: "creditCard", monospace: true },
  { key: "investment_in_business",label: "Capital Invested (₹)",    section: "financials", type: "number", icon: "coins", monospace: true },
  { key: "other_income_sources",  label: "Other Income",                  section: "financials", fullWidth: true },
  // itr_filing_status handled as special composite block

  // Loan
  { key: "loan_amount",   label: "Loan Amount (₹)", section: "loan", type: "number", required: true, icon: "coins", monospace: true },
  { key: "loan_purpose",  label: "Loan Purpose", section: "loan",
    options: ["Working Capital", "Business Expansion", "Machinery Purchase", "Equipment Purchase", "Inventory / Stock", "Shop / Office Renovation", "Commercial Property Purchase", "Debt Consolidation", "New Branch Setup", "Vehicle for Business", "Other"], required: true },
  { key: "tenure_months", label: "Tenure (months)", section: "loan", type: "number", required: true, icon: "clock" },
  { key: "collateral_offered", label: "Collateral", section: "loan", fullWidth: true },
];

// ── DEMO DATA ──────────────────────────────────────────────────────────
// Salaried: Rahul Sharma case from the prompt
const DEMO_SALARIED = {
  transcript: `Haan so the applicant is Rahul Sharma, male, 32 years old, married, Hindu, Indian. PAN A-B-C-D-E-1-2-3-4-F. Aadhaar last four 4521. DOB 15th March 1993. Father Ramesh Sharma, mother Sunita Devi, wife Priya Sharma, 2 dependents. Mobile 98765 43210, email rahul dot sharma at gmail dot com. Rented flat in HSR Layout Bengaluru 560102, residing since 3 years, permanent address same. Works at Infosys as Senior Engineer, listed company, total 8 years experience, 4 years at Infosys. Salary in HDFC Bank, gross 1.5 lakh, take home 1.2 lakh, existing car loan EMI 15000. Wants 5 lakh personal loan for wedding, 36 months tenure.`,
  durationSec: 52,
  fields: {
    salutation:       { value: "Mr.", c: 0.99, note: "Derived from gender = Male" },
    full_name:        { value: "Rahul Sharma", c: 0.98 },
    pan:              { value: "ABCDE1234F", c: 0.72, status: "spelled", note: "Spelled out — reconstructed & validated against PAN format" },
    aadhaar_masked:   { value: "XXXX-XXXX-4521", c: 0.96, note: "Last 4 only — never stored in full" },
    dob:              { value: "15/03/1993", c: 0.95 },
    gender:           { value: "Male", c: 0.99 },
    nationality:      { value: "Indian", c: 0.98 },
    marital_status:   { value: "Married", c: 0.97 },
    religion:         { value: "Hindu", c: 0.94 },
    father_name:      { value: "Ramesh Sharma", c: 0.95 },
    mother_name:      { value: "Sunita Devi", c: 0.93 },
    spouse_name:      { value: "Priya Sharma", c: 0.94 },
    dependents:       { value: 2, c: 0.96 },
    primary_mobile:   { value: "9876543210", c: 0.97 },
    email:            { value: "rahul.sharma@gmail.com", c: 0.88, note: "Reconstructed ‘dot’ → . and ‘at’ → @" },
    current_address:  { value: "HSR Layout, Bengaluru, 560102", c: 0.82, note: "‘Blr’ expanded to ‘Bengaluru’" },
    residence_type:   { value: "Rented", c: 0.96 },
    residing_since_months: { value: 36, c: 0.92 },
    permanent_address_same_as_current: { value: "Yes", c: 0.95 },
    employer_name:    { value: "Infosys", c: 0.97 },
    employer_type:    { value: "Listed", c: 0.80, note: "Stated as ‘listed company’" },
    designation:      { value: "Senior Engineer", c: 0.94 },
    experience_total_years:   { value: 8, c: 0.95 },
    experience_current_years: { value: 4, c: 0.93 },
    mode_of_salary:   { value: "Bank credit", c: 0.98 },
    salary_bank:      { value: "HDFC Bank", c: 0.97, note: "‘HDFC’ normalized to ‘HDFC Bank’" },
    gross_monthly_salary: { value: 150000, c: 0.94, note: "‘1.5 lakh’ → 150000" },
    net_take_home_salary: { value: 120000, c: 0.94 },
    monthly_emis:     { value: 15000, c: 0.93 },
    loan_amount:      { value: 500000, c: 0.96, note: "‘5 lakh’ → 500000" },
    loan_purpose:     { value: "Wedding", c: 0.95 },
    tenure_months:    { value: 36, c: 0.97 },
  },
};

// SEP: Dr. Rajesh Mehta case from the prompt. Throw in a couple of low-conf gaps
// to show the missing-fields warning + low-conf callout moments.
const DEMO_SEP = {
  transcript: `Applicant is Dr. Rajesh Mehta, male, 48 years old, married, Hindu, Indian. Runs Mehta Diagnostics, a proprietorship, service provider, pathology lab basically. PAN A-B-C-P-M-1-2-3-4-K. Aadhaar last four 8734. DOB 12th August 1977. Father Mohanlal Mehta, mother Geeta Devi, wife Sunita Mehta, 3 dependents. Mobile plus 91 98111 22334, email drmehta at gmail dot com. Owns a flat in Vasant Kunj Delhi 110070, living there since 10 years, permanent address same. Business at C-45 Lajpat Nagar Delhi 110024, running since 2010 so 16 years vintage. Nature of business is pathology lab, healthcare sector, sub sector diagnostic lab. GST number 07ABCPM1234K1Z5, registered since 2017. Udyam registered UDYAM-DL-03-0045678. Around 8 employees. Turnover last FY was 1.8 crore. MCI registration number DMC-45678. Total medical experience 22 years, running this lab for 16 years, before this worked at AIIMS for 6 years. Gross annual income as per ITR 28 lakh, net monthly take home about 1.8 lakh, existing EMI 25000 for car loan. Invested around 80 lakh in the business. ITR filed last 3 years, last year 28 lakh, year before 24 lakh. Needs 40 lakh loan for machinery purchase, new MRI unit, 60 months tenure. Offering property at Vasant Kunj as collateral.`,
  durationSec: 78,
  fields: {
    salutation:   { value: "Mr.", c: 0.99, note: "Derived from gender = Male" },
    full_name:    { value: "Rajesh Mehta", c: 0.97 },
    pan:          { value: "ABCPM1234K", c: 0.68, status: "spelled", note: "Spelled out — 4th char ‘P’ confirms individual PAN" },
    aadhaar_masked:{ value: "XXXX-XXXX-8734", c: 0.95 },
    dob:          { value: "12/08/1977", c: 0.94 },
    gender:       { value: "Male", c: 0.99 },
    nationality:  { value: "Indian", c: 0.97 },
    marital_status:{ value: "Married", c: 0.96 },
    religion:     { value: "Hindu", c: 0.93 },
    entity_name:  { value: "Mehta Diagnostics", c: 0.95 },
    constitution: { value: "Proprietorship", c: 0.96 },
    entity_type:  { value: "Service Provider", c: 0.88 },
    nature_of_business: { value: "Pathology Lab", c: 0.92 },
    industry_sector: { value: "Healthcare", c: 0.94 },
    sub_industry_sector: { value: "Diagnostic Lab", c: 0.90 },
    father_name: { value: "Mohanlal Mehta", c: 0.94, note: "Surname forced to match applicant (‘Mehta’)" },
    mother_name: { value: "Geeta Devi", c: 0.92 },
    spouse_name: { value: "Sunita Mehta", c: 0.93 },
    dependents:  { value: 3, c: 0.96 },
    primary_mobile: { value: "919811122334", c: 0.93, note: "‘+91’ prefix preserved" },
    alternate_mobile: { value: null, c: null },
    email:       { value: "drmehta@gmail.com", c: 0.86 },
    current_address: { value: "Vasant Kunj, Delhi, 110070", c: 0.84 },
    residence_type: { value: "Owned", c: 0.97 },
    residing_since_months: { value: 120, c: 0.91 },
    permanent_address_same_as_current: { value: "Yes", c: 0.95 },
    business_address: { value: "C-45, Lajpat Nagar, Delhi, 110024", c: 0.85 },
    gst_number: { value: "07ABCPM1234K1Z5", c: 0.65, status: "spelled", note: "15-char GSTIN — reconstructed & PAN-segment match confirmed" },
    gst_registered_since: { value: "DD/MM/2017", c: 0.62, status: "low", note: "Only year captured — month/day missing" },
    udyam_msme_registration: { value: "UDYAM-DL-03-0045678", c: 0.88 },
    business_pan: { value: null, c: null, note: "Proprietorship — uses individual PAN" },
    professional_trade_licence_number: { value: "DMC-45678", c: 0.86 },
    number_of_employees: { value: 8, c: 0.90 },
    business_vintage_years: { value: 16, c: 0.94, note: "‘Since 2010’ → 16 years" },
    total_professional_experience_years: { value: 22, c: 0.95 },
    current_business_experience_years: { value: 16, c: 0.93 },
    prior_business_experience: { value: "6 years at AIIMS before starting own lab", c: 0.87 },
    annual_turnover: { value: 18000000, c: 0.93, note: "‘1.8 crore’ → 18000000" },
    gross_annual_income: { value: 2800000, c: 0.91, note: "‘28 lakh’ → 2800000" },
    net_monthly_income: { value: 180000, c: 0.92 },
    monthly_emis: { value: 25000, c: 0.93 },
    investment_in_business: { value: 8000000, c: 0.88 },
    other_income_sources: { value: null, c: null },
    loan_amount: { value: 4000000, c: 0.96, note: "‘40 lakh’ → 4000000" },
    loan_purpose: { value: "Machinery Purchase", c: 0.91, note: "‘New MRI unit’ mapped to Machinery Purchase" },
    tenure_months: { value: 60, c: 0.95 },
    collateral_offered: { value: "Property at Vasant Kunj", c: 0.87 },
  },
  itr: { filed: true, years_filed: 3, last_year_income: 2800000, prior_year_income: 2400000, c: 0.90 },
};

// Formatted currency for display (no commas in inputs but show commas in summary)
const formatINR = (n) => {
  if (n === null || n === undefined || n === "") return "";
  const num = Number(n);
  if (!isFinite(num)) return String(n);
  // Indian comma grouping
  const s = Math.abs(num).toString();
  let last3 = s.slice(-3), rest = s.slice(0, -3);
  if (rest) rest = rest.replace(/\B(?=(\d{2})+(?!\d))/g, ",");
  return (num < 0 ? "-" : "") + (rest ? rest + "," : "") + last3;
};

Object.assign(window, {
  SECTIONS_SALARIED, FIELDS_SALARIED, SECTIONS_SEP, FIELDS_SEP,
  DEMO_SALARIED, DEMO_SEP, formatINR,
});
