// Consumer Attributes (CA) — brand-level data model + helpers
// CAs are the middle layer between question-options and consumer profiles.
// Each CA has a type (categorical / numeric / boolean) and a list of values.
// Study questions can be MAPPED to a CA — each question option maps to a CA value.

const CONSUMER_ATTRIBUTES = [
  {
    id: 'ca-age', name: 'Age', type: 'numeric', unit: 'years',
    description: 'Respondent age, normalized into brand-standard buckets.',
    values: [
      { id: 'v-age-18-24', label: '18–24', range: [18, 24] },
      { id: 'v-age-25-34', label: '25–34', range: [25, 34] },
      { id: 'v-age-35-44', label: '35–44', range: [35, 44] },
      { id: 'v-age-45-54', label: '45–54', range: [45, 54] },
      { id: 'v-age-55plus', label: '55+',  range: [55, 120] },
    ],
    usedInStudies: 27, respondentsTagged: 18420,
  },
  {
    id: 'ca-gender', name: 'Gender', type: 'categorical',
    description: 'Self-reported gender identity.',
    values: [
      { id: 'v-g-female', label: 'Female' },
      { id: 'v-g-male', label: 'Male' },
      { id: 'v-g-nonbinary', label: 'Non-binary' },
      { id: 'v-g-noanswer', label: 'Prefer not to say' },
    ],
    usedInStudies: 26, respondentsTagged: 18120,
  },
  {
    id: 'ca-income', name: 'Household income', type: 'numeric', unit: 'USD/yr',
    description: 'Annual household income, normalized into brand-standard buckets.',
    values: [
      { id: 'v-inc-low',  label: '<$50k', range: [0, 49999] },
      { id: 'v-inc-mid1', label: '$50–75k', range: [50000, 74999] },
      { id: 'v-inc-mid2', label: '$75–125k', range: [75000, 124999] },
      { id: 'v-inc-high', label: '$125k+', range: [125000, 5000000] },
    ],
    usedInStudies: 19, respondentsTagged: 14210,
  },
  {
    id: 'ca-activity', name: 'Primary activity / sport', type: 'categorical',
    description: 'Sports the respondent participates in regularly. Multi-select.',
    values: [
      { id: 'v-act-run', label: 'Running' },
      { id: 'v-act-trail', label: 'Trail running' },
      { id: 'v-act-hike', label: 'Hiking' },
      { id: 'v-act-gym', label: 'Gym / training' },
      { id: 'v-act-basketball', label: 'Basketball' },
      { id: 'v-act-tennis', label: 'Tennis' },
      { id: 'v-act-pickleball', label: 'Pickleball' },
      { id: 'v-act-yoga', label: 'Yoga' },
      { id: 'v-act-climb', label: 'Climbing' },
    ],
    usedInStudies: 22, respondentsTagged: 15880,
  },
  {
    id: 'ca-mileage', name: 'Weekly run mileage', type: 'numeric', unit: 'mi/wk',
    description: 'Self-reported running mileage per week.',
    values: [
      { id: 'v-mi-0', label: '0 mi', range: [0, 0] },
      { id: 'v-mi-1-10', label: '1–10 mi', range: [1, 10] },
      { id: 'v-mi-11-20', label: '11–20 mi', range: [11, 20] },
      { id: 'v-mi-21-40', label: '21–40 mi', range: [21, 40] },
      { id: 'v-mi-40plus', label: '40+ mi', range: [41, 500] },
    ],
    usedInStudies: 8, respondentsTagged: 4920,
  },
  {
    id: 'ca-purchasefreq', name: 'Athletic shoe purchase frequency', type: 'categorical',
    description: 'How often the respondent buys athletic footwear.',
    values: [
      { id: 'v-pf-monthly', label: 'Monthly' },
      { id: 'v-pf-quarterly', label: 'Every few months' },
      { id: 'v-pf-2y', label: '1–2× per year' },
      { id: 'v-pf-rare', label: 'Less than yearly' },
    ],
    usedInStudies: 17, respondentsTagged: 11340,
  },
  {
    id: 'ca-lastadidas', name: 'Last Competitor Brand purchase', type: 'categorical',
    description: 'How recently the respondent last bought Competitor Brand.',
    values: [
      { id: 'v-la-6mo', label: 'Last 6 months' },
      { id: 'v-la-12mo', label: '6–12 months ago' },
      { id: 'v-la-24mo', label: '1–2 years ago' },
      { id: 'v-la-old', label: '2+ years ago' },
      { id: 'v-la-never', label: 'Never' },
    ],
    usedInStudies: 14, respondentsTagged: 9210,
  },
  {
    id: 'ca-spend', name: 'Average pair spend', type: 'numeric', unit: 'USD',
    description: 'Average price paid per pair of athletic footwear.',
    values: [
      { id: 'v-sp-lt75',  label: '<$75',  range: [0, 74] },
      { id: 'v-sp-75-150', label: '$75–150', range: [75, 150] },
      { id: 'v-sp-150-225', label: '$150–225', range: [150, 225] },
      { id: 'v-sp-225plus', label: '$225+', range: [225, 5000] },
    ],
    usedInStudies: 11, respondentsTagged: 7890,
  },
  {
    id: 'ca-climate', name: 'Climate / region', type: 'categorical',
    description: 'Climate band the respondent lives in.',
    values: [
      { id: 'v-cl-temperate', label: 'Temperate' },
      { id: 'v-cl-wet', label: 'Wet / variable' },
      { id: 'v-cl-arid', label: 'Arid / hot' },
      { id: 'v-cl-cold', label: 'Cold / snow' },
    ],
    usedInStudies: 6, respondentsTagged: 3120,
  },
  {
    id: 'ca-social', name: 'Social-media follow frequency', type: 'categorical',
    description: 'How often the respondent follows footwear content on social.',
    values: [
      { id: 'v-soc-daily', label: 'Daily' },
      { id: 'v-soc-weekly', label: 'Weekly' },
      { id: 'v-soc-monthly', label: 'Monthly' },
      { id: 'v-soc-rare', label: 'Rarely' },
      { id: 'v-soc-never', label: 'Never' },
    ],
    usedInStudies: 9, respondentsTagged: 5680,
  },
];

