"use client";

import { useState } from "react";
import Link from "next/link";
import { Button } from "@/components/ui/button";

export default function ReviewsPage() {
  const [rating, setRating] = useState(0);
  const [comment, setComment] = useState("");
  const [loading, setLoading] = useState(false);
  const [success, setSuccess] = useState(false);

  async function handleSubmit() {
    if (rating === 0) return;
    setLoading(true);
    try {
      await fetch("/api/reviews", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ project_id: "123", reviewee_id: "456", rating, comment }),
      });
      setSuccess(true);
    } catch (err) {
      console.error(err);
    } finally {
      setLoading(false);
    }
  }

  return (
    <div className="min-h-screen bg-gray-50">
      <header className="bg-white border-b px-6 py-4 flex items-center justify-between">
        <Link href="/dashboard" className="text-blue-600 hover:text-blue-800">← Назад</Link>
        <h1 className="text-xl font-bold">Отзывы и рейтинги</h1>
      </header>

      <main className="container mx-auto px-4 py-8 max-w-2xl">
        <div className="bg-white rounded-xl shadow-sm p-6 mb-6">
          <h2 className="text-2xl font-bold mb-4">Оставить отзыв</h2>

          {/* Rating stars */}
          <div className="flex gap-1 mb-4">
            {[1, 2, 3, 4, 5].map((star) => (
              <button key={star} onClick={() => setRating(star)} className="text-3xl cursor-pointer">
                {star <= rating ? "⭐" : "☆"}
              </button>
            ))}
          </div>

          <textarea
            value={comment}
            onChange={(e) => setComment(e.target.value)}
            placeholder="Расскажите о вашем опыте..."
            className="w-full px-4 py-3 border rounded-lg mb-4 min-h-[100px]"
          />

          {success && (
            <div className="mb-4 p-3 bg-green-50 text-green-700 rounded">Спасибо за отзыв!</div>
          )}

          <Button onClick={handleSubmit} disabled={loading || rating === 0}>
            {loading ? "Отправка..." : `Оценить на ${rating}/5`}
          </Button>
        </div>

        {/* Reviews list */}
        <div className="space-y-4">
          {[1, 2, 3].map((i) => (
            <div key={i} className="bg-white rounded-xl shadow-sm p-6">
              <div className="flex items-center justify-between mb-2">
                <span className="font-semibold">Пользователь {i}</span>
                <span>{rating}/5</span>
              </div>
              <p className="text-gray-600 text-sm">Отличный опыт работы! Рекомендую.</p>
            </div>
          ))}
        </div>
      </main>
    </div>
  );
}
