'use client';
import { useState } from 'react';
import { Banknote, Handshake, Scale } from 'lucide-react';
import { useGameStore } from '@/stores/gameStore';
import { apiFetch } from '@/lib/api';
interface StepProps { onNext: () => void; }

export function StepExtraBill({ onNext }: StepProps) {
  const [choice, setChoice] = useState<string | null>(null);
  const [saving, setSaving] = useState(false);
  const playerId = useGameStore((s) => s.player?.id);

  const handleChoice = async (c: string) => {
    if (saving) return;
    setChoice(c);
    setSaving(true);

    if (playerId) {
      try {
        await apiFetch(`/api/player/${playerId}/onboarding-choice`, {
          method: 'POST',
          body: JSON.stringify({ step: 'extra_bill', choice: c }),
        });
      } catch {
      }
    }

    setTimeout(() => {
      setSaving(false);
      onNext();
    }, 800);
  };

  return (
    <div className="text-center">
      <h1 className="font-gothic text-3xl text-gold-gradient mb-2">Le Billet Supplémentaire</h1>
      <p className="mb-6" style={{ color: 'var(--text-secondary)' }}>
        En faisant ta livraison, tu trouves un billet de 100∞ par terre.
        Personne ne t&apos;a vu. Que fais-tu ?
      </p>
      <div className="space-y-3 max-w-lg mx-auto">
        <button onClick={() => handleChoice('keep')}
          disabled={saving}
          className={`w-full text-left p-4 rounded-xl panel-glass transition-all ${choice === 'keep' ? 'scale-[1.02]' : 'hover:scale-[1.01]'}`}
          style={{ borderColor: choice === 'keep' ? 'var(--color-red)' : undefined, borderWidth: choice === 'keep' ? '2px' : undefined }}>
          <h3 className="font-bold text-sm flex items-center gap-2" style={{ color: 'var(--color-red-light)' }}>
            <Banknote className="w-4 h-4" /> Tu le gardes
          </h3>
          <p className="text-xs mt-1" style={{ color: 'var(--text-muted)' }}>+100∞ • Conscience -10</p>
        </button>
        <button onClick={() => handleChoice('return')}
          disabled={saving}
          className={`w-full text-left p-4 rounded-xl panel-glass transition-all ${choice === 'return' ? 'scale-[1.02]' : 'hover:scale-[1.01]'}`}
          style={{ borderColor: choice === 'return' ? '#22c55e' : undefined, borderWidth: choice === 'return' ? '2px' : undefined }}>
          <h3 className="font-bold text-sm flex items-center gap-2" style={{ color: '#22c55e' }}>
            <Handshake className="w-4 h-4" /> Tu le rends à Karim
          </h3>
          <p className="text-xs mt-1" style={{ color: 'var(--text-muted)' }}>Conscience +15 • Respect +5</p>
        </button>
        <button onClick={() => handleChoice('split')}
          disabled={saving}
          className={`w-full text-left p-4 rounded-xl panel-glass transition-all ${choice === 'split' ? 'scale-[1.02]' : 'hover:scale-[1.01]'}`}
          style={{ borderColor: choice === 'split' ? 'var(--color-gold)' : undefined, borderWidth: choice === 'split' ? '2px' : undefined }}>
          <h3 className="font-bold text-sm flex items-center gap-2" style={{ color: 'var(--color-gold)' }}>
            <Scale className="w-4 h-4" /> Tu partages 50/50
          </h3>
          <p className="text-xs mt-1" style={{ color: 'var(--text-muted)' }}>+50∞ • Conscience +5 • Social +5</p>
        </button>
      </div>
    </div>
  );
}
