'use client';
import { useState } from 'react';
import Image from 'next/image';
import { apiFetch } from '@/lib/api';
import { useGameStore } from '@/stores/gameStore';
interface StepProps { onNext: () => void; }

/**
 * StepComplete — dernière étape de l'onboarding.
 *
 * Crime-first : le joueur commence TOUJOURS par la voie criminelle.
 * Les voies Politique et Rédemption se débloquent via les missions GATE
 * (GATE_CR_05_DINER → politique/légal, GATE_PO_05_COULOIR → criminel/légal,
 *  GATE_RE_05_MARCHE → politique/criminel).
 *
 * Pas de choix présenté — on envoie directement choice='crime' au backend.
 */
export function StepComplete({ onNext }: StepProps) {
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const player = useGameStore((s) => s.player);

  const handleGo = async () => {
    if (!player?.id) return;
    setSaving(true);
    setError(null);
    try {
      await apiFetch(`/api/player/${player.id}/onboarding-choice`, {
        method: 'POST',
        body: JSON.stringify({ step: 'path', choice: 'crime' }),
      });
      onNext();
    } catch (err) {
      console.error('[StepComplete] Failed to save path choice:', err);
      setError('Erreur lors de la sauvegarde. Réessaie.');
    } finally {
      setSaving(false);
    }
  };

  return (
    <div className="text-center">
      <Image src="/images/logo.png" alt="Logo" width={80} height={80} className="mx-auto mb-4" />

      <h1 className="font-gothic text-4xl text-gold-gradient glow-gold mb-4">
        Enfin... passons aux choses sérieuses !
      </h1>

      <div className="max-w-md mx-auto mb-6 p-4 rounded-xl" style={{ background: 'rgba(196,30,30,0.1)', border: '1px solid rgba(196,30,30,0.3)' }}>
        <span className="text-3xl">💀</span>
        <p className="text-sm font-bold mt-2" style={{ color: 'var(--color-red)' }}>
          Voie Criminelle
        </p>
        <p className="text-xs italic mt-2" style={{ color: 'var(--text-primary)' }}>
          &ldquo;La loi du plus fort, c&apos;est la mienne. Je prends ce que je veux, par la force s&apos;il le faut.&rdquo;
        </p>
        <p className="text-[10px] mt-2" style={{ color: 'var(--text-muted)' }}>
          Commence par le crime. Les voies Politique et Rédemption se débloqueront au fil de ta progression.
        </p>
      </div>

      <p className="text-sm mb-8" style={{ color: 'var(--text-muted)' }}>
        Le monde est devant toi. Fais tes preuves dans la rue, et d&apos;autres portes s&apos;ouvriront.
      </p>

      {error && (
        <p className="text-sm mb-4" style={{ color: 'var(--color-red)' }}>{error}</p>
      )}

      <button onClick={handleGo} disabled={saving}
        className="px-12 py-4 rounded-xl font-bold text-xl transition-all hover:scale-110 disabled:opacity-50"
        style={{
          background: 'linear-gradient(135deg, var(--color-red-dark), var(--color-red), var(--color-gold-dark))',
          color: 'white',
          boxShadow: '0 4px 40px rgba(196,30,30,0.5)',
        }}>
        {saving ? 'Chargement...' : 'GO !'}
      </button>
    </div>
  );
}
