'use client';

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

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

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

  const handleAccept = async () => {
    if (!player?.id || loading) return;
    setLoading(true);
    try {
      await apiFetch('/api/employment/hire', {
        method: 'POST',
        body: JSON.stringify({
          type: 'livreur',
          employer_name: 'Karim Express',
        }),
      });
    } catch (err) {
      console.error('Erreur creation emploi:', err);
    } finally {
      setLoading(false);
      onNext();
    }
  };

  return (
    <div className="text-center">
      <h1 className="font-gothic text-3xl text-gold-gradient mb-2">Premier Boulot</h1>
      <p className="mb-6" style={{ color: 'var(--text-secondary)' }}>
        Karim, un commerçant du quartier, cherche un coursier.
        C&apos;est l&apos;occasion de gagner tes premiers ∞.
      </p>
      <div className="panel-glass rounded-xl p-6 max-w-lg mx-auto mb-6 text-left">
        <div className="flex items-center gap-3 mb-3">
          <div className="w-12 h-12 rounded-full flex items-center justify-center text-xl"
            style={{ background: 'var(--bg-tertiary)' }}>👤</div>
          <div>
            <h3 className="font-bold text-sm" style={{ color: 'var(--text-primary)' }}>Karim — Karim Express</h3>
            <p className="text-xs" style={{ color: 'var(--text-muted)' }}>Coursier • 12∞/heure</p>
          </div>
        </div>
        <p className="text-xs" style={{ color: 'var(--text-secondary)' }}>
          &quot;Eh toi, t&apos;as l&apos;air de quelqu&apos;un qui cherche du boulot.
          J&apos;ai des livraisons à faire dans le quartier. Ça t&apos;intéresse ?&quot;
        </p>
      </div>
      <button
        onClick={handleAccept}
        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 ? 'Embauche en cours...' : 'Accepter le boulot'}
      </button>
    </div>
  );
}
