/**
 * Vehicle CSV Template Download
 *
 * GET /api/dashboard/vehicles/template
 *   Returns a CSV template with headers and example rows for bulk vehicle import.
 */

import { NextResponse } from "next/server";

export const dynamic = "force-dynamic";

export async function GET() {
  const headers = [
    "make",
    "model",
    "year",
    "type",
    "color",
    "licensePlate",
    "seats",
    "fuelType",
    "transmission",
    "pricePerDay",
    "pricePerWeek",
    "pricePerMonth",
    "depositAmount",
    "descriptionEn",
  ];

  const examples = [
    [
      "Toyota",
      "Camry",
      "2024",
      "CAR",
      "White",
      "QTR-1234",
      "5",
      "PETROL",
      "AUTOMATIC",
      "150",
      "900",
      "3000",
      "500",
      "Comfortable sedan with Apple CarPlay",
    ],
    [
      "Nissan",
      "Patrol",
      "2024",
      "SUV",
      "Silver",
      "QTR-5678",
      "7",
      "DIESEL",
      "AUTOMATIC",
      "350",
      "2100",
      "7000",
      "1000",
      "Full-size SUV perfect for desert trips",
    ],
    [
      "Toyota",
      "Hiace",
      "2023",
      "VAN",
      "White",
      "QTR-9012",
      "12",
      "DIESEL",
      "MANUAL",
      "200",
      "1200",
      "4000",
      "700",
      "Spacious passenger van",
    ],
    [
      "Mercedes",
      "S-Class",
      "2024",
      "LUXURY",
      "Black",
      "QTR-3456",
      "5",
      "PETROL",
      "AUTOMATIC",
      "800",
      "5000",
      "18000",
      "2000",
      "Premium luxury sedan with chauffeur option",
    ],
    [
      "Tesla",
      "Model 3",
      "2024",
      "ELECTRIC",
      "Blue",
      "QTR-7890",
      "5",
      "ELECTRIC",
      "AUTOMATIC",
      "250",
      "1500",
      "5000",
      "800",
      "Long-range electric sedan",
    ],
  ];

  const csvRows = [
    headers.join(","),
    ...examples.map((row) =>
      row.map((cell) => (cell.includes(",") ? `"${cell}"` : cell)).join(","),
    ),
  ];

  const csv = csvRows.join("\n");

  return new NextResponse(csv, {
    headers: {
      "Content-Type": "text/csv",
      "Content-Disposition": "attachment; filename=vehicle-import-template.csv",
    },
  });
}
