"use client"

import { useLayoutEffect, type ReactNode } from "react"
import { DEMO_USER } from "@/components/demo/demo-mail-data"
import { DEMO_MAIL_ACCOUNT_ID } from "@/lib/demo/demo-mail-api-data"
import { DemoMailProvider } from "@/lib/demo/demo-mail-context"
import { ComposeProvider } from "@/lib/compose-context"
import { ScheduledMailProvider } from "@/lib/scheduled-mail-context"
import { useAccountStore } from "@/lib/stores/account-store"
import { useComposeIdentitiesStore } from "@/lib/stores/compose-identities-store"

function ProductMailDemoBootstrap() {
  useLayoutEffect(() => {
    useAccountStore.getState().setActiveAccountId(DEMO_MAIL_ACCOUNT_ID)
    useComposeIdentitiesStore.getState().hydrateFromApi([
      {
        id: "product-demo-compose-identity",
        accountId: DEMO_MAIL_ACCOUNT_ID,
        name: DEMO_USER.name,
        email: DEMO_USER.email,
        defaultSignatureId: null,
        signatureHtml: null,
        isDefault: true,
      },
    ])
  }, [])
  return null
}

/** Providers minimaux pour embarquer le compositeur mail en démo produit. */
export function ProductMailDemoShell({ children }: { children: ReactNode }) {
  return (
    <DemoMailProvider onReset={() => {}}>
      <ProductMailDemoBootstrap />
      <ComposeProvider>
        <ScheduledMailProvider>
          <div className="h-full w-full bg-transparent text-foreground">{children}</div>
        </ScheduledMailProvider>
      </ComposeProvider>
    </DemoMailProvider>
  )
}
