// Stylized SVG car silhouettes — placeholders for real photography.
const CarArt = {
  hatch: ({ accent = '#0B3D2E' }) => (
    <svg viewBox="0 0 360 160" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id="hatchBody" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor={accent} stopOpacity="0.95"/>
          <stop offset="1" stopColor={accent} stopOpacity="0.75"/>
        </linearGradient>
      </defs>
      <path d="M30 110 Q40 85 80 80 L120 55 Q145 40 195 40 L240 42 Q275 50 305 80 L335 95 Q345 100 340 115 L320 120 Q316 105 300 105 Q284 105 280 120 L130 120 Q126 105 110 105 Q94 105 90 120 L40 120 Q26 118 30 110 Z"
            fill="url(#hatchBody)"/>
      <path d="M120 60 Q145 47 190 47 L235 50 Q255 55 268 75 L150 75 Z" fill="#fff" opacity="0.18"/>
      <circle cx="105" cy="120" r="16" fill="#1a1a1a"/><circle cx="105" cy="120" r="6" fill="#888"/>
      <circle cx="295" cy="120" r="16" fill="#1a1a1a"/><circle cx="295" cy="120" r="6" fill="#888"/>
      <ellipse cx="180" cy="138" rx="155" ry="6" fill="#000" opacity="0.08"/>
    </svg>
  ),
  sedan: ({ accent = '#0B3D2E' }) => (
    <svg viewBox="0 0 360 160" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id="sedanBody" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor={accent} stopOpacity="0.95"/>
          <stop offset="1" stopColor={accent} stopOpacity="0.75"/>
        </linearGradient>
      </defs>
      <path d="M20 112 Q26 92 60 86 L110 56 Q140 44 200 44 L260 50 Q295 58 322 84 L348 96 Q356 102 350 116 L325 122 Q321 106 305 106 Q289 106 285 122 L130 122 Q126 106 110 106 Q94 106 90 122 L34 120 Q14 118 20 112 Z"
            fill="url(#sedanBody)"/>
      <path d="M118 60 Q142 50 198 50 L255 56 Q278 62 295 80 L150 80 Z" fill="#fff" opacity="0.18"/>
      <circle cx="105" cy="122" r="17" fill="#1a1a1a"/><circle cx="105" cy="122" r="6" fill="#888"/>
      <circle cx="300" cy="122" r="17" fill="#1a1a1a"/><circle cx="300" cy="122" r="6" fill="#888"/>
      <ellipse cx="180" cy="140" rx="160" ry="6" fill="#000" opacity="0.08"/>
    </svg>
  ),
  suv: ({ accent = '#0B3D2E' }) => (
    <svg viewBox="0 0 360 160" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id="suvBody" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor={accent} stopOpacity="0.95"/>
          <stop offset="1" stopColor={accent} stopOpacity="0.75"/>
        </linearGradient>
      </defs>
      <path d="M22 110 Q30 80 70 76 L100 44 Q125 32 200 32 L265 38 Q300 46 325 78 L350 92 Q358 100 350 116 L325 122 Q321 104 305 104 Q289 104 285 122 L130 122 Q126 104 110 104 Q94 104 90 122 L36 120 Q16 118 22 110 Z"
            fill="url(#suvBody)"/>
      <path d="M108 48 Q132 38 198 38 L260 44 Q282 50 298 72 L145 72 Z" fill="#fff" opacity="0.18"/>
      <circle cx="105" cy="122" r="19" fill="#1a1a1a"/><circle cx="105" cy="122" r="7" fill="#888"/>
      <circle cx="300" cy="122" r="19" fill="#1a1a1a"/><circle cx="300" cy="122" r="7" fill="#888"/>
      <ellipse cx="180" cy="142" rx="160" ry="6" fill="#000" opacity="0.08"/>
    </svg>
  ),
  bigsuv: ({ accent = '#0B3D2E' }) => (
    <svg viewBox="0 0 360 160" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id="bigBody" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor={accent} stopOpacity="0.95"/>
          <stop offset="1" stopColor={accent} stopOpacity="0.75"/>
        </linearGradient>
      </defs>
      <path d="M18 108 Q24 76 60 72 L82 38 Q102 28 200 28 L278 34 Q310 42 332 72 L352 88 Q360 98 352 116 L328 124 Q324 104 308 104 Q292 104 288 124 L128 124 Q124 104 108 104 Q92 104 88 124 L34 120 Q12 118 18 108 Z"
            fill="url(#bigBody)"/>
      <path d="M88 42 Q110 34 200 34 L272 40 Q294 46 308 68 L130 68 Z" fill="#fff" opacity="0.18"/>
      <circle cx="105" cy="124" r="22" fill="#1a1a1a"/><circle cx="105" cy="124" r="8" fill="#888"/>
      <circle cx="300" cy="124" r="22" fill="#1a1a1a"/><circle cx="300" cy="124" r="8" fill="#888"/>
      <ellipse cx="180" cy="146" rx="165" ry="6" fill="#000" opacity="0.08"/>
    </svg>
  ),
};

