// data.jsx — VM0047 v1.1 supports two field-capture workflows.
// One agent role, two workflow types — chosen per project, not per agent:
//
//   1. CENSUS-BASED   atomic record = planting unit (one tree, one bamboo clump)
//                     for agroforestry, shelterbelts, urban planting, dispersed mosaics.
//                     Density-capped at 50 units/ha per VM0047.
//
//   2. AREA-BASED     atomic record = a fixed-radius sampling plot inside a stratum.
//                     for forest restoration with land-cover change. Plot inventory
//                     + observations scale up via remote sensing.
//
// A VISIT is the unit of agent work. For census it is a SITE. For area it is a PLOT
// inside a stratum. Same shell, branched brief.

// ───────────────────── VISITS (today's queue) ─────────────────────
const VISITS = [
  // ── CENSUS-BASED ──
  {
    id: 'STE-0427',
    workflow: 'census',
    participant: 'Wanjiru, M.',
    village: 'Bunyala Central · Mau-Mau village',
    sublocality: 'Bunyala, Busia',
    type: 'New enrollment',
    standKind: 'Tree',
    status: 'critical',
    missing: 4,
    due: 'Today',
    distance: '0.3 km',
    project: 'Bunyala Riparian · GS V1.2',
    plotLabel: 'Site A · ~1.8 ha',
    siteAreaHa: 1.8,
    targetUnits: 78,
    capturedUnits: 0,
  },
  {
    id: 'STE-0431',
    workflow: 'census',
    participant: 'Okello, S.',
    village: 'Bunyala North · Rukala',
    sublocality: 'Bunyala, Busia',
    type: 'Monitoring · Y2',
    standKind: 'Bamboo',
    status: 'partial',
    missing: 2,
    due: 'Today',
    distance: '0.9 km',
    project: 'Busia Bamboo · Verra VM0047',
    plotLabel: 'Site B · 0.6 ha',
    siteAreaHa: 0.6,
    targetUnits: 28,
    capturedUnits: 18,
  },

  // ── AREA-BASED ──
  // Sambia Hills Forest Restoration — a 412 ha VM0047 v1.1 ARR project with 3 strata
  // and 28 permanent sampling plots. Mary services 3 of them today.
  {
    id: 'PLT-A-04',
    workflow: 'area',
    participant: 'Sambia Hills · Stratum A',
    village: 'Sambia Hills · north slope',
    sublocality: 'Sambia, Busia',
    type: 'Monitoring · annual',
    standKind: 'Forest',
    status: 'partial',
    missing: 3,
    due: 'Today',
    distance: '1.2 km',
    project: 'Sambia Hills ARR · Verra VM0047',
    plotLabel: 'Plot A-04 · 14 m radius',
    stratum: 'A',
    stratumName: 'Closed-canopy regrowth',
    stratumAreaHa: 164,
    plotNumber: 4,
    plotShape: 'circular',
    plotRadiusM: 14,
    plotAreaM2: 615,
  },
  {
    id: 'PLT-B-11',
    workflow: 'area',
    participant: 'Sambia Hills · Stratum B',
    village: 'Sambia Hills · south slope',
    sublocality: 'Sambia, Busia',
    type: 'Monitoring · annual',
    standKind: 'Forest',
    status: 'critical',
    missing: 5,
    due: 'Today',
    distance: '1.8 km',
    project: 'Sambia Hills ARR · Verra VM0047',
    plotLabel: 'Plot B-11 · 14 m radius',
    stratum: 'B',
    stratumName: 'Open-canopy regenerating',
    stratumAreaHa: 187,
    plotNumber: 11,
    plotShape: 'circular',
    plotRadiusM: 14,
    plotAreaM2: 615,
  },
  {
    id: 'PLT-B-12',
    workflow: 'area',
    participant: 'Sambia Hills · Stratum B',
    village: 'Sambia Hills · south slope',
    sublocality: 'Sambia, Busia',
    type: 'Monitoring · annual',
    standKind: 'Forest',
    status: 'ready',
    missing: 0,
    due: 'Today',
    distance: '2.0 km',
    project: 'Sambia Hills ARR · Verra VM0047',
    plotLabel: 'Plot B-12 · 14 m radius',
    stratum: 'B',
    stratumName: 'Open-canopy regenerating',
    stratumAreaHa: 187,
    plotNumber: 12,
    plotShape: 'circular',
    plotRadiusM: 14,
    plotAreaM2: 615,
  },
  {
    id: 'PLT-C-03',
    workflow: 'area',
    participant: 'Sambia Hills · Stratum C',
    village: 'Sambia Hills · ridge',
    sublocality: 'Sambia, Busia',
    type: 'Callback',
    standKind: 'Forest',
    status: 'callback',
    missing: 1,
    due: 'Callback',
    distance: '2.8 km',
    project: 'Sambia Hills ARR · Verra VM0047',
    plotLabel: 'Plot C-03 · 14 m radius',
    stratum: 'C',
    stratumName: 'Shrub & grass · early regen',
    stratumAreaHa: 61,
    plotNumber: 3,
    plotShape: 'circular',
    plotRadiusM: 14,
    plotAreaM2: 615,
  },

  // ── More CENSUS-BASED ──
  {
    id: 'STE-0438',
    workflow: 'census',
    participant: 'Wekesa, P.',
    village: 'Budalangi · Rukala B',
    sublocality: 'Budalangi, Busia',
    type: 'New enrollment',
    standKind: 'Mixed',
    status: 'ready',
    missing: 0,
    due: 'Today',
    distance: '3.4 km',
    project: 'Budalangi Agroforestry · GS V1.2',
    plotLabel: 'Site C · 2.4 ha',
    siteAreaHa: 2.4,
    targetUnits: 96,
    capturedUnits: 96,
  },
];

