"use client"

import {
  createContext,
  useContext,
  useMemo,
  type ReactNode,
} from "react"
import type { Email } from "@/lib/email-data"
import {
  useScheduledStore,
  type ScheduleSendPayload,
} from "@/lib/stores/scheduled-store"

export type { ScheduleSendPayload } from "@/lib/stores/scheduled-store"

type ScheduledMailContextValue = {
  scheduledEmails: Email[]
  snoozedEmails: Email[]
  sentPlaceholderEmails: Email[]
  refreshAll: () => Promise<void>
  scheduleSend: (payload: ScheduleSendPayload) => Promise<{ id: string }>
  removeScheduledLocal: (id: string) => void
  requestDeleteScheduled: (id: string) => Promise<void>
  requestArchiveScheduled: (id: string) => Promise<void>
  requestSnoozeScheduled: (id: string) => Promise<void>
  requestToggleReadScheduled: (id: string, read: boolean) => Promise<void>
  requestRescheduleScheduled: (id: string, sendAtIso: string) => Promise<void>
  requestGetScheduledEditPayload: (id: string) => Promise<ScheduleSendPayload | null>
  requestUpdateScheduledSend: (id: string, payload: ScheduleSendPayload) => Promise<void>
  requestSendScheduledNow: (id: string) => Promise<void>
  requestSnoozeMailboxEmail: (row: Email) => Promise<void>
  requestRestoreSnoozedToInbox: (row: Email) => Promise<void>
}

const ScheduledMailContext = createContext<ScheduledMailContextValue | null>(null)

const noop = async () => {}

export function ScheduledMailProvider({ children }: { children: ReactNode }) {
  const scheduledEmails = useScheduledStore((s) => s.scheduledEmails)
  const snoozedEmails = useScheduledStore((s) => s.snoozedEmails)
  const sentPlaceholderEmails = useScheduledStore((s) => s.sentPlaceholderEmails)

  const value = useMemo<ScheduledMailContextValue>(() => {
    const actions = useScheduledStore.getState()
    return {
      scheduledEmails,
      snoozedEmails,
      sentPlaceholderEmails,
      refreshAll: noop,
      scheduleSend: async (payload) => actions.createScheduledSend(payload),
      removeScheduledLocal: (id) => actions.removeScheduledLocal(id),
      requestDeleteScheduled: async (id) => { actions.deleteScheduledSend(id) },
      requestArchiveScheduled: async (id) => { actions.archiveScheduledSend(id) },
      requestSnoozeScheduled: async (id) => { actions.snoozeScheduledSend(id) },
      requestToggleReadScheduled: async (id, read) => { actions.markScheduledReadState(id, read) },
      requestRescheduleScheduled: async (id, sendAtIso) => { actions.rescheduleScheduledSend(id, sendAtIso) },
      requestGetScheduledEditPayload: async (id) => actions.getScheduledEditPayload(id),
      requestUpdateScheduledSend: async (id, payload) => { actions.updateScheduledSend(id, payload) },
      requestSendScheduledNow: async (id) => { actions.sendScheduledNow(id) },
      requestSnoozeMailboxEmail: async (row) => {
        actions.snoozeMailboxEmail(row)
      },
      requestRestoreSnoozedToInbox: async (row) => {
        actions.restoreSnoozedToInbox(row)
      },
    }
  }, [scheduledEmails, snoozedEmails, sentPlaceholderEmails])

  return (
    <ScheduledMailContext.Provider value={value}>
      {children}
    </ScheduledMailContext.Provider>
  )
}

export function useScheduledMail() {
  const ctx = useContext(ScheduledMailContext)
  if (!ctx) {
    throw new Error("useScheduledMail must be used within ScheduledMailProvider")
  }
  return ctx
}
