import { LEADS_STATUS } from '@/utills/constant';
import { LeadsRecordI } from './interface';

export const allLeads = [
  {
    id: 1,
    followUpDateTime: '2023-10-20 14:30',
    customerName: 'Alice Johnson',
    project: 'Web Development',
    config: 'Basic',
    budgetStatus: 5000,
    status: 'Approved',
  },
  {
    id: 2,
    followUpDateTime: '2023-10-21 10:00',
    customerName: 'Bob Smith',
    project: 'Mobile App Development',
    config: 'Advanced',
    budget: 8000,
    status: 'Pending',
  },
  {
    id: 3,
    followUpDateTime: '2023-10-22 15:45',
    customerName: 'Charlie Brown',
    project: 'E-commerce Website',
    config: 'Pro',
    budgetStatus: 12000,
    status: 'Rejected',
  },
  {
    id: 4,
    followUpDateTime: '2023-10-23 09:15',
    customerName: 'David Green',
    project: 'Data Analysis',
    config: 'Intermediate',
    budgetStatus: 7500,
    status: 'Approved',
  },
  {
    id: 5,
    followUpDateTime: '2023-10-24 11:30',
    customerName: 'Ella Davis',
    project: 'Graphic Design',
    config: 'Basic',
    budgetStatus: 3000,
    status: 'Pending',
  },
  {
    id: 6,
    followUpDateTime: '2023-11-25 16:00',
    customerName: 'Frank Wilson',
    project: 'SEO Optimization',
    config: 'Pro',
    budgetStatus: 9500,
    status: 'Approved',
  },
  // Add more objects with data as needed
];
export const scheduled = [
  {
    id: 1,
    scheduledDateTime: '2023-11-20 14:30',
    customerName: 'Alice Johnson',
    project: 'Web Development',
    config: 'Basic',
    budgetStatus: 5000,
    status: 'Approved',
  },
  {
    id: 2,
    scheduledDateTime: '2023-11-21 10:00',
    customerName: 'Bob Smith',
    project: 'Mobile App Development',
    config: 'Advanced',
    budget: 8000,
    status: 'Pending',
  },
  {
    id: 3,
    scheduledDateTime: '2023-11-22 15:45',
    customerName: 'Charlie Brown',
    project: 'E-commerce Website',
    config: 'Pro',
    budgetStatus: 12000,
    status: 'Rejected',
  },
  {
    id: 4,
    scheduledDateTime: '2023-11-23 09:15',
    customerName: 'David Green',
    project: 'Data Analysis',
    config: 'Intermediate',
    budgetStatus: 7500,
    status: 'Approved',
  },
  {
    id: 5,
    scheduledDateTime: '2023-11-24 11:30',
    customerName: 'Ella Davis',
    project: 'Graphic Design',
    config: 'Basic',
    budgetStatus: 3000,
    status: 'Pending',
  },
  {
    id: 6,
    scheduledDateTime: '2023-11-25 16:00',
    customerName: 'Frank Wilson',
    project: 'SEO Optimization',
    config: 'Pro',
    budgetStatus: 9500,
    status: 'Approved',
  },
  // Add more objects with data as needed
];
export const activeLeads = [
  {
    id: 1,
    inquiryDate: '2023-11-20 14:30',
    customerName: 'Alice Johnson',
    project: 'Web Development',
    config: 'Basic',
    budgetStatus: 5000,
    status: 'Approved',
    launch: '2023-11-20 14:30',
  },
  {
    id: 2,
    inquiryDate: '2023-11-21 10:00',
    customerName: 'Bob Smith',
    project: 'Mobile App Development',
    config: 'Advanced',
    budget: 8000,
    status: 'Pending',
    launch: '2023-11-20 14:30',
  },
  {
    id: 3,
    inquiryDate: '2023-11-22 15:45',
    customerName: 'Charlie Brown',
    project: 'E-commerce Website',
    config: 'Pro',
    budgetStatus: 12000,
    status: 'Rejected',
    launch: '2023-11-20 14:30',
  },
  {
    id: 4,
    inquiryDate: '2023-11-23 09:15',
    customerName: 'David Green',
    project: 'Data Analysis',
    config: 'Intermediate',
    budgetStatus: 7500,
    status: 'Approved',
    launch: '2023-11-20 14:30',
  },
  {
    id: 5,
    inquiryDate: '2023-11-24 11:30',
    customerName: 'Ella Davis',
    project: 'Graphic Design',
    config: 'Basic',
    budgetStatus: 3000,
    status: 'Pending',
    launch: '2023-11-20 14:30',
  },
  {
    id: 6,
    inquiryDate: '2023-11-25 16:00',
    customerName: 'Frank Wilson',
    project: 'SEO Optimization',
    config: 'Pro',
    budgetStatus: 9500,
    status: 'Approved',
    launch: '2023-11-20 14:30',
  },
  // Add more objects with data as needed
];
export const overdueLeads = [
  {
    id: 1,
    followUpDateTime: '2023-11-20 14:30',
    customerName: 'Alice Johnson',
    project: 'Web Development',
    config: 'Basic',
    budgetStatus: 5000,
    status: 'Approved',
    launch: '2023-11-20 14:30',
  },
  {
    id: 2,
    followUpDateTime: '2023-11-21 10:00',
    customerName: 'Bob Smith',
    project: 'Mobile App Development',
    config: 'Advanced',
    budget: 8000,
    status: 'Pending',
    launch: '2023-11-20 14:30',
  },
  {
    id: 3,
    followUpDateTime: '2023-11-22 15:45',
    customerName: 'Charlie Brown',
    project: 'E-commerce Website',
    config: 'Pro',
    budgetStatus: 12000,
    status: 'Rejected',
    launch: '2023-11-20 14:30',
  },
  {
    id: 4,
    followUpDateTime: '2023-11-23 09:15',
    customerName: 'David Green',
    project: 'Data Analysis',
    config: 'Intermediate',
    budgetStatus: 7500,
    status: 'Approved',
    launch: '2023-11-20 14:30',
  },
  {
    id: 5,
    followUpDateTime: '2023-11-24 11:30',
    customerName: 'Ella Davis',
    project: 'Graphic Design',
    config: 'Basic',
    budgetStatus: 3000,
    status: 'Pending',
    launch: '2023-11-20 14:30',
  },
  {
    id: 6,
    followUpDateTime: '2023-11-25 16:00',
    customerName: 'Frank Wilson',
    project: 'SEO Optimization',
    config: 'Pro',
    budgetStatus: 9500,
    status: 'Approved',
    launch: '2023-11-20 14:30',
  },
  // Add more objects with data as needed
];

