/**
 * Property CSV Template Download
 * GET /api/real-estate/properties/template
 */

import { NextResponse } from "next/server";

export const dynamic = "force-dynamic";

export async function GET() {
  const headers = [
    "titleEn",
    "titleAr",
    "propertyType",
    "listingType",
    "city",
    "district",
    "price",
    "currency",
    "bedrooms",
    "bathrooms",
    "builtUpArea",
    "furnishing",
    "ownership",
  ];

  const examples = [
    [
      "Modern Apartment in West Bay",
      "شقة حديثة في الخليج الغربي",
      "APARTMENT",
      "RENT",
      "Doha",
      "West Bay",
      "8500",
      "QAR",
      "2",
      "2",
      "120",
      "FURNISHED",
      "FREEHOLD",
    ],
    [
      "Luxury Villa in Al Rayyan",
      "فيلا فاخرة في الريان",
      "VILLA",
      "SALE",
      "Doha",
      "Al Rayyan",
      "2500000",
      "QAR",
      "4",
      "3",
      "350",
      "UNFURNISHED",
      "FREEHOLD",
    ],
    [
      "Studio in The Pearl",
      "استوديو في اللؤلؤة",
      "STUDIO",
      "RENT",
      "Doha",
      "The Pearl",
      "5000",
      "QAR",
      "0",
      "1",
      "55",
      "FURNISHED",
      "LEASEHOLD",
    ],
  ];

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

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