"use client";

import { useState, useEffect } from "react";
import { updateSitePreferences } from "@/actions/admin/preferences";
import { Button, Card, Input } from "@/components/ui";

interface SitePreferences {
  id: string;
  eventFeedbackPoints: number;
}

interface EventFeedbackPointsPreferencesProps {
  initialPreferences: SitePreferences;
}

export default function EventFeedbackPointsPreferences({
  initialPreferences,
}: EventFeedbackPointsPreferencesProps) {
  const [preferences, setPreferences] = useState<SitePreferences | null>(
    initialPreferences
  );
  const [isEditing, setIsEditing] = useState(false);
  const [formData, setFormData] = useState({
    eventFeedbackPoints: initialPreferences.eventFeedbackPoints.toString(),
  });
  const [isSaving, setIsSaving] = useState(false);

  // Synchroniser les préférences quand initialPreferences change
  useEffect(() => {
    setPreferences(initialPreferences);
    setFormData({
      eventFeedbackPoints: initialPreferences.eventFeedbackPoints.toString(),
    });
  }, [initialPreferences]);

  const handleEdit = () => {
    setIsEditing(true);
  };

  const handleSave = async () => {
    const points = parseInt(formData.eventFeedbackPoints, 10);
    
    if (isNaN(points) || points < 0) {
      alert("Le nombre de points doit être un nombre positif");
      return;
    }

    setIsSaving(true);
    try {
      const result = await updateSitePreferences({
        eventFeedbackPoints: points,
      });

      if (result.success && result.data) {
        setPreferences(result.data);
        setFormData({
          eventFeedbackPoints: result.data.eventFeedbackPoints.toString(),
        });
        setIsEditing(false);
      } else {
        console.error("Error updating preferences:", result.error);
        alert(result.error || "Erreur lors de la mise à jour");
      }
    } catch (error) {
      console.error("Error updating preferences:", error);
      alert("Erreur lors de la mise à jour");
    } finally {
      setIsSaving(false);
    }
  };

  const handleCancel = () => {
    setIsEditing(false);
    if (preferences) {
      setFormData({
        eventFeedbackPoints: preferences.eventFeedbackPoints.toString(),
      });
    }
  };

  return (
    <Card variant="default" className="p-3 sm:p-4">
      <div className="flex flex-col sm:flex-row sm:justify-between sm:items-start gap-3 mb-4">
        <div className="min-w-0 flex-1">
          <h3 className="text-pixel-gold font-bold text-base sm:text-lg break-words">
            Points de feedback sur les événements
          </h3>
          <p className="text-gray-400 text-xs sm:text-sm">
            Nombre de points attribués lorsqu&apos;un utilisateur donne un feedback à un événement (première fois uniquement)
          </p>
        </div>
        {!isEditing && (
          <Button
            onClick={handleEdit}
            variant="primary"
            size="sm"
            className="whitespace-nowrap flex-shrink-0"
          >
            Modifier
          </Button>
        )}
      </div>

      {isEditing ? (
        <div className="space-y-4">
          <div>
            <label
              htmlFor="eventFeedbackPoints"
              className="block text-sm font-medium text-pixel-gold mb-2"
            >
              Points de feedback
            </label>
            <Input
              id="eventFeedbackPoints"
              type="number"
              min="0"
              value={formData.eventFeedbackPoints}
              onChange={(e) =>
                setFormData({
                  ...formData,
                  eventFeedbackPoints: e.target.value,
                })
              }
              placeholder="100"
              className="w-full"
            />
            <p className="text-xs text-gray-400 mt-1">
              Les utilisateurs gagneront ce nombre de points lors de leur premier feedback sur un événement
            </p>
          </div>
          <div className="flex flex-col sm:flex-row gap-2 pt-4">
            <Button
              onClick={handleSave}
              variant="success"
              size="md"
              disabled={isSaving}
            >
              {isSaving ? "Enregistrement..." : "Enregistrer"}
            </Button>
            <Button
              onClick={handleCancel}
              variant="secondary"
              size="md"
              disabled={isSaving}
            >
              Annuler
            </Button>
          </div>
        </div>
      ) : (
        <div className="flex flex-col sm:flex-row sm:items-center gap-2 sm:gap-4">
          <span className="text-pixel-gold font-bold text-sm sm:text-base min-w-0 sm:min-w-[200px] flex-shrink-0">
            Points actuels:
          </span>
          <div className="flex items-center gap-2">
            <span className="text-lg sm:text-xl font-bold text-white">
              {preferences?.eventFeedbackPoints ?? 100}
            </span>
            <span className="text-xs sm:text-sm text-gray-400">points</span>
          </div>
        </div>
      )}
    </Card>
  );
}