export const tableScrollToStart = () => {
  const tableContent: HTMLElement | null = document.querySelector('.ant-table-content');
  if (tableContent) {
    tableContent.scrollLeft = 0;
  }
};

export function checkLeadMatchesTab(tabKey: string, lead: LeadsRecordI): boolean {
  const now = new Date();
  const today = now.toISOString().split('T')[0]; // 'YYYY-MM-DD'

  switch (tabKey) {
    case 'today_follow_up': {
      if (!lead.follow_up_date) return false;

      const followUpDate = new Date(lead.follow_up_date);
      if (isNaN(followUpDate.getTime())) return false;

      const now = new Date();
      const today = now.toISOString().split('T')[0]; // YYYY-MM-DD
      const followUpDateOnly = followUpDate.toISOString().split('T')[0];

      const allowedStatuses = [
        LEADS_STATUS.scheduled_sv.id,
        LEADS_STATUS.scheduled_obm.id,
        LEADS_STATUS.scheduled_vc.id,
        LEADS_STATUS.ringing.id,
        LEADS_STATUS.notReachable.id,
        LEADS_STATUS.switchOff.id,
        LEADS_STATUS.interested.id,
        LEADS_STATUS.visit_sv.id,
        LEADS_STATUS.visit_vc.id,
        LEADS_STATUS.visit_obm.id,
        LEADS_STATUS.talkDone.id,
        LEADS_STATUS.didNotShowUp.id,
      ];

      const disallowedStatuses = [
        LEADS_STATUS.bookingCancelled.id,
        LEADS_STATUS.notInterested.id,
        LEADS_STATUS.broker.id,
        LEADS_STATUS.fake.id,
        LEADS_STATUS.dead?.id,
      ];

      return (
        followUpDateOnly === today &&
        followUpDate.getTime() >= now.getTime() &&
        allowedStatuses.includes(lead.status_id!) &&
        !disallowedStatuses.includes(lead.status_id!)
      );
    }

    case 'overdue': {
      const createdAt = lead.created_at ? new Date(lead.created_at) : null;
      const followUpAt = lead.follow_up_date ? new Date(lead.follow_up_date) : null;

      const isCreatedOverAnHourAgo =
        createdAt instanceof Date &&
        !isNaN(createdAt.getTime()) &&
        createdAt.getTime() < now.getTime() - 60 * 60 * 1000 &&
        lead.status_id === LEADS_STATUS.assigned.id;

      const isPastFollowUp =
        followUpAt instanceof Date &&
        !isNaN(followUpAt.getTime()) &&
        followUpAt.getTime() < now.getTime() &&
        lead.follow_up_date !== '';

      const allowedStatuses = [
        LEADS_STATUS.assigned.id,
        LEADS_STATUS.scheduled_sv.id,
        LEADS_STATUS.scheduled_obm.id,
        LEADS_STATUS.scheduled_vc.id,
        LEADS_STATUS.ringing.id,
        LEADS_STATUS.notReachable.id,
        LEADS_STATUS.switchOff.id,
        LEADS_STATUS.interested.id,
        LEADS_STATUS.visit_sv.id,
        LEADS_STATUS.visit_vc.id,
        LEADS_STATUS.visit_obm.id,
        LEADS_STATUS.didNotAnswer.id,
        LEADS_STATUS.didNotShowUp.id,
        LEADS_STATUS.talkDone.id,
      ];

      const disallowedStatuses = [
        LEADS_STATUS.bookingCancelled.id,
        LEADS_STATUS.notInterested.id,
        LEADS_STATUS.broker.id,
        LEADS_STATUS.fake.id,
        LEADS_STATUS.dead?.id,
      ];

      return (
        (isCreatedOverAnHourAgo || isPastFollowUp) &&
        allowedStatuses.includes(lead.status_id ?? -1) &&
        !disallowedStatuses.includes(lead.status_id ?? -1)
      );
    }

    case 'visited': {
        const visitedStatuses = [
          LEADS_STATUS.visit_sv.id,
          LEADS_STATUS.visit_vc.id,
          LEADS_STATUS.visit_obm.id,
        ];

        const statusMatch = visitedStatuses.includes(lead.status_id ?? -1);

        if (!statusMatch) return false;

        return true;
      }

      case 'active': {
        const followUpDate = lead.follow_up_date ? new Date(lead.follow_up_date) : null;

        const isFollowUpEmpty =
          !lead.follow_up_date || lead.follow_up_date === '';

        const isFutureFollowUp =
          followUpDate instanceof Date &&
          !isNaN(followUpDate.getTime()) &&
          followUpDate.toISOString().split('T')[0] > today;

        const disallowedStatuses = [
          LEADS_STATUS.assigned.id,
          LEADS_STATUS.scheduled_sv.id,
          LEADS_STATUS.scheduled_obm.id,
          LEADS_STATUS.scheduled_vc.id,
          LEADS_STATUS.dead?.id,
          LEADS_STATUS.bookingDone.id,
          LEADS_STATUS.bookingCancelled.id,
          LEADS_STATUS.notInterested.id,
          LEADS_STATUS.broker.id,
          LEADS_STATUS.fake.id,
          LEADS_STATUS.visit_sv.id,
          LEADS_STATUS.visit_vc.id,
          LEADS_STATUS.visit_obm.id,
          LEADS_STATUS.tokenDone.id,
        ];

        return (
          (isFollowUpEmpty || isFutureFollowUp) &&
          !disallowedStatuses.includes(lead.status_id ?? -1)
        );
      }

    case 'fresh_lead': {
        if (!lead.created_at) return false;

        const createdAt = new Date(lead.created_at);
        if (isNaN(createdAt.getTime())) return false;

        const now = new Date();
        const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000); // 1 hour before now

        const allowedStatuses = [LEADS_STATUS.assigned.id];

        const disallowedStatuses = [
          LEADS_STATUS.bookingCancelled.id,
          LEADS_STATUS.notInterested.id,
          LEADS_STATUS.broker.id,
          LEADS_STATUS.fake.id,
          LEADS_STATUS.dead?.id,
          LEADS_STATUS.scheduled_sv.id,
          LEADS_STATUS.scheduled_obm.id,
          LEADS_STATUS.scheduled_vc.id,
          LEADS_STATUS.ringing.id,
          LEADS_STATUS.notReachable.id,
          LEADS_STATUS.switchOff.id,
          LEADS_STATUS.interested.id,
          LEADS_STATUS.visit_sv.id,
          LEADS_STATUS.visit_vc.id,
          LEADS_STATUS.visit_obm.id,
          LEADS_STATUS.didNotAnswer.id,
          LEADS_STATUS.didNotShowUp.id,
          LEADS_STATUS.talkDone.id,
        ];

        return (
          createdAt >= oneHourAgo &&
          allowedStatuses.includes(lead.status_id!) &&
          !disallowedStatuses.includes(lead.status_id!)
        );
      }
    case 'scheduled': {
      const scheduledStatuses = [
        LEADS_STATUS.scheduled_sv.id,
        LEADS_STATUS.scheduled_vc.id,
        LEADS_STATUS.scheduled_obm.id,
      ];

      return scheduledStatuses.includes(lead.status_id ?? -1);
    }
    case 'token_done': {
      return LEADS_STATUS.tokenDone.id === lead.status_id;
    }
    case 'booked': {
      return LEADS_STATUS.bookingDone.id === lead.status_id;
    }
    case 'dead': {
    return [
      LEADS_STATUS.dead.id,
      LEADS_STATUS.bookingCancelled.id,
      LEADS_STATUS.notInterested.id,
      LEADS_STATUS.broker.id,
      LEADS_STATUS.fake.id,
    ].includes(lead.status_id!);
  }

    default:
      return false;
  }
}

