import type { Email } from "@/lib/email-data"
import type { VideoConferenceProvider } from "@/lib/calendar-invitation"

type DemoVcBrand = {
  provider: VideoConferenceProvider
  id: string
  brandLabel: string
  sender: string
  senderEmail: string
  summary: string
  location: string
  /** Heure de début (Europe/Paris) — 15 mai 2026 */
  startHour: number
  startMinute?: number
  /** Jour du mois (mai 2026, Europe/Paris) */
  mailDay: number
  read?: boolean
}

function fixtureParisIso(day: number, hour: number, minute = 0): string {
  const p = (n: number) => String(n).padStart(2, "0")
  return `2026-05-${p(day)}T${p(hour)}:${p(minute)}:00+02:00`
}

const DEMO_ORGANIZER = "demo.organisateur@ultimail.test"
const DEMO_ATTENDEE = "moi@example.com"

const BODY_STYLE =
  "font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.5; color: #202124;"

function buildIcs(spec: {
  uid: string
  summary: string
  location: string
  startHour: number
  startMinute: number
  durationMinutes: number
}): string {
  const pad = (n: number) => String(n).padStart(2, "0")
  const sh = spec.startHour
  const sm = spec.startMinute
  const endTotal = sh * 60 + sm + spec.durationMinutes
  const eh = Math.floor(endTotal / 60) % 24
  const em = endTotal % 60
  const start = `20260515T${pad(sh)}${pad(sm)}00`
  const end = `20260515T${pad(eh)}${pad(em)}00`

  return `BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART;TZID=Europe/Paris:${start}
DTEND;TZID=Europe/Paris:${end}
DTSTAMP:20260514T120000Z
ORGANIZER;CN=Demo Ultimail:MAILTO:${DEMO_ORGANIZER}
UID:${spec.uid}
ATTENDEE;CN=Moi:MAILTO:${DEMO_ATTENDEE}
SUMMARY:${spec.summary}
LOCATION:${spec.location}
DESCRIPTION:Lien visio: ${spec.location}
END:VEVENT
END:VCALENDAR`
}

function buildDemoVcEmail(brand: DemoVcBrand): Email {
  const startMinute = brand.startMinute ?? 0
  const ics = buildIcs({
    uid: `vc-demo-${brand.provider}@ultimail.test`,
    summary: brand.summary,
    location: brand.location,
    startHour: brand.startHour,
    startMinute,
    durationMinutes: 45,
  })

  const timeLabel = `${String(brand.startHour).padStart(2, "0")}:${String(startMinute).padStart(2, "0")}`
  const endHour = Math.floor((brand.startHour * 60 + startMinute + 45) / 60) % 24
  const endMin = (brand.startHour * 60 + startMinute + 45) % 60
  const endLabel = `${String(endHour).padStart(2, "0")}:${String(endMin).padStart(2, "0")}`

  return {
    id: brand.id,
    sender: brand.sender,
    senderEmail: brand.senderEmail,
    subject: `Invitation: ${brand.summary} - ven. 15 mai 2026 ${timeLabel} (Europe/Paris)`,
    preview: `Invitation ${brand.brandLabel} : ${brand.summary} — ven. 15 mai 2026 ${timeLabel} – ${endLabel}`,
    body: `<div style="${BODY_STYLE}">
<p>Vous êtes invité·e à une réunion <strong>${brand.brandLabel}</strong>.</p>
<p><strong>${brand.summary}</strong><br/>
ven. 15 mai 2026 ${timeLabel} (Europe/Paris)</p>
<p><a href="${brand.location}">Rejoindre</a></p>
</div>`,
    date: fixtureParisIso(brand.mailDay, brand.startHour, startMinute),
    read: brand.read ?? false,
    starred: false,
    important: false,
    hasInvitation: true,
    hasAttachment: true,
    labels: ["inbox", "Démos visio"],
    calendarInvitation: { ics },
    attachments: [
      {
        name: `${brand.provider}-invite.ics`,
        kind: "other",
        sizeBytes: 2_800,
        inlineText: ics,
      },
    ],
  }
}

