import { useState, useEffect } from "react"

const TOAST_LIMIT = 1

let count = 0
function generateId() {
  count = (count + 1) % Number.MAX_VALUE
  return count.toString()
}

const toastStore = {
  state: {
    toasts: [],
  },
  listeners: [],
  
  getState: () => toastStore.state,
  
  setState: (nextState) => {
    if (typeof nextState === 'function') {
      toastStore.state = nextState(toastStore.state)
    } else {
      toastStore.state = { ...toastStore.state, ...nextState }
    }
    
    toastStore.listeners.forEach(listener => listener(toastStore.state))
  },
  
  subscribe: (listener) => {
    toastStore.listeners.push(listener)
    return () => {
      toastStore.listeners = toastStore.listeners.filter(l => l !== listener)
    }
  }
}

export const toast = ({ ...props }) => {
  // Hard blocklist for legacy placeholders / prompt-related messaging.
  // If any toast contains these patterns, it must NOT render.
  const text = `${props?.title ?? ''} ${props?.description ?? ''}`
    .toString()
    .replace(/\s+/g, ' ')
    .trim();

  const BLOCKLIST = [
    /prochain\s+demande/i,
    /prochaine\s+demande/i,
    /prochain\s+prompt/i,
    /\bprompt\b/i,
    /\bllm\b/i,
    /\bopenai\b/i,
    /\bgpt\b/i,
    /\bclaude\b/i,
    /pas\s+encore\s+impl[ée]ment/i,
    /sera\s+impl[ée]ment/i,
    /fonctionnalit[ée]\s+.*\b(a\s+venir|à\s+venir)\b/i,
    /\bcoming\s+soon\b/i,
    /\bnot\s+implemented\b/i,
  ];

  if (text && BLOCKLIST.some((re) => re.test(text))) {
    // Return a no-op handle so callers don't break.
    return {
      id: 'blocked',
      dismiss: () => {},
      update: () => {},
    };
  }

  const id = generateId()

  const update = (props) =>
    toastStore.setState((state) => ({
      ...state,
      toasts: state.toasts.map((t) =>
        t.id === id ? { ...t, ...props } : t
      ),
    }))

  const dismiss = () => toastStore.setState((state) => ({
    ...state,
    toasts: state.toasts.filter((t) => t.id !== id),
  }))

  toastStore.setState((state) => ({
    ...state,
    toasts: [
      { ...props, id, dismiss },
      ...state.toasts,
    ].slice(0, TOAST_LIMIT),
  }))

  return {
    id,
    dismiss,
    update,
  }
}

export function useToast() {
  const [state, setState] = useState(toastStore.getState())
  
  useEffect(() => {
    const unsubscribe = toastStore.subscribe((state) => {
      setState(state)
    })
    
    return unsubscribe
  }, [])
  
  useEffect(() => {
    const timeouts = []

    state.toasts.forEach((toast) => {
      if (toast.duration === Infinity) {
        return
      }

      const timeout = setTimeout(() => {
        toast.dismiss()
      }, toast.duration || 5000)

      timeouts.push(timeout)
    })

    return () => {
      timeouts.forEach((timeout) => clearTimeout(timeout))
    }
  }, [state.toasts])

  return {
    toast,
    toasts: state.toasts,
  }
}
