/**
 * Sector Custom Fields
 *
 * Flat-shape adapter over `sector-registry`'s `FieldDef[]`. Provides the
 * `SectorFieldDefinition` type and `getSectorCustomFields()` helper that
 * older consumers (the dashboard `SectorFieldsForm`, the api-schemas
 * validator, the `useSectorFields` hook) expect.
 *
 * The single source of truth lives in `sector-registry.ts`. This module
 * just unwraps `label: { en, ar }` into flat `labelEn` / `labelAr` and
 * adds optional bilingual placeholders that the form renders next to
 * each field.
 *
 * Why this shim exists: an earlier sector commit referenced this module
 * but it was never created. Rather than rewrite every consumer to use
 * the registry's nested label shape, the adapter keeps the public API
 * stable while letting the registry stay canonical.
 */

import { SECTOR_REGISTRY, type FieldDef } from "@/lib/config/sector-registry";
import type { SectorKey } from "@/lib/config/industry-sector-map";

/**
 * Bilingual option for a select field.
 */
export interface SectorFieldOption {
  value: string;
  labelEn: string;
  labelAr: string;
}

/**
 * Flat field definition shape used by `SectorFieldsForm.tsx`,
 * `useSectorFields.ts`, and `api-schemas.ts validateSectorMetadata()`.
 */
export interface SectorFieldDefinition {
  key: string;
  type: "text" | "textarea" | "date" | "time" | "number" | "select";
  labelEn: string;
  labelAr: string;
  placeholderEn?: string;
  placeholderAr?: string;
  required: boolean;
  options?: SectorFieldOption[];
}

/**
 * Field keys that should render as a textarea instead of a single-line
 * text input. The registry types these as `"text"` (the engine doesn't
 * care), but on the dashboard form they need a multi-line widget.
 */
const TEXTAREA_FIELDS = new Set<string>([
  "dietary_requirements",
  "issue_description",
  "service_address",
  "special_occasion",
  "treatment_area",
]);

/**
 * Bilingual placeholder hints per field key. Optional — falls back to
 * the field label when no placeholder is configured.
 */
const FIELD_PLACEHOLDERS: Record<string, { en: string; ar: string }> = {
  // Healthcare
  doctor_specialist: { en: "e.g. Dr. Smith, dermatologist", ar: "مثال: د. سميث، جلدية" },
  department: { en: "e.g. Cardiology", ar: "مثال: قسم القلب" },
  // Beauty
  stylist_therapist: { en: "Preferred stylist or therapist", ar: "المصفف أو المعالج المفضل" },
  treatment_area: { en: "e.g. face, back, hands", ar: "مثال: الوجه، الظهر، اليدين" },
  // Food
  party_size: { en: "Number of guests", ar: "عدد الضيوف" },
  dietary_requirements: { en: "Allergies, vegetarian, halal, etc.", ar: "حساسية، نباتي، حلال، الخ" },
  special_occasion: { en: "Birthday, anniversary, etc.", ar: "عيد ميلاد، ذكرى، الخ" },
  // Retail
  consultant: { en: "Preferred consultant", ar: "المستشار المفضل" },
  service_category: { en: "Category of service", ar: "فئة الخدمة" },
  // Property
  property_address: { en: "Address or area", ar: "العنوان أو المنطقة" },
  property_type: { en: "Villa, apartment, etc.", ar: "فيلا، شقة، الخ" },
  bedrooms: { en: "Number of bedrooms", ar: "عدد غرف النوم" },
  // Mobility
  vehicle_type: { en: "SUV, sedan, etc.", ar: "دفع رباعي، سيدان، الخ" },
  pickup_location: { en: "Where to pick up", ar: "موقع الاستلام" },
  return_date: { en: "When to return", ar: "تاريخ الإرجاع" },
  // Home services
  service_address: { en: "Service address", ar: "عنوان الخدمة" },
  issue_description: { en: "Describe the issue", ar: "اوصف المشكلة" },
  // Education
  course_subject: { en: "Course or subject", ar: "الدورة أو المادة" },
  level: { en: "Beginner, intermediate, advanced", ar: "مبتدئ، متوسط، متقدم" },
  // Events
  event_type: { en: "Wedding, corporate, etc.", ar: "زواج، شركات، الخ" },
  guest_count: { en: "Expected number of guests", ar: "عدد الضيوف المتوقع" },
  venue_preference: { en: "Indoor or outdoor", ar: "داخلي أو خارجي" },
  // Pet
  pet_type: { en: "Dog, cat, etc.", ar: "كلب، قطة، الخ" },
  pet_name: { en: "Pet's name", ar: "اسم الحيوان" },
  pet_weight: { en: "Weight in kg", ar: "الوزن بالكيلو" },
  // Nonprofit
  program_name: { en: "Program name", ar: "اسم البرنامج" },
  participant_count: { en: "Number of participants", ar: "عدد المشاركين" },
  // Public sector
  service_type: { en: "Type of service", ar: "نوع الخدمة" },
  reference_number: { en: "Existing reference number", ar: "رقم مرجعي موجود" },
};

/**
 * Adapt the registry's nested `FieldDef` shape to the flat
 * `SectorFieldDefinition` shape that consumers expect.
 *
 * Promotes a few well-known field keys to `textarea` so the dashboard
 * form gives the user a multi-line widget for free-form input.
 */
function adaptField(field: FieldDef): SectorFieldDefinition {
  const placeholder = FIELD_PLACEHOLDERS[field.key];
  const type: SectorFieldDefinition["type"] = TEXTAREA_FIELDS.has(field.key)
    ? "textarea"
    : field.type;
  return {
    key: field.key,
    type,
    labelEn: field.label.en,
    labelAr: field.label.ar,
    placeholderEn: placeholder?.en,
    placeholderAr: placeholder?.ar,
    required: field.required,
  };
}

/**
 * Get the custom fields configured for a sector.
 *
 * Falls back to retail_services if the sector key is unknown so callers
 * never get a hard error mid-flow.
 */
export function getSectorCustomFields(
  sectorKey: SectorKey | string | null | undefined,
): SectorFieldDefinition[] {
  if (!sectorKey) return [];
  const config =
    SECTOR_REGISTRY[sectorKey as SectorKey] ?? SECTOR_REGISTRY.retail_services;
  if (!config) return [];
  return config.extraFields.map(adaptField);
}