// Sample question→CA mappings. Keys are CA value ids; values are arrays of
// {studyName, questionTitle, optionLabel} — used for the "Mapped from N study
// questions" expandable row in the Profile Detail and the CA author page.
const CA_VALUE_MAPPINGS = {
  // Age
  'v-age-18-24': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'What is your age?', optionLabel: '18–24' },
    { studyName: "Ultraboost Light — concept test", questionTitle: 'How old are you?', optionLabel: 'Under 25' },
    { studyName: "Originals Q4 — naming", questionTitle: 'Age', optionLabel: '18 to 24' },
  ],
  'v-age-25-34': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'What is your age?', optionLabel: '25–34' },
    { studyName: "Ultraboost Light — concept test", questionTitle: 'How old are you?', optionLabel: '25–34' },
  ],
  'v-age-35-44': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'What is your age?', optionLabel: '35–44' },
  ],
  // Activity
  'v-act-run': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'Which activities do you do regularly?', optionLabel: 'Running' },
    { studyName: "Ultraboost Light — concept test", questionTitle: 'What sports do you do?', optionLabel: 'Road running' },
  ],
  'v-act-trail': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'Which activities do you do regularly?', optionLabel: 'Trail running' },
    { studyName: "Terrex outdoor — needs test", questionTitle: 'Outdoor activities', optionLabel: 'Trail run' },
  ],
  'v-act-hike': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'Which activities do you do regularly?', optionLabel: 'Hiking' },
    { studyName: "Terrex outdoor — needs test", questionTitle: 'Outdoor activities', optionLabel: 'Hike' },
  ],
  'v-act-gym': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'Which activities do you do regularly?', optionLabel: 'Gym' },
  ],
  // Mileage
  'v-mi-11-20': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'How many miles do you run per week?', optionLabel: '15+' },
  ],
  'v-mi-21-40': [
    { studyName: "Ultraboost Light — concept test", questionTitle: 'Weekly running volume', optionLabel: '20–40 mi' },
  ],
  // Purchase freq
  'v-pf-monthly': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'How many pairs of athletic shoes have you bought in the last 6 months?', optionLabel: '4+' },
  ],
  'v-pf-quarterly': [
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'How many pairs of athletic shoes have you bought in the last 6 months?', optionLabel: '2' },
    { studyName: "FW25 trail runner — colorway validation", questionTitle: 'How many pairs of athletic shoes have you bought in the last 6 months?', optionLabel: '3' },
  ],
  // Last Competitor Brand
  'v-la-old': [
    { studyName: "Originals Q4 — naming", questionTitle: 'When did you last buy from Competitor Brand?', optionLabel: '2+ years ago' },
  ],
  // Spend
  'v-sp-150-225': [
    { studyName: "Premium SS26 — pricing test", questionTitle: 'On average, how much do you spend on a pair of shoes?', optionLabel: '$150–225' },
  ],
  'v-sp-225plus': [
    { studyName: "Premium SS26 — pricing test", questionTitle: 'On average, how much do you spend on a pair of shoes?', optionLabel: '$225+' },
  ],
  // Social
  'v-soc-daily': [
    { studyName: "Originals Q4 — naming", questionTitle: 'How often do you follow footwear accounts on social media?', optionLabel: 'Daily' },
  ],
  'v-soc-weekly': [
    { studyName: "Originals Q4 — naming", questionTitle: 'How often do you follow footwear accounts on social media?', optionLabel: 'Weekly' },
  ],
  // Income
  'v-inc-low': [
    { studyName: "Value-tier launch — concept test", questionTitle: 'What is your household income?', optionLabel: '<$50k' },
  ],
  'v-inc-mid1': [
    { studyName: "Value-tier launch — concept test", questionTitle: 'What is your household income?', optionLabel: '$50–75k' },
  ],
};

