/**
 * Tenant Issue CSV Template Download
 * GET /api/dashboard/tenant-issues/template
 */

import { NextResponse } from "next/server";

export const dynamic = "force-dynamic";

export async function GET() {
  const headers = [
    "propertyId",
    "title",
    "description",
    "category",
    "priority",
    "unit",
    "tenantName",
  ];

  const examples = [
    [
      "<property-id>",
      "Water leak in bathroom",
      "Persistent dripping from ceiling above shower",
      "plumbing",
      "high",
      "Apt 4B",
      "Ahmed Al-Mansoori",
    ],
    [
      "<property-id>",
      "AC not cooling",
      "Split unit in master bedroom blowing warm air",
      "hvac",
      "urgent",
      "Villa 12",
      "Sarah Johnson",
    ],
    [
      "<property-id>",
      "Broken light switch",
      "Living room main switch not responding",
      "electrical",
      "medium",
      "Unit 7A",
      "Khalid Ibrahim",
    ],
    [
      "<property-id>",
      "Kitchen sink clogged",
      "Water draining very slowly in kitchen",
      "plumbing",
      "medium",
      "Apt 2C",
      "Fatima Al-Thani",
    ],
    [
      "<property-id>",
      "Pest issue in storage room",
      "Cockroaches spotted near the storage area",
      "pest_control",
      "high",
      "Apt 9D",
      "Omar Hussain",
    ],
  ];

  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=tenant-issues-import-template.csv",
    },
  });
}
