export interface DashboardCard {
  title: string;
  value: number;
  percentage: number;
  isPositive: boolean;
  icon: string;
  color: string;
}

export interface ModuleData {
  name: string;
  cards: DashboardCard[];
  modules: string[];
}

export interface FeedbackData {
  _id: string;
  name: string;
}

export interface FeedbackChartData {
  "This Week": any[];
  "This Month": any[];
  "This Year": any[];
}

export interface PeriodMultiplier {
  "This Week": number;
  "This Month": number;
  "This Year": number;
}

const dashboardDataMap: Record<string, ModuleData> = {
  security: {
    name: 'Security',
    cards: [
      {
        title: 'Feedback',
        value: 45,
        percentage: 12.5,
        isPositive: true,
        icon: 'mdi-comment-multiple',
        color: '#1E88E5',
      },
      {
        title: 'Work Orders',
        value: 28,
        percentage: 8.3,
        isPositive: true,
        icon: 'mdi-clipboard-list',
        color: '#D32F2F',
      },
      {
        title: 'Vehicle Management',
        value: 12,
        percentage: 5.2,
        isPositive: true,
        icon: 'mdi-truck',
        color: '#FFA726',
      },
      {
        title: 'Building Management',
        value: 35,
        percentage: 15.4,
        isPositive: true,
        icon: 'mdi-building',
        color: '#43A047',
      },
      {
        title: 'Visitor Management',
        value: 22,
        percentage: 6.8,
        isPositive: true,
        icon: 'mdi-account-multiple',
        color: '#7E57C2',
      },
      {
        title: 'Incident Reports',
        value: 9,
        percentage: -3.5,
        isPositive: false,
        icon: 'mdi-alert-circle',
        color: '#EF5350',
      },
      {
        title: 'Guest Management',
        value: 18,
        percentage: 4.2,
        isPositive: true,
        icon: 'mdi-account-badge',
        color: '#AB47BC',
      },
    ],
    modules: [
      'Feedback',
      'Work Orders',
      'Vehicle Management',
      'Building Management',
      'Visitor Management',
      'Incident Reports',
      'Guest Management',
    ],
  },
  hygiene: {
    name: 'Hygiene',
    cards: [
      {
        title: 'Feedback',
        value: 52,
        percentage: 14.2,
        isPositive: true,
        icon: 'mdi-comment-multiple',
        color: '#1E88E5',
      },
      {
        title: 'Work Orders',
        value: 38,
        percentage: 11.3,
        isPositive: true,
        icon: 'mdi-clipboard-list',
        color: '#D32F2F',
      },
      {
        title: 'Schedule Task Ticket',
        value: 25,
        percentage: 7.8,
        isPositive: true,
        icon: 'mdi-calendar-check',
        color: '#FFA726',
      },
      {
        title: 'Attendance',
        value: 64,
        percentage: 18.9,
        isPositive: true,
        icon: 'mdi-clock-check',
        color: '#43A047',
      },
      {
        title: 'Equipment Management',
        value: 42,
        percentage: 12.5,
        isPositive: true,
        icon: 'mdi-warehouse',
        color: '#7E57C2',
      },
      {
        title: 'Checkout Items',
        value: 19,
        percentage: 5.1,
        isPositive: true,
        icon: 'mdi-cart-plus',
        color: '#29B6F6',
      },
      {
        title: 'Cleaning Schedule',
        value: 31,
        percentage: 9.4,
        isPositive: true,
        icon: 'mdi-broom',
        color: '#66BB6A',
      },
    ],
    modules: [
      'Feedback',
      'Work Orders',
      'Schedule Task Ticket',
      'Attendance',
      'Equipment Management',
      'Checkout Items',
      'Cleaning Schedule - Checklist, Area, Unit',
    ],
  },
  landscaping: {
    name: 'Landscaping',
    cards: [
      {
        title: 'Feedback',
        value: 33,
        percentage: 10.5,
        isPositive: true,
        icon: 'mdi-comment-multiple',
        color: '#1E88E5',
      },
      {
        title: 'Work Orders',
        value: 47,
        percentage: 13.2,
        isPositive: true,
        icon: 'mdi-clipboard-list',
        color: '#D32F2F',
      },
    ],
    modules: [
      'Feedback',
      'Work Orders',
    ],
  },
  mechanical_electrical: {
    name: 'Mechanical & Electrical',
    cards: [
      {
        title: 'Feedback',
        value: 38,
        percentage: 11.8,
        isPositive: true,
        icon: 'mdi-comment-multiple',
        color: '#1E88E5',
      },
      {
        title: 'Work Orders',
        value: 52,
        percentage: 15.7,
        isPositive: true,
        icon: 'mdi-clipboard-list',
        color: '#D32F2F',
      },
    ],
    modules: [
      'Feedback',
      'Work Orders',
    ],
  },
  pool: {
    name: 'Pool',
    cards: [
      {
        title: 'Feedback',
        value: 29,
        percentage: 9.3,
        isPositive: true,
        icon: 'mdi-comment-multiple',
        color: '#1E88E5',
      },
      {
        title: 'Work Orders',
        value: 41,
        percentage: 12.1,
        isPositive: true,
        icon: 'mdi-clipboard-list',
        color: '#D32F2F',
      },
    ],
    modules: [
      'Feedback',
      'Work Orders',
    ],
  },
  pest: {
    name: 'Pest',
    cards: [
      {
        title: 'Feedback',
        value: 26,
        percentage: 8.6,
        isPositive: true,
        icon: 'mdi-comment-multiple',
        color: '#1E88E5',
      },
      {
        title: 'Work Orders',
        value: 35,
        percentage: 10.2,
        isPositive: true,
        icon: 'mdi-clipboard-list',
        color: '#D32F2F',
      },
    ],
    modules: [
      'Feedback',
      'Work Orders',
    ],
  },
};