const DEMO_BRANDS: DemoVcBrand[] = [
  {
    provider: "meet",
    id: "vc-demo-meet",
    brandLabel: "Google Meet",
    sender: "Google Agenda",
    senderEmail: "calendar-notification@google.com",
    summary: "Point produit — Google Meet",
    location: "https://meet.google.com/ultimail-demo-meet",
    startHour: 9,
    mailDay: 14,
  },
  {
    provider: "teams",
    id: "vc-demo-teams",
    brandLabel: "Microsoft Teams",
    sender: "Microsoft Teams",
    senderEmail: "noreply@teams.microsoft.com",
    summary: "Sprint review — Teams",
    location:
      "https://teams.microsoft.com/l/meetup-join/19%3ameeting_ultimail_demo",
    startHour: 10,
    mailDay: 14,
  },
  {
    provider: "zoom",
    id: "vc-demo-zoom",
    brandLabel: "Zoom",
    sender: "Zoom",
    senderEmail: "no-reply@zoom.us",
    summary: "Stand-up équipe — Zoom",
    location: "https://zoom.us/j/98765432101?pwd=demo",
    startHour: 11,
    mailDay: 14,
  },
  {
    provider: "skype",
    id: "vc-demo-skype",
    brandLabel: "Skype",
    sender: "Skype",
    senderEmail: "notifications@skype.com",
    summary: "Entretien client — Skype",
    location: "https://join.skype.com/invite/ultimailDemoSkype",
    startHour: 12,
    mailDay: 14,
  },
  {
    provider: "jitsi",
    id: "vc-demo-jitsi",
    brandLabel: "Jitsi",
    sender: "Jitsi Meet",
    senderEmail: "noreply@jitsi.org",
    summary: "Atelier open source — Jitsi",
    location: "https://meet.jit.si/ultimail-demo-jitsi",
    startHour: 13,
    mailDay: 14,
  },
  {
    provider: "whatsapp",
    id: "vc-demo-whatsapp",
    brandLabel: "WhatsApp",
    sender: "WhatsApp",
    senderEmail: "invite@whatsapp.com",
    summary: "Groupe projet — WhatsApp",
    location: "https://chat.whatsapp.com/InviteUltimailDemoWA",
    startHour: 14,
    mailDay: 14,
  },
  {
    provider: "signal",
    id: "vc-demo-signal",
    brandLabel: "Signal",
    sender: "Signal",
    senderEmail: "noreply@signal.org",
    summary: "Comité confidentialité — Signal",
    location: "https://signal.group/#UltimailDemoSignalGroup",
    startHour: 15,
    mailDay: 14,
  },
  {
    provider: "instagram",
    id: "vc-demo-instagram",
    brandLabel: "Instagram",
    sender: "Instagram",
    senderEmail: "mail@instagram.com",
    summary: "Live créateurs — Instagram",
    location: "https://www.instagram.com/direct/t/ultimail-demo-live",
    startHour: 16,
    mailDay: 14,
  },
  {
    provider: "discord",
    id: "vc-demo-discord",
    brandLabel: "Discord",
    sender: "Discord",
    senderEmail: "noreply@discord.com",
    summary: "Voice channel équipe — Discord",
    location: "https://discord.gg/ultimail-demo-voice",
    startHour: 17,
    mailDay: 14,
  },
  {
    provider: "slack",
    id: "vc-demo-slack",
    brandLabel: "Slack",
    sender: "Slack",
    senderEmail: "notification@slack.com",
    summary: "Huddle quotidien — Slack",
    location: "https://app.slack.com/huddle/TULTIMail/CdemoHuddle",
    startHour: 9,
    startMinute: 30,
    mailDay: 13,
    read: true,
  },
  {
    provider: "telegram",
    id: "vc-demo-telegram",
    brandLabel: "Telegram",
    sender: "Telegram",
    senderEmail: "noreply@telegram.org",
    summary: "Appel groupe — Telegram",
    location: "https://t.me/joinchat/UltimailDemoTelegram",
    startHour: 10,
    startMinute: 30,
    mailDay: 13,
    read: true,
  },
  {
    provider: "messenger",
    id: "vc-demo-messenger",
    brandLabel: "Messenger",
    sender: "Messenger",
    senderEmail: "noreply@messenger.com",
    summary: "Room famille — Messenger",
    location: "https://m.me/c/ultimail-demo-room",
    startHour: 11,
    startMinute: 30,
    mailDay: 13,
    read: true,
  },
  {
    provider: "facetime",
    id: "vc-demo-facetime",
    brandLabel: "FaceTime",
    sender: "Apple Calendar",
    senderEmail: "calendar@icloud.com",
    summary: "Visio FaceTime — design",
    location: "facetime://demo.organisateur@ultimail.test",
    startHour: 14,
    startMinute: 30,
    mailDay: 13,
    read: true,
  },
  {
    provider: "phone",
    id: "vc-demo-phone",
    brandLabel: "Téléphone",
    sender: "Secrétariat",
    senderEmail: "secretariat@ultimail.test",
    summary: "Conf call PSTN — téléphone",
    location: "tel:+33183927510",
    startHour: 15,
    startMinute: 30,
    mailDay: 13,
    read: true,
  },
  {
    provider: "other",
    id: "vc-demo-webex",
    brandLabel: "Webex",
    sender: "Cisco Webex",
    senderEmail: "noreply@webex.com",
    summary: "Réunion entreprise — Webex",
    location: "https://company.webex.com/meet/ultimail-demo-webex",
    startHour: 16,
    startMinute: 30,
    mailDay: 13,
    read: true,
  },
]

/** Un message boîte de réception par marque de visioconférence (fixtures). */
export const demoCalendarInvitationEmails: Email[] = DEMO_BRANDS.map(buildDemoVcEmail)
