// ─────────────────────────────────────────────────────────────────────
// COLLECTION ROSTER — the scalability fixture for the assignment sheet.
//
// Collector Archives is built for serious collectors, dealers and
// collection managers whose collections run to dozens — or hundreds — of
// vehicles. The live prototype garage only holds three richly-modelled
// cars; this roster stands in for the rest of a large collection so the
// assignment action sheet can prove it scales: AI suggestion first,
// search when needed, never an endless manual scroll.
//
// Each entry is intentionally light (registry-level metadata only). The
// three real vehicles still carry the photography, owners and full
// records — the roster is everything else on the shelf.
// ─────────────────────────────────────────────────────────────────────

// id · year · make · model · nickname · vin · project (active build, or null) · status
const ROSTER_BASE = [
  // ── Porsche — the backbone of the collection ───────────────────────
  ['356A 1600 Speedster', 'Porsche', 1957, 'Silver Bullet', '83019', null, 'In Storage'],
  ['356B 1600 S Coupe', 'Porsche', 1961, 'Reutter', '121884', null, 'Active'],
  ['911 2.0 SWB Coupe', 'Porsche', 1965, 'Irish Green', '301077', 'Hot-rod 2.0 freshening', 'In Restoration'],
  ['911S 2.4', 'Porsche', 1972, 'Ölklappe', '9112301456', null, 'Active'],
  ['911 Carrera 3.0', 'Porsche', 1976, 'Grand Prix', '9116600789', null, 'Active'],
  ['930 Turbo 3.3', 'Porsche', 1978, 'Whale Tail', '9308800142', 'Turbo top-end refresh', 'In Restoration'],
  ['911 SC Targa', 'Porsche', 1983, 'Cabrio Sun', 'WP0EB0911DS', null, 'In Storage'],
  ['959 Komfort', 'Porsche', 1988, 'Supercar Zero', 'WP0ZZZ95ZJS', null, 'Active'],
  ['964 Carrera RS', 'Porsche', 1992, 'Lightweight', 'WP0ZZZ96ZNS', 'Lightweight corner-balance', 'In Restoration'],
  ['993 Carrera RS', 'Porsche', 1995, 'Last Aircooled', 'WP0ZZZ99ZTS', null, 'Active'],
  ['993 Turbo S', 'Porsche', 1997, 'Violetto', 'WP0AC2992VS', null, 'Active'],
  ['Carrera GT', 'Porsche', 2004, 'Carbon Monster', 'WP0CA29844L', null, 'In Storage'],

  // ── Ferrari — the marquee row ──────────────────────────────────────
  ['166 MM Barchetta', 'Ferrari', 1950, 'Touring', '0064M', 'Body & brightwork', 'In Restoration'],
  ['250 GT SWB Berlinetta', 'Ferrari', 1961, 'Comp/61', '2735GT', null, 'Active'],
  ['250 GT Lusso', 'Ferrari', 1963, 'Pininfarina', '4807GT', null, 'Active'],
  ['275 GTB Long Nose', 'Ferrari', 1966, 'Torino', '08213', 'Six-carb conversion', 'In Restoration'],
  ['365 GTB/4 Daytona', 'Ferrari', 1971, 'Plexi', '14219', null, 'Active'],
  ['Dino 246 GT', 'Ferrari', 1972, 'Little Ferrari', '03658', null, 'In Storage'],
  ['365 GT4 BB', 'Ferrari', 1974, 'Berlinetta Boxer', '17965', null, 'Active'],
  ['308 GTB Vetroresina', 'Ferrari', 1976, 'Fibreglass', '19771', 'Fibreglass body sort', 'In Restoration'],
  ['288 GTO', 'Ferrari', 1985, 'Group B', '57801', null, 'In Storage'],
  ['F40', 'Ferrari', 1990, 'Rosso Fixx', '84319', null, 'Active'],
  ['F50', 'Ferrari', 1996, 'Open Top', '104589', null, 'Active'],
  ['550 Maranello', 'Ferrari', 1999, 'GT Express', '116241', null, 'Active'],

  // ── Mercedes-Benz ──────────────────────────────────────────────────
  ['540K Special Roadster', 'Mercedes-Benz', 1937, 'Sindelfingen', '154140', 'Chassis & coachwork', 'In Restoration'],
  ['300SL Roadster', 'Mercedes-Benz', 1959, 'Disc Brake', '1980427500', null, 'Active'],
  ['280SE 3.5 Cabriolet', 'Mercedes-Benz', 1971, 'W111', '11102712', null, 'In Storage'],
  ['600 Pullman', 'Mercedes-Benz', 1972, 'Grosser', '10001512', 'Hydraulics overhaul', 'In Restoration'],
  ['450SEL 6.9', 'Mercedes-Benz', 1977, 'Q-Ship', '11603612', null, 'Active'],
  ['280SL Pagoda', 'Mercedes-Benz', 1969, 'Pagode', '11304412', null, 'Active'],
  ['190E 2.5-16 Evo II', 'Mercedes-Benz', 1990, 'Cosworth', 'WDB2010361', null, 'Active'],

  // ── Jaguar ─────────────────────────────────────────────────────────
  ['XK120 Roadster', 'Jaguar', 1950, 'Alloy Body', '660058', null, 'In Storage'],
  ['C-Type', 'Jaguar', 1953, 'Le Mans', 'XKC051', 'Engine & brakes', 'In Restoration'],
  ['D-Type', 'Jaguar', 1955, 'Long Nose', 'XKD509', null, 'Active'],
  ['E-Type Series 1 3.8', 'Jaguar', 1961, 'Flat Floor', '885021', 'Flat-floor refresh', 'In Restoration'],
  ['E-Type Series 1 4.2 FHC', 'Jaguar', 1966, 'Opalescent', '1E21045', null, 'Active'],
  ['XJ13', 'Jaguar', 1966, 'The Prototype', 'XJ13001', null, 'In Storage'],

  // ── Aston Martin ───────────────────────────────────────────────────
  ['DB4 GT Zagato', 'Aston Martin', 1961, 'Zagato', 'DB4GT0186', null, 'Active'],
  ['DB5', 'Aston Martin', 1964, 'Silver Birch', 'DB51486L', 'Brightwork & trim', 'In Restoration'],
  ['DB6 Vantage', 'Aston Martin', 1967, 'Volante', 'DB62701', null, 'Active'],
  ['V8 Vantage', 'Aston Martin', 1978, 'Oscar India', 'V8V11534', null, 'In Storage'],

  // ── Alfa Romeo · Lancia · Maserati ─────────────────────────────────
  ['8C 2900B', 'Alfa Romeo', 1938, 'Lungo', '412020', 'Full nut-and-bolt', 'In Restoration'],
  ['Giulietta Sprint Veloce', 'Alfa Romeo', 1957, 'Bertone', '149315', null, 'Active'],
  ['Giulia TZ', 'Alfa Romeo', 1964, 'Tubolare', '750055', null, 'In Storage'],
  ['33 Stradale', 'Alfa Romeo', 1968, 'Road Racer', '750331', null, 'Active'],
  ['Aurelia B24 Spider', 'Lancia', 1955, 'America', 'B241096', 'Mechanical sort', 'In Restoration'],
  ['Stratos HF Stradale', 'Lancia', 1974, 'Wedge', '829006', null, 'Active'],
  ['A6GCS Berlinetta', 'Maserati', 1954, 'Pininfarina', '2059', null, 'In Storage'],
  ['Ghibli SS', 'Maserati', 1970, 'Spyder', 'AM1154901', null, 'Active'],

  // ── British GT · roadsters ─────────────────────────────────────────
  ['Continental R-Type', 'Bentley', 1954, 'Fastback', 'BC26C', null, 'Active'],
  ['Silver Cloud III', 'Rolls-Royce', 1965, 'Drophead', 'LCSC81', null, 'In Storage'],
  ['Elan S1', 'Lotus', 1963, 'Featherweight', '263011', 'Chassis swap', 'In Restoration'],
  ['Esprit S1', 'Lotus', 1977, 'Submarine', '7705110', null, 'Active'],

  // ── American iron ──────────────────────────────────────────────────
  ['Cobra 289', 'Shelby', 1964, 'Slabside', 'CSX2287', null, 'Active'],
  ['Cobra 427', 'Shelby', 1966, 'Big Block', 'CSX3015', 'Side-pipe refit', 'In Restoration'],
  ['GT350', 'Shelby', 1965, 'Wimbledon', 'SFM5S471', null, 'Active'],
  ['GT40 Mk I', 'Ford', 1966, 'Gulf', 'GT40P1075', null, 'In Storage'],
  ['Corvette L88', 'Chevrolet', 1967, 'Big Tank', '194377S', 'Numbers-match rebuild', 'In Restoration'],
  ['Camaro Z/28', 'Chevrolet', 1969, 'Cross-Ram', '124379N', null, 'Active'],
  ['Charger R/T 426 Hemi', 'Dodge', 1969, 'General', 'XS29J9B', null, 'In Storage'],

  // ── Modern blue-chip ───────────────────────────────────────────────
  ['M1', 'BMW', 1980, 'Procar', 'WBS5901', null, 'Active'],
  ['3.0 CSL Batmobile', 'BMW', 1973, 'Batmobile', '2275981', 'Aero & paint', 'In Restoration'],
  ['Countach LP400', 'Lamborghini', 1975, 'Periscopio', '1120148', null, 'Active'],
  ['Miura SV', 'Lamborghini', 1971, 'Bull', '4912', 'Driveline freshening', 'In Restoration'],
  ['EB110 SS', 'Bugatti', 1994, 'Blu Bugatti', 'ZA9AB01S', null, 'In Storage'],
  ['2000GT', 'Toyota', 1967, 'Bond', 'MF10100078', null, 'Active'],
  ['240Z', 'Datsun', 1971, 'Fairlady', 'HLS3007841', 'Engine bay detail', 'In Restoration'],
];

