"use client";

import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";

export default function ProfilePage() {
  const router = useRouter();
  const [name, setName] = useState("Loading...");
  const [email, setEmail] = useState("Loading...");
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    // Fetch user profile from API
    fetch("/api/auth/profile")
      .then((r) => r.json())
      .then((data) => {
        if (data.user) {
          setName(data.user.name || "Parent");
          setEmail(data.user.email || "parent@example.com");
        }
        setLoading(false);
      })
      .catch(() => {
        setName("Parent");
        setEmail("parent@example.com");
        setLoading(false);
      });
  }, []);

  const saveProfile = async () => {
    // TODO: Call API to save profile
    alert("Profile saved!");
  };

  return (
    <div className="min-h-screen bg-gradient-to-br from-rose-50 to-amber-50 dark:from-gray-900 dark:to-gray-800">
      <div className="p-4 flex items-center gap-4">
        <button onClick={() => router.back()} className="p-2">←</button>
        <h1 className="text-xl font-bold">Profile</h1>
      </div>

      <div className="px-4 space-y-4">
        {/* Avatar */}
        <div className="flex flex-col items-center py-8">
          <div className="w-24 h-24 bg-rose-100 rounded-full flex items-center justify-center text-4xl mb-4">
            👤
          </div>
          <button className="text-rose-500 text-sm">Change Photo</button>
        </div>

        {/* Form */}
        {loading ? (
          <div className="text-center py-8 text-gray-400">Loading...</div>
        ) : (
          <div className="space-y-3">
            <div>
              <label className="block text-sm font-medium mb-1">Name</label>
              <input
                type="text"
                value={name}
                onChange={(e) => setName(e.target.value)}
                className="w-full p-3 bg-white dark:bg-gray-800 rounded-xl border"
              />
            </div>

            <div>
              <label className="block text-sm font-medium mb-1">Email</label>
              <input
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                className="w-full p-3 bg-white dark:bg-gray-800 rounded-xl border"
                disabled
              />
              <div className="text-xs text-gray-400 mt-1">Email cannot be changed</div>
            </div>

            <button
              onClick={saveProfile}
              className="w-full p-3 bg-rose-400 text-white rounded-xl mt-4"
            >
              Save Changes
            </button>
          </div>
        )}

        {/* Account Info */}
        <div className="p-4 bg-white dark:bg-gray-800 rounded-xl mt-6">
          <div className="font-medium mb-2">Account</div>
          <div className="text-sm text-gray-500">Member since: January 2024</div>
        </div>
      </div>
    </div>
  );
}