// Feedback items data for dropdowns
const feedbackItemsMap: Record<string, FeedbackData[]> = {
  security: [
    { _id: "sec-1", name: "Perimeter Security" },
    { _id: "sec-2", name: "Access Control" },
    { _id: "sec-3", name: "CCTV Monitoring" },
    { _id: "sec-4", name: "Guard Services" },
  ],
  hygiene: [
    { _id: "hyg-1", name: "Cleaning Services" },
    { _id: "hyg-2", name: "Disinfection" },
    { _id: "hyg-3", name: "Waste Management" },
    { _id: "hyg-4", name: "Sanitation" },
  ],
  landscaping: [
    { _id: "land-1", name: "Lawn Care" },
    { _id: "land-2", name: "Garden Maintenance" },
    { _id: "land-3", name: "Landscaping Design" },
    { _id: "land-4", name: "Tree Service" },
  ],
  mechanical_electrical: [
    { _id: "me-1", name: "Electrical Services" },
    { _id: "me-2", name: "HVAC Maintenance" },
    { _id: "me-3", name: "Equipment Repair" },
    { _id: "me-4", name: "System Inspection" },
  ],
  pool: [
    { _id: "pool-1", name: "Water Treatment" },
    { _id: "pool-2", name: "Equipment Maintenance" },
    { _id: "pool-3", name: "Cleaning Services" },
    { _id: "pool-4", name: "Safety Inspection" },
  ],
  pest: [
    { _id: "pest-1", name: "Pest Control" },
    { _id: "pest-2", name: "Fumigation" },
    { _id: "pest-3", name: "Prevention Services" },
    { _id: "pest-4", name: "Inspection" },
  ],
};

