import type { Call } from '@/types/entities';
import { daysAgo } from './factories';
import { mockLeads } from './leads';

// Helper: pick a lead by stable index (module-level, after mockLeads is defined)
const getLeadAt = (index: number) => mockLeads[index % mockLeads.length];

// Helper: build an ISO end time given a start time ISO string and duration in seconds
function addSeconds(isoStart: string, seconds: number): string {
    const d = new Date(isoStart);
    d.setSeconds(d.getSeconds() + seconds);
    return d.toISOString();
}

// Spread leads across call records for denormalized display data
const lead0 = getLeadAt(0);
const lead1 = getLeadAt(1);
const lead2 = getLeadAt(2);
const lead3 = getLeadAt(3);
const lead4 = getLeadAt(4);
const lead5 = getLeadAt(5);
const lead6 = getLeadAt(6);
const lead7 = getLeadAt(7);
const lead8 = getLeadAt(8);
const lead9 = getLeadAt(9);
const lead10 = getLeadAt(10);
const lead11 = getLeadAt(11);
const lead12 = getLeadAt(12);
const lead13 = getLeadAt(13);
const lead14 = getLeadAt(14);

function leadDisplayName(lead: (typeof mockLeads)[number]): string {
    const fn = lead.contactName?.firstName ?? '';
    const ln = lead.contactName?.lastName ?? '';
    return `${fn} ${ln}`.trim() || 'Unknown';
}

function leadDisplayPhone(lead: (typeof mockLeads)[number]): string {
    return lead.contactPhone?.[0]?.number ?? '';
}

// Today and yesterday timestamps (hours offset)
function hoursAgo(h: number): string {
    const d = new Date();
    d.setHours(d.getHours() - h);
    return d.toISOString();
}

const c1Start = hoursAgo(0.5);
const c2Start = hoursAgo(1.2);
const c3Start = hoursAgo(2.1);
const c4Start = hoursAgo(3.0);
const c5Start = hoursAgo(4.5);
const c6Start = hoursAgo(5.3);
const c7Start = hoursAgo(6.0);
const c8Start = hoursAgo(7.2);
const c9Start = hoursAgo(8.8);
const c10Start = hoursAgo(10.0);
// Yesterday calls
const c11Start = daysAgo(1.05);
const c12Start = daysAgo(1.1);
const c13Start = daysAgo(1.2);
const c14Start = daysAgo(1.35);
const c15Start = daysAgo(1.5);

