"use client";

import { Mail, Phone, MapPin, Link as LinkIcon, Briefcase, Linkedin, Github, Globe, Edit2 } from "lucide-react";
import Link from "next/link";
import { motion } from "framer-motion";

interface ProfileHeaderProps {
    user: any; // Prisma User with nested profile
    isOwnProfile?: boolean;
}

export function ProfileHeader({ user, isOwnProfile = false }: ProfileHeaderProps) {
    const profile = user.profile || {};

    // Construct full name if available, fallback to individual or email
    const displayName = user.name || (user.first_name && user.last_name ? `${user.first_name} ${user.last_name}` : user.email?.split('@')[0]) || "User";
    const title = profile.headline;
    const company = profile.company_name;
    const location = profile.location;

    return (
        <motion.div
            initial={{ opacity: 0, scale: 0.98 }}
            animate={{ opacity: 1, scale: 1 }}
            transition={{ duration: 0.4 }}
            className="bg-white border border-slate-100 rounded-3xl p-8 shadow-[0_8px_30px_rgb(0,0,0,0.04)] relative overflow-hidden"
        >
            {/* Background Decorative Element */}
            <div className="absolute top-0 left-0 right-0 h-32 bg-gradient-to-r from-primary/10 via-primary/5 to-transparent -z-10" />

            <div className="flex flex-col md:flex-row items-start md:items-end gap-6 relative z-10 pt-4">
                {/* Avatar */}
                <div className="flex-shrink-0 relative">
                    <motion.div
                        whileHover={{ scale: 1.05 }}
                        className="w-32 h-32 rounded-2xl bg-white border-4 border-white shadow-xl flex items-center justify-center text-primary text-4xl font-bold overflow-hidden"
                    >
                        {user.avatar_url ? (
                            <img src={user.avatar_url} alt={displayName} className="w-full h-full object-cover" />
                        ) : (
                            <div className="w-full h-full bg-gradient-to-br from-primary/20 to-primary/10 flex items-center justify-center">
                                {displayName.charAt(0).toUpperCase()}
                            </div>
                        )}
                    </motion.div>
                </div>

                {/* Info */}
                <div className="flex-1 pb-2 w-full">
                    <div className="flex flex-col sm:flex-row sm:items-start justify-between gap-4">
                        <div>
                            <h1 className="text-3xl font-bold text-slate-900 tracking-tight">{displayName}</h1>
                            {title && (
                                <p className="text-lg font-medium text-slate-600 mt-1.5 flex items-center gap-2">
                                    <Briefcase className="h-4 w-4 text-slate-400" />
                                    {title}
                                    {company && <span className="text-slate-400 font-normal">at {company}</span>}
                                </p>
                            )}
                            {location && (
                                <p className="text-sm text-slate-500 mt-2 flex items-center gap-1.5 font-medium">
                                    <MapPin className="h-4 w-4 text-slate-400" />
                                    {location}
                                </p>
                            )}
                        </div>

                        {isOwnProfile && (
                            <Link
                                href="/dashboard/profile/edit"
                                className="inline-flex items-center gap-2 px-5 py-2.5 rounded-xl bg-slate-900 border border-transparent text-sm font-semibold text-white hover:bg-slate-800 transition-all shadow-md hover:shadow-lg self-start whitespace-nowrap"
                            >
                                <Edit2 className="h-4 w-4" />
                                Edit Profile
                            </Link>
                        )}
                    </div>

                    {/* Contact Info */}
                    <div className="flex flex-wrap gap-x-6 gap-y-3 mt-6 pt-6 border-t border-slate-100">
                        {user.email && (
                            <a href={`mailto:${user.email}`} className="flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary transition-colors">
                                <Mail className="h-4 w-4 text-slate-400" />
                                {user.email}
                            </a>
                        )}
                        {profile.phone && (
                            <a href={`tel:${profile.phone}`} className="flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary transition-colors">
                                <Phone className="h-4 w-4 text-slate-400" />
                                {profile.phone}
                            </a>
                        )}
                        {profile.portfolio_url && (
                            <a href={profile.portfolio_url} target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary transition-colors">
                                <Globe className="h-4 w-4 text-slate-400" />
                                Portfolio
                            </a>
                        )}
                        {profile.linkedin_url && (
                            <a href={profile.linkedin_url} target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary transition-colors text-blue-600">
                                <Linkedin className="h-4 w-4" />
                                LinkedIn
                            </a>
                        )}
                        {profile.github_url && (
                            <a href={profile.github_url} target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary transition-colors">
                                <Github className="h-4 w-4 text-slate-400" />
                                GitHub
                            </a>
                        )}
                        {profile.website_url && (
                            <a href={profile.website_url} target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary transition-colors">
                                <LinkIcon className="h-4 w-4 text-slate-400" />
                                Website
                            </a>
                        )}
                        {profile.resume_url && (
                            <a href={profile.resume_url} target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 text-sm font-medium text-primary hover:text-primary/80 transition-colors bg-primary/5 px-3 py-1.5 rounded-lg border border-primary/10">
                                <LinkIcon className="h-4 w-4" />
                                View Resume
                            </a>
                        )}
                    </div>
                </div>
            </div>
        </motion.div>
    );
}
