'use client';
import { useEffect, useState } from 'react';

type SessionResp = { id?: string; token?: string; studioUrl?: string; username?: string; [k: string]: any };

async function resolveIdentity(): Promise<string | null> {
  // 1) try NextAuth standard endpoint
  try {
    const r = await fetch('/api/auth/session');
    if (r.ok) {
      const j = await r.json();
      // NextAuth returns { user: { name, email }, expires }
      if (j && j.user) {
        return j.user.name || j.user.email || null;
      }
    }
  } catch (e) { /* ignore */ }

  // 2) try a global set by the app
  try {
    // @ts-ignore
    if (typeof window !== 'undefined' && (window as any).__AVANZACAST_USER) {
      // @ts-ignore
      const u = (window as any).__AVANZACAST_USER;
      if (typeof u === 'string') return u;
      if (u && (u.name || u.id || u.email)) return u.name || u.id || u.email;
    }
  } catch (e) { /* ignore */ }

  // 3) fallback: prompt the user (only in dev/testing)
  try {
    const answer = window.prompt('Identifícate (nombre de usuario) para ligar con la room', 'visual-runner');
    if (answer && answer.trim()) return answer.trim();
  } catch (e) { /* ignore */ }

  return null;
}

export default function AutoRequestAndInject({ roomName }: { roomName: string }) {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [username, setUsername] = useState<string | null>(null);

  useEffect(() => {
    let mounted = true;
    async function run() {
      setLoading(true);
      setError(null);
      try {
        const identity = await resolveIdentity();
        if (!identity) throw new Error('No identity resolved; please login or set window.__AVANZACAST_USER');
        if (mounted) setUsername(identity);

        const resp = await fetch('/api/session/proxy', {
          method: 'POST', headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ room: roomName, username: identity, ttl: 300 }),
        });
        if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
        const json: SessionResp = await resp.json();

        // Try to get token if not in POST response
        if (!json.token) {
          if (json.id) {
            const getResp = await fetch(`/api/session/proxy/${encodeURIComponent(json.id)}`);
            if (getResp.ok) {
              const getJson = await getResp.json();
              if (getJson && getJson.token) {
                json.token = getJson.token;
              }
            }
          }
        }

        if (!json.token) throw new Error('No token returned from session proxy');

        // Post message with token to same-origin listeners
        window.postMessage({ type: 'LIVEKIT_TOKEN', token: json.token, room: roomName, username: identity }, window.location.origin);
        console.log('AutoRequestAndInject: posted LIVEKIT_TOKEN to window', { room: roomName, username: identity });
      } catch (err: any) {
        console.error('AutoRequestAndInject error', err);
        if (mounted) setError(String(err));
      } finally {
        if (mounted) setLoading(false);
      }
    }
    run();
    return () => { mounted = false; };
  }, [roomName]);

  if (loading) return <div aria-live="polite">Solicitando token de sesión para room "{roomName}"...</div>;
  if (error) return <div role="alert">Error solicitando token: {error}</div>;
  return (
    <div>
      {username ? <div>Identidad: <strong>{username}</strong></div> : null}
    </div>
  );
}
