import { Calendar, Briefcase, Mail } from 'lucide-react';
import { formatDistanceToNow } from 'date-fns';
import { User, Job, Application } from '@prisma/client';

type AdminUserOverview = User & {
    jobs?: (Job & { applicant_count?: number })[];
    applications?: (Application & { job?: { title: string; company: string } })[];
};

export function OverviewTab({ user }: { user: AdminUserOverview }) {
    return (
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6">

            {/* Left Column - Quick Info */}
            <div className="col-span-1 space-y-6">
                <div className="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
                    <h3 className="text-sm font-semibold text-slate-900 uppercase tracking-wider mb-4">Account Details</h3>
                    <div className="space-y-4 text-sm">
                        <div className="flex items-center gap-3 text-slate-600">
                            <Mail className="w-4 h-4 text-slate-400" />
                            <span className="truncate">{user.email}</span>
                        </div>
                        <div className="flex items-center gap-3 text-slate-600">
                            <Calendar className="w-4 h-4 text-slate-400" />
                            <span>Joined {new Date(user.created_at).toLocaleDateString()}</span>
                        </div>
                        <div className="flex items-center gap-3 text-slate-600">
                            <div className="w-4 h-4 border border-slate-300 rounded-sm flex items-center justify-center text-slate-400 text-[10px]">ID</div>
                            <span className="font-mono text-xs truncate" title={user.id}>{user.id}</span>
                        </div>
                    </div>
                </div>

                {/* Profile Completeness - Mock for now */}
                <div className="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
                    <h3 className="text-sm font-semibold text-slate-900 uppercase tracking-wider mb-4">Profile Completeness</h3>
                    <div className="w-full bg-slate-100 rounded-full h-2.5 mb-2">
                        <div className="bg-blue-600 h-2.5 rounded-full" style={{ width: '85%' }}></div>
                    </div>
                    <p className="text-xs text-slate-500 text-right">85% Complete</p>
                </div>
            </div>

            {/* Middle/Right Column - Recent Activity & Summaries */}
            <div className="col-span-1 md:col-span-2 space-y-6">
                <div className="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
                    <h3 className="text-lg font-bold text-slate-900 mb-6">Recent Platform Activity</h3>

                    {user.role === 'employer' && (
                        <div>
                            <h4 className="text-sm font-semibold text-slate-500 uppercase tracking-wider mb-4">Latest Jobs Posted</h4>
                            {user.jobs && user.jobs.length > 0 ? (
                                <div className="space-y-3">
                                    {user.jobs.slice(0, 3).map((job) => (
                                        <div key={job.id} className="flex justify-between items-center p-3 hover:bg-slate-50 rounded-lg border border-slate-100">
                                            <div>
                                                <div className="font-medium text-slate-900 text-sm">{job.title}</div>
                                                <div className="text-xs text-slate-500 mt-0.5">{job.work_location_type} • {job.applicant_count} applicants</div>
                                            </div>
                                            <div className="text-xs text-slate-400">
                                                {formatDistanceToNow(new Date(job.posted_at), { addSuffix: true })}
                                            </div>
                                        </div>
                                    ))}
                                </div>
                            ) : (
                                <p className="text-sm text-slate-500 italic">No recent jobs posted.</p>
                            )}
                        </div>
                    )}

                    {user.role === 'candidate' && (
                        <div>
                            <h4 className="text-sm font-semibold text-slate-500 uppercase tracking-wider mb-4">Recent Applications</h4>
                            {user.applications && user.applications.length > 0 ? (
                                <div className="space-y-3">
                                    {user.applications.slice(0, 3).map((app) => (
                                        <div key={app.id} className="flex justify-between items-center p-3 hover:bg-slate-50 rounded-lg border border-slate-100">
                                            <div>
                                                <div className="font-medium text-slate-900 text-sm">{app.job?.title}</div>
                                                <div className="text-xs text-slate-500 mt-0.5">{app.job?.company}</div>
                                            </div>
                                            <div className="text-right">
                                                <span className="inline-block px-2 py-1 text-[10px] font-medium bg-slate-100 text-slate-600 rounded-md uppercase tracking-wider">
                                                    {app.status}
                                                </span>
                                                <div className="text-[10px] text-slate-400 mt-1">
                                                    {formatDistanceToNow(new Date(app.applied_at), { addSuffix: true })}
                                                </div>
                                            </div>
                                        </div>
                                    ))}
                                </div>
                            ) : (
                                <p className="text-sm text-slate-500 italic">No recent applications.</p>
                            )}
                        </div>
                    )}

                </div>
            </div>

        </div>
    );
}