// Default fleet
const DEFAULT_CARS = [
  {
    id: 'clio', category: 'economy', name: 'Renault Clio', year: '2025',
    seats: 5, bags: 2, trans: 'Manual', fuel: 'Petrol', art: 'hatch', swatch: '#1F4D8A',
    image: '',
    desc: {
      en: 'Agile city hatchback — perfect for Marrakech medina runs and coastal day-trips.',
      fr: 'Citadine agile — idéale pour la médina de Marrakech et les excursions côtières.',
      ar: 'هاتشباك مدني رشيق — مثالي للتنقل في مدينة مراكش والرحلات الساحلية.',
      zh: '灵活的城市掀背车 — 穿梭马拉喀什老城和海岸日游的理想之选。',
    },
  },
  {
    id: 'logan', category: 'economy', name: 'Dacia Logan', year: '2025',
    seats: 5, bags: 3, trans: 'Manual', fuel: 'Diesel', art: 'sedan', swatch: '#A8AEB3',
    image: '',
    desc: {
      en: 'Spacious, economical sedan with impressive boot space. The travellers\' favourite.',
      fr: 'Berline spacieuse et économique avec un grand coffre. La préférée des voyageurs.',
      ar: 'سيارة سيدان فسيحة واقتصادية مع مساحة صندوق كبيرة. المفضلة للمسافرين.',
      zh: '宽敞经济的三厢车,行李箱空间充裕。旅行者的最爱。',
    },
  },
  {
    id: 'accent', category: 'economy', name: 'Hyundai Accent', year: '2025',
    seats: 5, bags: 3, trans: 'Auto', fuel: 'Petrol', art: 'sedan', swatch: '#D9D9D6',
    image: '',
    desc: {
      en: 'Smooth automatic transmission, modern tech and cold A/C for long Moroccan drives.',
      fr: 'Boîte automatique souple, tech moderne et climatisation froide pour les longs trajets.',
      ar: 'ناقل حركة أوتوماتيكي سلس، تقنية حديثة وتكييف بارد للرحلات الطويلة.',
      zh: '平顺的自动变速箱、现代科技和强劲空调,适合长途驾驶。',
    },
  },
  {
    id: 'rover', category: 'luxury', name: 'Range Rover Vogue', year: '2025',
    seats: 5, bags: 4, trans: 'Auto', fuel: 'Diesel', art: 'bigsuv', swatch: '#0B3D2E',
    image: '',
    desc: {
      en: 'Our flagship. British luxury meets Atlas mountain capability — arrive in style, anywhere.',
      fr: 'Notre produit phare. Luxe britannique et capacité des montagnes de l\'Atlas.',
      ar: 'سيارتنا الرائدة. فخامة بريطانية مع قدرة جبال الأطلس — اصل بأناقة أينما كنت.',
      zh: '我们的旗舰车型。英式豪华与阿特拉斯山越野能力的完美结合。',
    },
  },
  {
    id: 'touareg', category: 'luxury', name: 'Volkswagen Touareg', year: '2025',
    seats: 5, bags: 4, trans: 'Auto', fuel: 'Diesel', art: 'bigsuv', swatch: '#1a1a1a',
    image: '',
    desc: {
      en: 'Commanding full-size SUV with German build quality. Ideal for desert expeditions.',
      fr: 'SUV pleine grandeur à la finition allemande. Idéal pour les expéditions dans le désert.',
      ar: 'سيارة SUV بكامل المقاس بجودة بناء ألمانية. مثالية لرحلات الصحراء.',
      zh: '全尺寸SUV,德系品质。沙漠探险的理想之选。',
    },
  },
  {
    id: 'tiguan', category: 'luxury', name: 'Volkswagen Tiguan', year: '2025',
    seats: 5, bags: 3, trans: 'Auto', fuel: 'Petrol', art: 'suv', swatch: '#3F4A52',
    image: '',
    desc: {
      en: 'Refined mid-size SUV — the sweet spot between comfort, space and city agility.',
      fr: 'SUV intermédiaire raffiné — l\'équilibre parfait entre confort, espace et agilité urbaine.',
      ar: 'سيارة SUV متوسطة الحجم راقية — التوازن المثالي بين الراحة والمساحة.',
      zh: '精致的中型SUV — 舒适、空间与城市灵活性的完美平衡。',
    },
  },
];

