import { AppDataSource } from '../data-source';
import { EdrTruckFleetSeeder } from '../seed/edr-truck-fleet.seeder';

/**
 * Seeds the 62-truck EDR fleet used by first-mile / last-mile.
 *
 * The seeder is idempotent (`ON CONFLICT (plate_number) DO NOTHING`), so a
 * re-run will NOT overwrite a truck whose rate was tuned by hand.
 */
async function seedEdrTrucks() {
  await AppDataSource.initialize();

  try {
    await new EdrTruckFleetSeeder(AppDataSource).run();

    const summary = await AppDataSource.query(`
      SELECT
        COUNT(*)::int AS trucks,
        COUNT(*) FILTER (WHERE status = 'ACTIVE')::int AS active,
        COUNT(*) FILTER (WHERE availability = 'FREE')::int AS free,
        COUNT(*) FILTER (WHERE price_per_km > 0)::int AS priced,
        COUNT(*) FILTER (WHERE price_per_km IS NULL OR price_per_km <= 0)::int AS unpriced,
        MIN(price_per_km)::text AS min_rate,
        MAX(price_per_km)::text AS max_rate
      FROM freight.vehicles
      WHERE vehicle_type = 'TRUCK';
    `);

    console.table(summary);
    console.log('Seeded EDR truck fleet.');
  } finally {
    await AppDataSource.destroy();
  }
}

seedEdrTrucks().catch((error) => {
  console.error('Failed to seed EDR truck fleet:', error);
  process.exit(1);
});
