/**
 * Sector-Specific Transaction Configuration
 * Maps each industry to the appropriate transaction type and label.
 * The dashboard tab and forms adapt based on the organisation's industry.
 */

import { getSectorForIndustry } from "./industry-sector-map";

export type TransactionType =
  | "APPOINTMENT"
  | "ORDER"
  | "ENROLMENT"
  | "RENTAL"
  | "JOB"
  | "EVENT"
  | "RESERVATION"
  | "VIEWING";

export interface SectorTransactionConfig {
  transactionType: TransactionType;
  tabLabel: { en: string; ar: string };
  singular: { en: string; ar: string };
  createLabel: { en: string; ar: string };
}

/**
 * Maps industry string values (from onboarding/org.industry) to transaction config.
 * Keys match the INDUSTRIES constant in onboarding setup page.
 */
const SECTOR_TRANSACTION_MAP: Record<string, SectorTransactionConfig> = {
  // Food, Hospitality & Stays → Orders (for food) / Reservations (for stays)
  food_hospitality: {
    transactionType: "ORDER",
    tabLabel: { en: "Orders", ar: "الطلبات" },
    singular: { en: "Order", ar: "طلب" },
    createLabel: { en: "New Order", ar: "طلب جديد" },
  },

  // Education → Enrolments
  education: {
    transactionType: "ENROLMENT",
    tabLabel: { en: "Enrolments", ar: "التسجيلات" },
    singular: { en: "Enrolment", ar: "تسجيل" },
    createLabel: { en: "New Enrolment", ar: "تسجيل جديد" },
  },

  // Mobility & Automotive → Rentals
  mobility_automotive: {
    transactionType: "RENTAL",
    tabLabel: { en: "Rentals", ar: "الإيجارات" },
    singular: { en: "Rental", ar: "إيجار" },
    createLabel: { en: "New Rental", ar: "إيجار جديد" },
  },

  // Home Services & Trades → Jobs
  home_services_trades: {
    transactionType: "JOB",
    tabLabel: { en: "Jobs", ar: "المهام" },
    singular: { en: "Job", ar: "مهمة" },
    createLabel: { en: "New Job", ar: "مهمة جديدة" },
  },

  // Events & Venues → Events
  events_venues: {
    transactionType: "EVENT",
    tabLabel: { en: "Events", ar: "الفعاليات" },
    singular: { en: "Event", ar: "فعالية" },
    createLabel: { en: "New Event", ar: "فعالية جديدة" },
  },

  // Property & Facilities → Viewings
  property_facilities: {
    transactionType: "VIEWING",
    tabLabel: { en: "Viewings", ar: "المعاينات" },
    singular: { en: "Viewing", ar: "معاينة" },
    createLabel: { en: "New Viewing", ar: "معاينة جديدة" },
  },

  // All remaining sectors default to APPOINTMENT (handled by DEFAULT_CONFIG)
  // health_care, beauty_wellness, retail_services, pet_services,
  // nonprofit_community, public_sector, other
};

const DEFAULT_CONFIG: SectorTransactionConfig = {
  transactionType: "APPOINTMENT",
  tabLabel: { en: "Appointments", ar: "المواعيد" },
  singular: { en: "Appointment", ar: "موعد" },
  createLabel: { en: "New Appointment", ar: "موعد جديد" },
};

export function getSectorTransactionConfig(
  industryKey?: string | null,
): SectorTransactionConfig {
  if (!industryKey) return DEFAULT_CONFIG;

  // Direct match first (e.g. "food_hospitality")
  const direct = SECTOR_TRANSACTION_MAP[industryKey];
  if (direct) return direct;

  // Resolve sub-industry keys (e.g. "dental" → "health_care")
  const sectorKey = getSectorForIndustry(industryKey);
  return SECTOR_TRANSACTION_MAP[sectorKey] ?? DEFAULT_CONFIG;
}

export { SECTOR_TRANSACTION_MAP, DEFAULT_CONFIG };
