/**
 * Appointment Types CSV Template Download
 *
 * GET /api/dashboard/appointment-types/template
 *   Returns a UTF-8 CSV file with the correct column headers and three
 *   contextual example rows.  The rows are tailored to the organisation's
 *   sector via the optional `?sector=` query parameter.
 *
 * Supported sector values:
 *   restaurants | schools | default (anything else)
 *
 * Response headers:
 *   Content-Type: text/csv; charset=utf-8
 *   Content-Disposition: attachment; filename="appointment-types-template.csv"
 *
 * CSV columns:
 *   nameEn, nameAr, duration, price, category, categoryAr, itemType
 */

import { NextRequest, NextResponse } from "next/server";

export const dynamic = "force-dynamic";

const CSV_HEADERS = "nameEn,nameAr,duration,price,category,categoryAr,itemType";

/** Row definitions per sector */
const SECTOR_EXAMPLES: Record<string, string[]> = {
  restaurants: [
    "Grilled Chicken,دجاج مشوي,0,45,Mains,الأطباق الرئيسية,menu_item",
    "Fresh Juice,عصير طازج,0,12,Beverages,المشروبات,menu_item",
    "Cheese Cake,كيك بالجبنة,0,22,Desserts,الحلويات,menu_item",
  ],
  schools: [
    "Tuition Fee,رسوم الدراسة,0,1500,Academic,أكاديمي,fee",
    "Activity Fee,رسوم الأنشطة,0,200,Extracurricular,أنشطة خارجية,fee",
    "Registration Fee,رسوم التسجيل,0,350,Administrative,إداري,fee",
  ],
  default: [
    "Consultation,استشارة,30,150,General,عام,service",
    "Follow-up Visit,زيارة متابعة,20,80,General,عام,service",
    "Full Assessment,تقييم شامل,60,300,Specialist,متخصص,service",
  ],
};

export async function GET(request: NextRequest) {
  const sector =
    request.nextUrl.searchParams.get("sector")?.toLowerCase() ?? "default";

  const rows = SECTOR_EXAMPLES[sector] ?? SECTOR_EXAMPLES["default"];

  const csv = [CSV_HEADERS, ...rows].join("\n");

  return new NextResponse(csv, {
    status: 200,
    headers: {
      "Content-Type": "text/csv; charset=utf-8",
      "Content-Disposition":
        'attachment; filename="appointment-types-template.csv"',
      "Cache-Control": "no-store",
    },
  });
}