// Expand a handful of long-production marques with believable year siblings
// so a large collection (and search) feels real, not padded.
const ROSTER_SIBLINGS = [
  ['Porsche', '911T 2.2 Coupe', 1971, 'Tangerine', 'In Storage'],
  ['Porsche', '911 Carrera 2.7 MFI', 1974, 'Euro Spec', 'Active'],
  ['Porsche', '911 Carrera 3.2', 1987, 'Guards Red', 'Active'],
  ['Porsche', '928 S4', 1988, 'Grand Tourer', 'In Storage'],
  ['Porsche', '968 Club Sport', 1993, 'Speed Yellow', 'Active'],
  ['Ferrari', '328 GTS', 1988, 'Targa', 'In Storage'],
  ['Ferrari', '348 tb', 1991, 'Berlinetta', 'Active'],
  ['Ferrari', '355 Spider', 1996, 'Open Air', 'Active'],
  ['Mercedes-Benz', '230SL Pagoda', 1965, 'California', 'Active'],
  ['Mercedes-Benz', '500E', 1992, 'Porsche-Built', 'In Storage'],
  ['Jaguar', 'XKSS', 1957, 'Steve', 'Active'],
  ['Jaguar', 'Mk2 3.8', 1962, 'Sports Saloon', 'In Storage'],
  ['Aston Martin', 'DB4', 1960, 'Series II', 'Active'],
  ['Alfa Romeo', 'Montreal', 1972, 'Show Car', 'In Storage'],
  ['Maserati', 'Bora', 1973, 'Italdesign', 'Active'],
  ['Lamborghini', 'Espada', 1970, 'Four-Seat', 'In Storage'],
  ['BMW', '2002 Turbo', 1974, 'Schrift', 'Active'],
  ['Chevrolet', 'Corvette Sting Ray', 1963, 'Split Window', 'Active'],
  ['Lotus', 'Seven S2', 1962, 'Clubman', 'In Storage'],
  ['Porsche', '911 RSR 2.8', 1973, 'Carrera Cup', 'Active'],
];