// Feedback chart data based on selected service
const feedbackChartDataMap: Record<string, FeedbackChartData> = {
  "sec-1": {
    "This Week": [
      { x: 100, y: 260, width: 60, height: 30, value: 5, label: "1", color: "#2196F3" },
      { x: 200, y: 194, width: 60, height: 96, value: 16, label: "2", color: "#2196F3" },
      { x: 300, y: 230, width: 60, height: 60, value: 10, label: "3", color: "#2196F3" },
      { x: 400, y: 248, width: 60, height: 42, value: 7, label: "4", color: "#2196F3" },
      { x: 500, y: 272, width: 60, height: 18, value: 3, label: "5", color: "#2196F3" },
      { x: 600, y: 260, width: 60, height: 30, value: 5, label: "6", color: "#2196F3" },
      { x: 700, y: 212, width: 60, height: 78, value: 13, label: "7", color: "#2196F3" },
      { x: 800, y: 200, width: 60, height: 90, value: 15, label: "8", color: "#2196F3" },
      { x: 900, y: 80, width: 60, height: 210, value: 70, label: "9", color: "#2196F3" },
    ],
    "This Month": [
      { x: 100, y: 254, width: 60, height: 36, value: 6, label: "1", color: "#2196F3" },
      { x: 200, y: 140, width: 60, height: 150, value: 25, label: "2", color: "#2196F3" },
      { x: 300, y: 194, width: 60, height: 96, value: 16, label: "3", color: "#2196F3" },
      { x: 400, y: 212, width: 60, height: 78, value: 13, label: "4", color: "#2196F3" },
      { x: 500, y: 230, width: 60, height: 60, value: 10, label: "5", color: "#2196F3" },
      { x: 600, y: 248, width: 60, height: 42, value: 7, label: "6", color: "#2196F3" },
      { x: 700, y: 158, width: 60, height: 132, value: 22, label: "7", color: "#2196F3" },
      { x: 800, y: 176, width: 60, height: 114, value: 19, label: "8", color: "#2196F3" },
      { x: 900, y: 50, width: 60, height: 240, value: 80, label: "9", color: "#2196F3" },
    ],
    "This Year": [
      { x: 100, y: 236, width: 60, height: 54, value: 9, label: "1", color: "#2196F3" },
      { x: 200, y: 104, width: 60, height: 186, value: 31, label: "2", color: "#2196F3" },
      { x: 300, y: 176, width: 60, height: 114, value: 19, label: "3", color: "#2196F3" },
      { x: 400, y: 194, width: 60, height: 96, value: 16, label: "4", color: "#2196F3" },
      { x: 500, y: 212, width: 60, height: 78, value: 13, label: "5", color: "#2196F3" },
      { x: 600, y: 230, width: 60, height: 60, value: 10, label: "6", color: "#2196F3" },
      { x: 700, y: 122, width: 60, height: 168, value: 28, label: "7", color: "#2196F3" },
      { x: 800, y: 158, width: 60, height: 132, value: 22, label: "8", color: "#2196F3" },
      { x: 900, y: 20, width: 60, height: 270, value: 90, label: "9", color: "#2196F3" },
    ],
  },
  "sec-2": {
    "This Week": [
      { x: 100, y: 272, width: 60, height: 18, value: 3, label: "1", color: "#4CAF50" },
      { x: 200, y: 176, width: 60, height: 114, value: 19, label: "2", color: "#4CAF50" },
      { x: 300, y: 230, width: 60, height: 60, value: 10, label: "3", color: "#4CAF50" },
      { x: 400, y: 248, width: 60, height: 42, value: 7, label: "4", color: "#4CAF50" },
      { x: 500, y: 260, width: 60, height: 30, value: 5, label: "5", color: "#4CAF50" },
      { x: 600, y: 272, width: 60, height: 18, value: 3, label: "6", color: "#4CAF50" },
      { x: 700, y: 194, width: 60, height: 96, value: 16, label: "7", color: "#4CAF50" },
      { x: 800, y: 188, width: 60, height: 102, value: 17, label: "8", color: "#4CAF50" },
      { x: 900, y: 32, width: 60, height: 258, value: 86, label: "9", color: "#4CAF50" },
    ],
    "This Month": [
      { x: 100, y: 254, width: 60, height: 36, value: 6, label: "1", color: "#4CAF50" },
      { x: 200, y: 140, width: 60, height: 150, value: 25, label: "2", color: "#4CAF50" },
      { x: 300, y: 194, width: 60, height: 96, value: 16, label: "3", color: "#4CAF50" },
      { x: 400, y: 212, width: 60, height: 78, value: 13, label: "4", color: "#4CAF50" },
      { x: 500, y: 230, width: 60, height: 60, value: 10, label: "5", color: "#4CAF50" },
      { x: 600, y: 248, width: 60, height: 42, value: 7, label: "6", color: "#4CAF50" },
      { x: 700, y: 158, width: 60, height: 132, value: 22, label: "7", color: "#4CAF50" },
      { x: 800, y: 176, width: 60, height: 114, value: 19, label: "8", color: "#4CAF50" },
      { x: 900, y: 50, width: 60, height: 240, value: 80, label: "9", color: "#4CAF50" },
    ],
    "This Year": [
      { x: 100, y: 236, width: 60, height: 54, value: 9, label: "1", color: "#4CAF50" },
      { x: 200, y: 104, width: 60, height: 186, value: 31, label: "2", color: "#4CAF50" },
      { x: 300, y: 176, width: 60, height: 114, value: 19, label: "3", color: "#4CAF50" },
      { x: 400, y: 194, width: 60, height: 96, value: 16, label: "4", color: "#4CAF50" },
      { x: 500, y: 212, width: 60, height: 78, value: 13, label: "5", color: "#4CAF50" },
      { x: 600, y: 230, width: 60, height: 60, value: 10, label: "6", color: "#4CAF50" },
      { x: 700, y: 122, width: 60, height: 168, value: 28, label: "7", color: "#4CAF50" },
      { x: 800, y: 158, width: 60, height: 132, value: 22, label: "8", color: "#4CAF50" },
      { x: 900, y: 20, width: 60, height: 270, value: 90, label: "9", color: "#4CAF50" },
    ],
  },
};

// Period multipliers for card data
const periodMultipliers: PeriodMultiplier = {
  "This Week": 1.0,
  "This Month": 4.3, // approx. weeks in a month
  "This Year": 52.0, // weeks in a year
};

export const useDashboardData = (appName: string) => {
  const normalizedName = appName.toLowerCase();
  
  // Try exact match first
  let moduleKey = normalizedName;
  
  // If exact match not found, try partial matching
  if (!dashboardDataMap[moduleKey]) {
    if (normalizedName.includes('landscaping')) moduleKey = 'landscaping';
    else if (normalizedName.includes('mechanical') || normalizedName.includes('electrical')) moduleKey = 'mechanical_electrical';
    else if (normalizedName.includes('pool')) moduleKey = 'pool';
    else if (normalizedName.includes('pest')) moduleKey = 'pest';
    else if (normalizedName.includes('hygiene')) moduleKey = 'hygiene';
    else if (normalizedName.includes('security')) moduleKey = 'security';
    else moduleKey = 'security'; // default fallback
  }
  
  const moduleData = dashboardDataMap[moduleKey] || dashboardDataMap.security;
  const feedbackItems = feedbackItemsMap[moduleKey] || feedbackItemsMap.security;

  return {
    moduleData,
    cards: moduleData.cards,
    modules: moduleData.modules,
    feedbackItems,
    feedbackChartDataMap,
    periodMultipliers,
  };
};