export const mockCalls: Call[] = [
    // --- Rekha S. calls (8 total) ---
    {
        id: 'call-1',
        createdAt: c1Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead0.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c1Start,
        endedAt: addSeconds(c1Start, 320),
        durationSeconds: 320,
        recordingUrl: null,
        disposition: 'APPOINTMENT_BOOKED',
        callNotes: 'Patient confirmed appointment for IVF consultation on Friday.',
        patientId: null,
        appointmentId: null,
        leadId: lead0.id,
        leadName: leadDisplayName(lead0),
        leadPhone: leadDisplayPhone(lead0),
        leadService: lead0.interestedService ?? undefined,
    },
    {
        id: 'call-2',
        createdAt: c2Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead1.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c2Start,
        endedAt: addSeconds(c2Start, 180),
        durationSeconds: 180,
        recordingUrl: null,
        disposition: 'INFO_PROVIDED',
        callNotes: 'Caller asked about cervical screening package pricing. Sent WhatsApp brochure.',
        patientId: null,
        appointmentId: null,
        leadId: lead1.id,
        leadName: leadDisplayName(lead1),
        leadPhone: leadDisplayPhone(lead1),
        leadService: lead1.interestedService ?? undefined,
    },
    {
        id: 'call-3',
        createdAt: c3Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead2.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c3Start,
        endedAt: addSeconds(c3Start, 540),
        durationSeconds: 540,
        recordingUrl: null,
        disposition: 'FOLLOW_UP_SCHEDULED',
        callNotes: 'Lead expressed interest but needs to discuss with spouse. Follow-up scheduled for tomorrow.',
        patientId: null,
        appointmentId: null,
        leadId: lead2.id,
        leadName: leadDisplayName(lead2),
        leadPhone: leadDisplayPhone(lead2),
        leadService: lead2.interestedService ?? undefined,
    },
    {
        id: 'call-4',
        createdAt: c4Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead3.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c4Start,
        endedAt: addSeconds(c4Start, 90),
        durationSeconds: 90,
        recordingUrl: null,
        disposition: 'NO_ANSWER',
        callNotes: 'Caller disconnected before agent could respond.',
        patientId: null,
        appointmentId: null,
        leadId: lead3.id,
        leadName: leadDisplayName(lead3),
        leadPhone: leadDisplayPhone(lead3),
        leadService: lead3.interestedService ?? undefined,
    },
    {
        id: 'call-5',
        createdAt: c7Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead4.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c7Start,
        endedAt: addSeconds(c7Start, 420),
        durationSeconds: 420,
        recordingUrl: null,
        disposition: 'APPOINTMENT_BOOKED',
        callNotes: 'Booked fertility assessment appointment for next Monday at 10 AM.',
        patientId: null,
        appointmentId: null,
        leadId: lead4.id,
        leadName: leadDisplayName(lead4),
        leadPhone: leadDisplayPhone(lead4),
        leadService: lead4.interestedService ?? undefined,
    },
    {
        id: 'call-6',
        createdAt: c8Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead5.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c8Start,
        endedAt: addSeconds(c8Start, 600),
        durationSeconds: 600,
        recordingUrl: null,
        disposition: 'APPOINTMENT_BOOKED',
        callNotes: 'Long call — patient had many questions about IVF process. Appointment booked.',
        patientId: null,
        appointmentId: null,
        leadId: lead5.id,
        leadName: leadDisplayName(lead5),
        leadPhone: leadDisplayPhone(lead5),
        leadService: lead5.interestedService ?? undefined,
    },
    {
        id: 'call-7',
        createdAt: c11Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead6.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c11Start,
        endedAt: addSeconds(c11Start, 250),
        durationSeconds: 250,
        recordingUrl: null,
        disposition: 'CALLBACK_REQUESTED',
        callNotes: 'Caller requested callback after 6 PM — busy at work.',
        patientId: null,
        appointmentId: null,
        leadId: lead6.id,
        leadName: leadDisplayName(lead6),
        leadPhone: leadDisplayPhone(lead6),
        leadService: lead6.interestedService ?? undefined,
    },
    {
        id: 'call-8',
        createdAt: c12Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead7.contactPhone,
        agentName: 'Rekha S.',
        startedAt: c12Start,
        endedAt: addSeconds(c12Start, 390),
        durationSeconds: 390,
        recordingUrl: null,
        disposition: 'FOLLOW_UP_SCHEDULED',
        callNotes: 'Interested in mammography screening. Wants to check insurance coverage first.',
        patientId: null,
        appointmentId: null,
        leadId: lead7.id,
        leadName: leadDisplayName(lead7),
        leadPhone: leadDisplayPhone(lead7),
        leadService: lead7.interestedService ?? undefined,
    },

    // --- Suresh P. calls (5 total) ---
    {
        id: 'call-9',
        createdAt: c5Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead8.contactPhone,
        agentName: 'Suresh P.',
        startedAt: c5Start,
        endedAt: addSeconds(c5Start, 480),
        durationSeconds: 480,
        recordingUrl: null,
        disposition: 'APPOINTMENT_BOOKED',
        callNotes: 'Diabetic patient. Booked endocrinology consultation for Wednesday.',
        patientId: null,
        appointmentId: null,
        leadId: lead8.id,
        leadName: leadDisplayName(lead8),
        leadPhone: leadDisplayPhone(lead8),
        leadService: lead8.interestedService ?? undefined,
    },
    {
        id: 'call-10',
        createdAt: c6Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead9.contactPhone,
        agentName: 'Suresh P.',
        startedAt: c6Start,
        endedAt: addSeconds(c6Start, 120),
        durationSeconds: 120,
        recordingUrl: null,
        disposition: 'WRONG_NUMBER',
        callNotes: 'Caller was trying to reach a different clinic. Politely redirected.',
        patientId: null,
        appointmentId: null,
        leadId: lead9.id,
        leadName: leadDisplayName(lead9),
        leadPhone: leadDisplayPhone(lead9),
        leadService: lead9.interestedService ?? undefined,
    },
    {
        id: 'call-11',
        createdAt: c9Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead10.contactPhone,
        agentName: 'Suresh P.',
        startedAt: c9Start,
        endedAt: addSeconds(c9Start, 330),
        durationSeconds: 330,
        recordingUrl: null,
        disposition: 'INFO_PROVIDED',
        callNotes: 'Answered questions about orthopaedic consultation availability and waiting times.',
        patientId: null,
        appointmentId: null,
        leadId: lead10.id,
        leadName: leadDisplayName(lead10),
        leadPhone: leadDisplayPhone(lead10),
        leadService: lead10.interestedService ?? undefined,
    },
    {
        id: 'call-12',
        createdAt: c13Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead11.contactPhone,
        agentName: 'Suresh P.',
        startedAt: c13Start,
        endedAt: addSeconds(c13Start, 560),
        durationSeconds: 560,
        recordingUrl: null,
        disposition: 'FOLLOW_UP_SCHEDULED',
        callNotes: 'Interested in senior health package for elderly parent. Needs to confirm family schedule.',
        patientId: null,
        appointmentId: null,
        leadId: lead11.id,
        leadName: leadDisplayName(lead11),
        leadPhone: leadDisplayPhone(lead11),
        leadService: lead11.interestedService ?? undefined,
    },
    {
        id: 'call-13',
        createdAt: c14Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead12.contactPhone,
        agentName: 'Suresh P.',
        startedAt: c14Start,
        endedAt: addSeconds(c14Start, 200),
        durationSeconds: 200,
        recordingUrl: null,
        disposition: 'APPOINTMENT_BOOKED',
        callNotes: 'Quick call — patient knew exactly what they wanted. Cardiology checkup booked.',
        patientId: null,
        appointmentId: null,
        leadId: lead12.id,
        leadName: leadDisplayName(lead12),
        leadPhone: leadDisplayPhone(lead12),
        leadService: lead12.interestedService ?? undefined,
    },

    // --- Meena K. calls (2 total) ---
    {
        id: 'call-14',
        createdAt: c10Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead13.contactPhone,
        agentName: 'Meena K.',
        startedAt: c10Start,
        endedAt: addSeconds(c10Start, 440),
        durationSeconds: 440,
        recordingUrl: null,
        disposition: 'INFO_PROVIDED',
        callNotes: 'Patient inquired about egg freezing costs and eligibility criteria. Sent detailed email.',
        patientId: null,
        appointmentId: null,
        leadId: lead13.id,
        leadName: leadDisplayName(lead13),
        leadPhone: leadDisplayPhone(lead13),
        leadService: lead13.interestedService ?? undefined,
    },
    {
        id: 'call-15',
        createdAt: c15Start,
        callDirection: 'INBOUND',
        callStatus: 'COMPLETED',
        callerNumber: lead14.contactPhone,
        agentName: 'Meena K.',
        startedAt: c15Start,
        endedAt: addSeconds(c15Start, 60),
        durationSeconds: 60,
        recordingUrl: null,
        disposition: 'NO_ANSWER',
        callNotes: 'Caller dropped after 60 seconds — possible network issue. Sent WhatsApp message.',
        patientId: null,
        appointmentId: null,
        leadId: lead14.id,
        leadName: leadDisplayName(lead14),
        leadPhone: leadDisplayPhone(lead14),
        leadService: lead14.interestedService ?? undefined,
    },
];
