// ─────────────────────────────────────────────────────────────────────
// VISUAL RECORD — inspection gallery data
// Photos are organized by EVENT (documentation session) and tagged by
// vehicle AREA. Every photo carries inspection metadata. This is a
// condition / provenance record, not a photo showcase.
// ─────────────────────────────────────────────────────────────────────

// Inspection event types (capture-session context).
const VR_EVENT_TYPES = [
  'Evaluation',
  'Pre-Purchase Inspection',
  'Delivery Condition',
  'Initial Acquisition',
  'Restoration',
  'Mechanical Work',
  'Paint Work',
  'Interior Restoration',
  'Service',
  'Auction Listing',
  'Sale Preparation',
  'Sold',
];

// Vehicle areas for rapid walk-around documentation.
const VR_AREAS = [
  { name: 'Exterior',            icon: 'car' },
  { name: 'Interior',            icon: 'archive' },
  { name: 'Engine Bay',          icon: 'spark' },
  { name: 'Undercarriage',       icon: 'wrench' },
  { name: 'Trunk',               icon: 'bookmark' },
  { name: 'Wheels & Tires',      icon: 'compass' },
  { name: 'VIN & Identification',icon: 'search' },
  { name: 'Documentation',       icon: 'certificate' },
  { name: 'Damage',              icon: 'flag' },
  { name: 'Restoration Work',    icon: 'hammer' },
];

// Condition statuses + their accent color.
const VR_CONDITIONS = ['Excellent', 'Very Good', 'Good', 'Fair', 'Needs Attention', 'Damage Noted'];
const vrConditionColor = (c) => {
  if (c === 'Excellent' || c === 'Very Good') return '#B89055';   // gold
  if (c === 'Good') return '#F4EFE6';                              // paper
  if (c === 'Fair') return '#8A8378';                             // mute
  return '#7A2418';                                                // signal — needs attention / damage
};

