'use client';
import React, { useEffect, useState } from 'react';
import api from '@/lib/api';

export default function ProfilePage() {
  const [user, setUser] = useState<{ email: string; role: string } | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchUser = async () => {
      try {
        const res = await api.get('/auth/me');
        setUser(res.data.user);
      } catch (err) {
        console.error("Failed to fetch user", err);
      } finally {
        setLoading(false);
      }
    };
    fetchUser();
  }, []);

  return (
    <div className="p-8 max-w-4xl mx-auto font-sans h-full">
      <div className="mb-8 bg-white p-6 rounded-2xl shadow-sm border border-gray-200">
        <h1 className="text-3xl font-extrabold text-gray-900 tracking-tight">Account Profile</h1>
        <p className="text-gray-500 mt-1">Manage your personal settings and subscription details.</p>
      </div>

      <div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-8">
        {loading ? (
          <div className="text-center text-gray-500">Loading profile...</div>
        ) : user ? (
          <div className="space-y-6">
            
            <div className="flex items-center gap-6 pb-6 border-b border-gray-100">
               <div className="w-20 h-20 bg-indigo-100 text-indigo-600 rounded-full flex items-center justify-center text-3xl font-bold uppercase shadow-sm">
                  {user.email.charAt(0)}
               </div>
               <div>
                  <h2 className="text-2xl font-bold text-gray-900">{user.email}</h2>
                  <span className="inline-flex items-center mt-2 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider bg-indigo-50 text-indigo-700 border border-indigo-100">
                    {user.role.replace('_', ' ')} Plan
                  </span>
               </div>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 gap-8 pt-4">
               <div>
                  <h3 className="text-lg font-bold text-gray-900 mb-4">Security</h3>
                  <button className="w-full bg-white border border-gray-300 hover:bg-gray-50 text-gray-700 font-semibold py-3 px-4 rounded-xl transition-colors shadow-sm text-left flex justify-between items-center">
                      Change Password
                      <svg className="w-5 h-5 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" /></svg>
                  </button>
               </div>
               <div>
                  <h3 className="text-lg font-bold text-gray-900 mb-4">Billing</h3>
                  <button className="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-3 px-4 rounded-xl transition-colors shadow-sm flex items-center justify-center gap-2">
                      <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z" /></svg>
                      Upgrade Subscription
                  </button>
               </div>
            </div>

          </div>
        ) : (
          <div className="text-center text-red-500">Failed to load profile.</div>
        )}
      </div>
    </div>
  );
}
