'use client';

import { useState } from 'react';
import { useGameStore } from '@/stores/gameStore';
import { apiFetch } from '@/lib/api';

interface StepProps { onNext: () => void; }

export function StepPhone({ onNext }: StepProps) {
  const { player } = useGameStore();
  const [loading, setLoading] = useState(false);

  const handleTake = async () => {
    if (!player?.id || loading) return;
    setLoading(true);
    try {
      await apiFetch(`/api/player/${player.id}/onboarding-choice`, {
        method: 'POST',
        body: JSON.stringify({ step: 'phone', choice: 'brikfon_1000' }),
      });
    } catch (err) {
      console.error('Erreur attribution telephone:', err);
    } finally {
      setLoading(false);
      onNext();
    }
  };

  return (
    <div className="text-center">
      <h1 className="font-gothic text-3xl text-gold-gradient mb-2">Trouver un Téléphone</h1>
      <p className="mb-6" style={{ color: 'var(--text-secondary)' }}>
        Tu as besoin d&apos;un téléphone pour communiquer et accéder aux services.
        Un vendeur de rue te propose un modèle basique.
      </p>
      <div className="panel-glass rounded-xl p-6 max-w-sm mx-auto mb-6">
        <div className="text-4xl mb-2">📱</div>
        <h3 className="font-bold" style={{ color: 'var(--text-primary)' }}>BrikFon 1000</h3>
        <p className="text-xs mb-2" style={{ color: 'var(--text-muted)' }}>480p • SMS + Journal</p>
        <p className="text-lg font-bold" style={{ color: '#22c55e' }}>Gratuit</p>
        <p className="text-[10px] mt-1" style={{ color: 'var(--text-muted)' }}>Premier téléphone offert</p>
      </div>
      <button
        onClick={handleTake}
        disabled={loading}
        className="px-8 py-3 rounded-lg font-bold text-sm hover:scale-105 transition-all disabled:opacity-50"
        style={{ background: 'linear-gradient(135deg, var(--color-red-dark), var(--color-red))', color: 'white' }}
      >
        {loading ? 'Attribution...' : 'Prendre le téléphone'}
      </button>
    </div>
  );
}