// Build a plausible visual record for a vehicle, tailored to its status.
// Each def: [type, date, time, photographer, note, areas[], condition, damageCount]
function seedVisualRecord(v) {
  const odo = v.mileage || 0;
  const sold = v.status === 'Sold' || v.status === 'Archived';
  const restoring = v.status === 'In Restoration';

  let defs;
  if (sold) {
    defs = [
      ['Sold', 'Mar 12, 2025', '11:20', 'You', 'Handover documentation — keys, tools and records transferred to new owner.', ['Exterior', 'Interior', 'VIN & Identification', 'Documentation'], 'Excellent', 0],
      ['Sale Preparation', 'Feb 02, 2025', '09:05', 'Studio · Canepa', 'Detail and studio photography set for the sale listing.', ['Exterior', 'Interior', 'Engine Bay', 'Wheels & Tires'], 'Excellent', 0],
      ['Auction Listing', 'Jan 20, 2025', '14:40', 'Broker', 'Catalogue photography for auction submission.', ['Exterior', 'Interior', 'Engine Bay', 'Undercarriage'], 'Very Good', 0],
      ['Pre-Purchase Inspection', 'Feb 07, 2021', '10:15', 'Marque Specialist', 'Full PPI prior to acquisition — paint depth, panel gaps, mechanicals.', ['Exterior', 'Interior', 'Engine Bay', 'Undercarriage', 'VIN & Identification', 'Damage'], 'Very Good', 2],
      ['Initial Acquisition', 'Feb 05, 2021', '16:30', 'You', 'Delivery condition on arrival.', ['Exterior', 'Interior', 'Documentation'], 'Very Good', 1],
    ];
  } else if (restoring) {
    defs = [
      ['Paint Work', 'Sep 18, 2024', '13:10', 'Rudi & Co.', 'Bare-metal stage — body prepped, panels straight, booth scheduled.', ['Exterior', 'Damage', 'Restoration Work'], 'Needs Attention', 3],
      ['Mechanical Work', 'Aug 02, 2024', '11:45', 'Metric Motors', 'Engine reunited with chassis · test run on stand.', ['Engine Bay', 'Undercarriage', 'Restoration Work'], 'Good', 0],
      ['Restoration', 'May 06, 2024', '09:30', 'Rudi & Co.', 'Media blast complete · full disassembly documented for reference.', ['Exterior', 'Interior', 'Undercarriage', 'Restoration Work', 'Damage'], 'Needs Attention', 4],
      ['Evaluation', 'Mar 02, 2024', '15:20', 'You', 'Intake condition assessment at the restoration shop.', ['Exterior', 'Interior', 'Engine Bay', 'VIN & Identification'], 'Fair', 5],
      ['Initial Acquisition', 'Jun 27, 2020', '10:00', 'You', 'Purchased at Bonhams Scottsdale — delivery condition.', ['Exterior', 'Interior', 'Documentation'], 'Good', 2],
    ];
  } else {
    defs = [
      ['Service', 'Oct 08, 2024', '08:50', 'Cain Customs', 'Oil & filter change — under-car inspection photos archived.', ['Engine Bay', 'Undercarriage', 'Wheels & Tires'], 'Excellent', 0],
      ['Evaluation', 'Aug 18, 2024', '07:40', 'You', 'Concours prep walk-around — full condition pass before judging.', ['Exterior', 'Interior', 'Engine Bay', 'Wheels & Tires'], 'Excellent', 0],
      ['Delivery Condition', 'May 14, 2024', '12:15', 'Reliable Carriers', 'Condition on arrival after enclosed transport.', ['Exterior', 'Wheels & Tires', 'Documentation'], 'Excellent', 0],
      ['Pre-Purchase Inspection', 'Feb 01, 2021', '10:15', 'Marque Specialist', 'PPI prior to auction — matching numbers verified.', ['Exterior', 'Interior', 'Engine Bay', 'Undercarriage', 'VIN & Identification', 'Damage'], 'Very Good', 1],
      ['Initial Acquisition', 'Feb 07, 2021', '16:30', 'You', 'Delivery condition on arrival from auction house.', ['Exterior', 'Interior', 'Documentation'], 'Very Good', 0],
    ];
  }

  // Use the hero image for the Exterior shot of the most recent event that has one.
  const heroEi = v.hero ? defs.findIndex(d => d[5].includes('Exterior')) : -1;

  const events = defs.map((d, ei) => {
    const [type, date, time, photographer, note, areas, condition, dmg] = d;
    const photos = areas.map((area, ai) => ({
      id: `${v.id}-e${ei}-p${ai}`,
      area,
      src: (area === 'Exterior' && ei === heroEi) ? v.hero : null,
      time: `${date} · ${time}`,
      condition: area === 'Damage' ? 'Damage Noted' : condition,
      damage: area === 'Damage'
        ? (type === 'Restoration' || type === 'Paint Work' ? 'Rust repair — lower rear quarter' : 'Stone chips · front bumper')
        : null,
      restoration: (type === 'Restoration' || type === 'Paint Work' || type === 'Mechanical Work' || type === 'Interior Restoration') ? type : null,
      photographer,
      odometer: odo,
      tags: [area, type],
      note,
      docs: area === 'Documentation' ? 2 : (area === 'VIN & Identification' ? 1 : 0),
    }));
    // A realistic, larger total — the walk-around captured more than we thumbnail.
    const count = areas.length + 6 + ((ei * 9 + areas.length * 5) % 28);
    return {
      id: `${v.id}-e${ei}`,
      type, date, time, photographer, note, condition,
      areas, photos, count, damageCount: dmg,
    };
  });

  return events;
}

// Aggregate area coverage across all events.
function vrAreaCoverage(events) {
  const map = {};
  VR_AREAS.forEach(a => { map[a.name] = { ...a, count: 0, photos: [] }; });
  events.forEach(ev => {
    const per = Math.max(1, Math.round(ev.count / ev.areas.length));
    ev.photos.forEach(p => {
      if (!map[p.area]) map[p.area] = { name: p.area, icon: 'gallery', count: 0, photos: [] };
      map[p.area].count += per;
      map[p.area].photos.push({ ...p, eventType: ev.type, eventDate: ev.date });
    });
  });
  return VR_AREAS.map(a => map[a.name]);
}

Object.assign(window, {
  VR_EVENT_TYPES, VR_AREAS, VR_CONDITIONS,
  vrConditionColor, seedVisualRecord, vrAreaCoverage,
});