const CARS_STORAGE_KEY = 'alz_cars_v1';
const MEDIA_STORAGE_KEY = 'alz_media_v1';

function loadCars() {
  try {
    const raw = localStorage.getItem(CARS_STORAGE_KEY);
    if (raw !== null) {
      const parsed = JSON.parse(raw);
      if (Array.isArray(parsed)) return parsed;
    }
  } catch {}
  return DEFAULT_CARS;
}

function saveCars(cars) {
  try { localStorage.setItem(CARS_STORAGE_KEY, JSON.stringify(cars)); } catch {}
  window.dispatchEvent(new CustomEvent('alz-cars-updated', { detail: cars }));
  if (window.ALZ_API) ALZ_API.saveCars(cars);
}

function resetCars() {
  try { localStorage.removeItem(CARS_STORAGE_KEY); } catch {}
  window.dispatchEvent(new CustomEvent('alz-cars-updated', { detail: DEFAULT_CARS }));
}

function useCars() {
  const [cars, setCars] = React.useState(loadCars);
  React.useEffect(() => {
    const update = () => setCars(loadCars());
    window.addEventListener('storage', update);
    window.addEventListener('alz-cars-updated', update);
    return () => {
      window.removeEventListener('storage', update);
      window.removeEventListener('alz-cars-updated', update);
    };
  }, []);
  return cars;
}

// Media management
function loadMedia() {
  try {
    const raw = localStorage.getItem(MEDIA_STORAGE_KEY);
    if (raw) return { hero: '', about: '', ...JSON.parse(raw) };
  } catch {}
  return { hero: '', about: '' };
}

function saveMedia(media) {
  try { localStorage.setItem(MEDIA_STORAGE_KEY, JSON.stringify(media)); } catch {}
  window.dispatchEvent(new CustomEvent('alz-media-updated', { detail: media }));
  if (window.ALZ_API) ALZ_API.saveMedia(media);
}

function useMedia() {
  const [media, setMedia] = React.useState(loadMedia);
  React.useEffect(() => {
    const update = () => setMedia(loadMedia());
    window.addEventListener('storage', update);
    window.addEventListener('alz-media-updated', update);
    return () => {
      window.removeEventListener('storage', update);
      window.removeEventListener('alz-media-updated', update);
    };
  }, []);
  return media;
}

window.CarArt = CarArt;
window.DEFAULT_CARS = DEFAULT_CARS;
window.loadCars = loadCars;
window.saveCars = saveCars;
window.resetCars = resetCars;
window.useCars = useCars;
window.CARS_STORAGE_KEY = CARS_STORAGE_KEY;
window.MEDIA_STORAGE_KEY = MEDIA_STORAGE_KEY;
window.loadMedia = loadMedia;
window.saveMedia = saveMedia;
window.useMedia = useMedia;