// Per-profile CA rules: profileId -> { caId: [valueId, ...] }
// Profile MATCHES when AND across CAs, OR within values.
const PROFILE_CA_RULES = {
  'p-trend': {
    'ca-age': ['v-age-18-24'],
    'ca-social': ['v-soc-daily', 'v-soc-weekly'],
    'ca-purchasefreq': ['v-pf-monthly', 'v-pf-quarterly'],
  },
  'p-core': {
    'ca-activity': ['v-act-run'],
    'ca-mileage': ['v-mi-11-20', 'v-mi-21-40', 'v-mi-40plus'],
  },
  'p-core2': {
    'ca-activity': ['v-act-gym', 'v-act-hike', 'v-act-pickleball', 'v-act-tennis'],
  },
  'p-value': {
    'ca-income': ['v-inc-low', 'v-inc-mid1'],
  },
  'p-lux': {
    'ca-spend': ['v-sp-150-225', 'v-sp-225plus'],
  },
  'p-lapsed': {
    'ca-lastadidas': ['v-la-old'],
  },
  'p-kids': {
    // No directly mapped CAs in this seed — exists as legacy question rules.
  },
  'p-hiker': {
    'ca-activity': ['v-act-trail', 'v-act-hike', 'v-act-climb'],
    'ca-climate': ['v-cl-wet', 'v-cl-cold'],
  },
};

const caById = (id) => CONSUMER_ATTRIBUTES.find(c => c.id === id);
const caValueById = (vid) => {
  for (const ca of CONSUMER_ATTRIBUTES) {
    const v = ca.values.find(vv => vv.id === vid);
    if (v) return { ca, value: v };
  }
  return null;
};

Object.assign(window, { CONSUMER_ATTRIBUTES, CA_VALUE_MAPPINGS, PROFILE_CA_RULES, caById, caValueById });