const MARQUE_CODE = {
  'Porsche': 'P', 'Ferrari': 'F', 'Mercedes-Benz': 'M', 'Jaguar': 'J',
  'Aston Martin': 'A', 'Alfa Romeo': 'AR', 'Lancia': 'L', 'Maserati': 'MA',
  'Bentley': 'B', 'Rolls-Royce': 'RR', 'Lotus': 'LO', 'Shelby': 'S',
  'Ford': 'FD', 'Chevrolet': 'C', 'Dodge': 'D', 'BMW': 'BM',
  'Lamborghini': 'LB', 'Bugatti': 'BG', 'Toyota': 'TY', 'Datsun': 'DA',
};

const slug = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');

function buildRoster() {
  const out = [];
  let n = 4000;
  ROSTER_BASE.forEach(([model, make, year, nickname, vinTail, project, status]) => {
    const code = MARQUE_CODE[make] || 'X';
    out.push({
      id: 'r-' + slug(make) + '-' + slug(model) + '-' + year,
      year, make, model, nickname,
      vin: code + (vinTail || (++n)),
      project: project || null,
      status,
    });
  });
  ROSTER_SIBLINGS.forEach(([make, model, year, nickname, status]) => {
    const code = MARQUE_CODE[make] || 'X';
    out.push({
      id: 'r-' + slug(make) + '-' + slug(model) + '-' + year,
      year, make, model, nickname,
      vin: code + (++n) + 'X',
      project: null,
      status,
    });
  });
  return out;
}

const COLLECTION_ROSTER = buildRoster();

// Vehicles the principal has assigned costs to most recently — surfaced
// above the long tail so common assignments stay one tap away. Mixes the
// real richly-modelled cars with roster entries.
const COLLECTION_RECENT_IDS = [
  'mercedes-300sl',
  'old-yeller',
  'r-ferrari-365-gtb-4-daytona-1971',
  'r-porsche-993-carrera-rs-1995',
];

// Merge the real (fully-modelled) vehicles with the roster, real first,
// de-duped by id. This is the searchable pool the assignment sheet draws
// from. Roster cars resolve here too, so allocations to them never break.
function vehiclePool(realVehicles) {
  const seen = new Set();
  const pool = [];
  (realVehicles || []).forEach(v => { if (v && !seen.has(v.id)) { seen.add(v.id); pool.push(v); } });
  COLLECTION_ROSTER.forEach(v => { if (!seen.has(v.id)) { seen.add(v.id); pool.push(v); } });
  return pool;
}

// Case-insensitive search across make/model/year/nickname/VIN/project.
function searchVehicles(pool, q) {
  const t = (q || '').trim().toLowerCase();
  if (!t) return [];
  return pool.filter(v => {
    const hay = `${v.year} ${v.make} ${v.model} ${v.nickname || ''} ${v.vin || ''} ${v.project || ''}`.toLowerCase();
    return hay.includes(t);
  });
}

Object.assign(window, {
  COLLECTION_ROSTER, COLLECTION_RECENT_IDS, vehiclePool, searchVehicles,
});
