// Shared quote-flow logic — used by all 4 directions.

const { useState, useEffect, useRef } = React;

const MOCK_ADDRESSES = [
  { line: '14 Seafield Crescent, Booterstown, Co. Dublin', eir: 'A94 X2P3', sqm: 142, roof: 'south' },
  { line: '7 Beechwood Park, Ranelagh, Dublin 6', eir: 'D06 H7K9', sqm: 98, roof: 'south-west' },
  { line: '23 Lakelands Avenue, Stillorgan, Co. Dublin', eir: 'A94 V8R2', sqm: 165, roof: 'south' },
  { line: 'Mount Pleasant, Dunmore East, Co. Waterford', eir: 'X91 K4P8', sqm: 188, roof: 'south-east' },
  { line: '12 Ard na Greine, Salthill, Galway', eir: 'H91 R6E4', sqm: 124, roof: 'west' },
  { line: '45 Ros Mór, Douglas, Cork', eir: 'T12 W9Y1', sqm: 156, roof: 'south' },
  { line: 'The Old Forge, Adare, Co. Limerick', eir: 'V94 P3D6', sqm: 210, roof: 'south' },
  { line: '8 Cnoc na Sí, Tramore, Co. Waterford', eir: 'X91 H8N5', sqm: 132, roof: 'south-west' },
];

function searchAddresses(q) {
  if (!q || q.length < 2) return [];
  const lo = q.toLowerCase();
  return MOCK_ADDRESSES.filter(a =>
    a.line.toLowerCase().includes(lo) || a.eir.toLowerCase().includes(lo)
  ).slice(0, 5);
}

function estimateQuote({ sqm = 120, billEur = 180, roof = 'south' }) {
  const roofFactor = { 'south': 1.0, 'south-west': 0.92, 'south-east': 0.92, 'west': 0.78, 'east': 0.78 }[roof] || 0.85;
  const usableRoofSqm = Math.min(sqm * 0.45, 60);
  const panelCount = Math.max(6, Math.min(20, Math.floor(usableRoofSqm / 1.95)));
  const systemKwp = +(panelCount * 0.44).toFixed(1);
  const annualKwh = Math.round(systemKwp * 950 * roofFactor);
  const annualSavings = Math.round(annualKwh * 0.32 + Math.min(billEur * 12, 3500) * 0.08);
  // SEAI Solar PV grant (2024+): €700/kWp for the first 2 kWp, €200/kWp for
  // the next 2 kWp, capped at €1,800 for systems of 4 kWp or larger.
  const seaiGrant = Math.round(
    Math.min(systemKwp, 2) * 700 + Math.max(0, Math.min(systemKwp, 4) - 2) * 200
  );
  const grossCost = Math.round(panelCount * 620 + 3200);
  const netCost = grossCost - seaiGrant;
  const paybackYears = +(netCost / annualSavings).toFixed(1);
  const co2Tonnes = +(annualKwh * 0.000296).toFixed(2);
  return {
    panelCount, systemKwp, annualKwh, annualSavings,
    seaiGrant, grossCost, netCost, paybackYears, co2Tonnes,
    roofFactor: Math.round(roofFactor * 100),
  };
}

const eur = (n) => '€' + Math.round(n).toLocaleString('en-IE');
const kwh = (n) => Math.round(n).toLocaleString('en-IE') + ' kWh';

function useQuoteFlow(initial = {}) {
  const base = {
    address: '', eir: '', sqm: 120, billEur: 180, roof: 'south',
    propertyType: 'detached', email: '', phone: '', name: '',
    timeline: '3-6 months', ...initial,
  };
  const [step, setStep] = useState(0);
  const [data, setData] = useState(base);
  const update = (k, v) => setData(d => ({ ...d, [k]: v }));
  const updateMany = (obj) => setData(d => ({ ...d, ...obj }));
  const reset = () => { setStep(0); setData(base); };
  const quote = estimateQuote(data);
  return { step, setStep, data, update, updateMany, quote, reset };
}

window.MOCK_ADDRESSES = MOCK_ADDRESSES;
window.searchAddresses = searchAddresses;
window.estimateQuote = estimateQuote;
window.useQuoteFlow = useQuoteFlow;
window.eur = eur;
window.kwh = kwh;