// ───────────────────── CENSUS sample data (planting units) ─────────────────────
const VM0047_MAX_UNITS_PER_HA = 50;

const SAMPLE_UNITS = {
  'STE-0431': [
    { id:'PU-00018', x:120, y:60,  species:'sp_bbalcooa', year:2024, photo:true,  flagged:false },
    { id:'PU-00017', x:142, y:78,  species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00016', x:165, y:62,  species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00015', x:184, y:85,  species:'sp_bvulgaris', year:2024, photo:false, flagged:true },
    { id:'PU-00014', x:208, y:70,  species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00013', x:226, y:92,  species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00012', x:248, y:75,  species:'sp_bbalcooa', year:2024, photo:true,  flagged:false },
    { id:'PU-00011', x:266, y:98,  species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00010', x:120, y:118, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00009', x:142, y:132, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00008', x:165, y:118, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00007', x:184, y:140, species:'sp_bbalcooa', year:2024, photo:true,  flagged:false },
    { id:'PU-00006', x:208, y:125, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00005', x:226, y:148, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00004', x:248, y:130, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00003', x:266, y:152, species:'sp_bvulgaris',year:2024, photo:false, flagged:false },
    { id:'PU-00002', x:286, y:138, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
    { id:'PU-00001', x:306, y:162, species:'sp_bbalcooa', year:2024, photo:false, flagged:false },
  ],
  'STE-0427': [],
  'STE-0438': [],
};

// ───────────────────── AREA sample data (stems within plots) ─────────────────────
// Stems are positioned in plot-local polar coords (theta deg, r metres from plot center).
// Render code converts to SVG x,y for the plot canvas.

const SAMPLE_STEMS = {
  'PLT-A-04': [
    { id:'ST-01', species:'sp_markhamia',  r: 3.2, theta: 22,  dbh:14.2, height:8.5,  status:'live' },
    { id:'ST-02', species:'sp_markhamia',  r: 5.1, theta: 75,  dbh:11.8, height:7.1,  status:'live' },
    { id:'ST-03', species:'sp_grevillea',  r: 7.4, theta: 110, dbh:9.6,  height:6.3,  status:'live' },
    { id:'ST-04', species:'sp_croton',     r: 8.8, theta: 158, dbh:18.4, height:11.2, status:'live' },
    { id:'ST-05', species:'sp_markhamia',  r: 4.6, theta: 200, dbh:12.1, height:7.5,  status:'live' },
    { id:'ST-06', species:'sp_acaciapoly', r: 10.2,theta: 245, dbh:22.0, height:13.8, status:'live' },
    { id:'ST-07', species:'sp_grevillea',  r: 11.4,theta: 290, dbh:6.4,  height:4.8,  status:'live' },
    { id:'ST-08', species:'sp_markhamia',  r: 6.8, theta: 330, dbh:10.5, height:6.7,  status:'dead' },
    { id:'ST-09', species:'sp_grevillea',  r: 2.4, theta: 305, dbh:7.2,  height:5.1,  status:'live' },
    { id:'ST-10', species:'sp_markhamia',  r: 12.6,theta: 50,  dbh:8.9,  height:5.9,  status:'live' },
    { id:'ST-11', species:'sp_croton',     r: 9.5, theta: 95,  dbh:15.7, height:9.8,  status:'live' },
  ],
  'PLT-B-11': [
    { id:'ST-01', species:'sp_grevillea', r: 4.1, theta: 30,  dbh:8.4,  height:5.2,  status:'live' },
    { id:'ST-02', species:'sp_grevillea', r: 6.8, theta: 92,  dbh:6.1,  height:4.4,  status:'live' },
    { id:'ST-03', species:'sp_calliandra',r: 8.2, theta: 165, dbh:5.0,  height:3.8,  status:'live' },
    { id:'ST-04', species:'sp_grevillea', r: 10.6,theta: 248, dbh:7.6,  height:4.9,  status:'live' },
    { id:'ST-05', species:'sp_grevillea', r: 11.8,theta: 312, dbh:6.5,  height:4.2,  status:'live' },
  ],
  'PLT-B-12': [
    { id:'ST-01', species:'sp_grevillea',  r: 3.5, theta: 18,  dbh:9.4,  height:6.0,  status:'live' },
    { id:'ST-02', species:'sp_grevillea',  r: 5.9, theta: 64,  dbh:11.2, height:7.1,  status:'live' },
    { id:'ST-03', species:'sp_markhamia',  r: 7.1, theta: 122, dbh:8.6,  height:5.8,  status:'live' },
    { id:'ST-04', species:'sp_grevillea',  r: 9.8, theta: 178, dbh:13.0, height:8.2,  status:'live' },
    { id:'ST-05', species:'sp_croton',     r: 11.4,theta: 235, dbh:15.5, height:9.6,  status:'live' },
    { id:'ST-06', species:'sp_grevillea',  r: 6.2, theta: 295, dbh:10.4, height:6.8,  status:'live' },
    { id:'ST-07', species:'sp_grevillea',  r: 8.5, theta: 340, dbh:7.9,  height:5.4,  status:'live' },
  ],
  'PLT-C-03': [
    { id:'ST-01', species:'sp_calliandra', r: 6.2, theta: 80,  dbh:4.1,  height:3.0,  status:'live' },
    { id:'ST-02', species:'sp_acaciapoly', r: 9.4, theta: 220, dbh:5.8,  height:3.8,  status:'live' },
  ],
};

// ───────────────────── SPECIES library ─────────────────────
const SPECIES = [
  // Trees
  { id: 'sp_grevillea',  sci: 'Grevillea robusta',     common: 'Silky oak',        kind: 'Tree',   shortlist: true,  native: false },
  { id: 'sp_markhamia',  sci: 'Markhamia lutea',       common: 'Siala / Lusiola',  kind: 'Tree',   shortlist: true,  native: true },
  { id: 'sp_acaciapoly', sci: 'Acacia polyacantha',    common: 'White thorn',      kind: 'Tree',   shortlist: true,  native: true },
  { id: 'sp_croton',     sci: 'Croton megalocarpus',   common: 'Mukinduri',        kind: 'Tree',   shortlist: true,  native: true },
  { id: 'sp_casuarina',  sci: 'Casuarina equisetifolia', common: 'She-oak',        kind: 'Tree',   shortlist: true,  native: false },
  { id: 'sp_mukau',      sci: 'Melia volkensii',       common: 'Mukau',            kind: 'Tree',   shortlist: false, native: true },
  { id: 'sp_eucsal',     sci: 'Eucalyptus saligna',    common: 'Blue gum',         kind: 'Tree',   shortlist: false, native: false },
  { id: 'sp_cypress',    sci: 'Cupressus lusitanica',  common: 'Mexican cypress',  kind: 'Tree',   shortlist: false, native: false },
  { id: 'sp_neem',       sci: 'Azadirachta indica',    common: 'Neem · Mwarobaini', kind: 'Tree',  shortlist: false, native: false },
  { id: 'sp_mangoind',   sci: 'Mangifera indica',      common: 'Mango',            kind: 'Tree',   shortlist: false, native: false },
  // Bamboo
  { id: 'sp_bbalcooa',   sci: 'Bambusa balcooa',       common: 'Balcooa bamboo',   kind: 'Bamboo', shortlist: true,  native: false },
  { id: 'sp_bvulgaris',  sci: 'Bambusa vulgaris',      common: 'Common bamboo',    kind: 'Bamboo', shortlist: true,  native: true },
  { id: 'sp_dgiganteus', sci: 'Dendrocalamus giganteus', common: 'Giant bamboo',   kind: 'Bamboo', shortlist: false, native: false },
  { id: 'sp_dasper',     sci: 'Dendrocalamus asper',   common: 'Sweet bamboo',     kind: 'Bamboo', shortlist: false, native: false },
  { id: 'sp_oabyss',     sci: 'Oxytenanthera abyssinica', common: 'Lowland bamboo',kind: 'Bamboo', shortlist: false, native: true },
  // Shrubs / agroforestry
  { id: 'sp_calliandra', sci: 'Calliandra calothyrsus', common: 'Calliandra',      kind: 'Shrub',  shortlist: true,  native: false },
  { id: 'sp_leucaena',   sci: 'Leucaena leucocephala', common: 'Leucaena',         kind: 'Shrub',  shortlist: false, native: false },
  { id: 'sp_gliricidia', sci: 'Gliricidia sepium',     common: 'Gliricidia',       kind: 'Shrub',  shortlist: false, native: false },
  { id: 'sp_sesbania',   sci: 'Sesbania sesban',       common: 'Egyptian pea',     kind: 'Shrub',  shortlist: false, native: true },
];

const speciesById = (id) => SPECIES.find(s => s.id === id) || SPECIES[0];

// Stand kinds (census workflow only)
const STAND_KINDS = [
  { id: 'Tree',   label: 'Trees',                hint: 'Single-stem trees — Grevillea, Markhamia, Acacia' },
  { id: 'Bamboo', label: 'Bamboo',               hint: 'Clump or running bamboo' },
  { id: 'Mixed',  label: 'Mixed (trees + crops)',hint: 'Trees grown together with crops' },
  { id: 'Shrub',  label: 'Shrubs',               hint: 'Smaller fodder or hedge plants' },
];

const PLANTATION_TYPES = [
  'Household planting',
  'Farm boundary planting',
  'Block planting (farm)',
  'Shelter belt',
  'Communal · Forest planting',
  'Riparian planting',
  'Nursery · Propagation site',
];

const AGE_BANDS = ['<1 year', '1 year', '2 years', '3 years', '4 years', '5 years', '6 years', '7–10 years', '11–20 years', '21–30 years', '30+ years', 'Unknown'];
const SOIL_TYPES = ['Loam', 'Clay', 'Sandy', 'Silt', 'Rocky', 'Mixed', 'Wetland-associated', 'Other'];
const TENURE_TYPES = ['Title deed', 'Customary right', 'Leasehold', 'Communal allocation', 'Cooperative · group title', 'Permit · concession', 'Other'];

const ADMIN_TREE = {
  'Kenya': {
    'Busia': {
      'Budalangi':       ['Bunyala Central', 'Bunyala North', 'Bunyala South', 'Bunyala West'],
      'Butula':          ['Marachi Central', 'Marachi East', 'Marachi North'],
      'Nambale':         ['Nambale Township', 'Bukhayo East', 'Bukhayo West'],
      'Sambia':          ['Sambia North', 'Sambia South', 'Sambia Ridge'],
    },
    'Kakamega': {
      'Mumias East':     ['Lubinu', 'Lusheya'],
      'Lurambi':         ['Butsotso Central', 'Butsotso East'],
    },
  },
};

// ───────────────────── COOKSTOVE WORKSPACE — households ─────────────────────
// Households are the atomic visit for cookstove projects. Same shape as the
// existing cookstove field agent: monthly monitoring against TPDDTEC / GS Metered
// methodologies, but the agent never sees methodology names.

const REFO_UNUSED_HOUSEHOLDS = [
  { id:'HH-0142', name:'Mukamana, A.',   village:'Bunyala · Kabare cell',  status:'critical', missing:2, due:'Today',     stove:'M5 Metered', distance:'0.4 km' },
  { id:'HH-0157', name:'Uwimana, J.',    village:'Bunyala · Kabare cell',  status:'partial',  missing:1, due:'Today',     stove:'M5 Metered', distance:'0.7 km' },
  { id:'HH-0164', name:'Habimana, P.',   village:'Bunyala · Karenge',      status:'ready',    missing:0, due:'Today',     stove:'M5 Metered', distance:'1.1 km' },
  { id:'HH-0178', name:'Nshuti, C.',     village:'Bunyala · Karenge',      status:'critical', missing:3, due:'Tomorrow',  stove:'TPDDTEC',    distance:'1.3 km' },
  { id:'HH-0191', name:'Murekatete, L.', village:'Bunyala · Nzige',        status:'partial',  missing:1, due:'Tomorrow',  stove:'M5 Metered', distance:'2.0 km' },
  { id:'HH-0204', name:'Kagabo, F.',     village:'Bunyala · Nzige',        status:'callback', missing:1, due:'Callback',  stove:'TPDDTEC',    distance:'2.4 km' },
];
const DISTURBANCE_TYPES = [
  { value:'none',     label:'None visible' },
  { value:'grazing',  label:'Grazing · cattle' },
  { value:'fire',     label:'Fire scar · recent' },
  { value:'cutting',  label:'Cutting · selective' },
  { value:'clearing', label:'Clearing · larger area' },
  { value:'pest',     label:'Pest or disease' },
  { value:'storm',    label:'Storm damage' },
  { value:'other',    label:'Other (note)' },
];

const COVER_CLASSES = [
  { value:'<10',   label:'< 10%',  hint:'Mostly bare ground' },
  { value:'10-40', label:'10–40%', hint:'Open canopy' },
  { value:'40-70', label:'40–70%', hint:'Partial canopy' },
  { value:'>70',   label:'> 70%',  hint:'Closed canopy' },
];

Object.assign(window, {
  VISITS, SAMPLE_UNITS, SAMPLE_STEMS, VM0047_MAX_UNITS_PER_HA,
  SPECIES, speciesById, STAND_KINDS, PLANTATION_TYPES, AGE_BANDS,
  SOIL_TYPES, TENURE_TYPES, ADMIN_TREE,
  DISTURBANCE_TYPES, COVER_CLASSES,
  REFO_UNUSED_HOUSEHOLDS,
});
