"use client";

import Link from "next/link";
import { cn } from "@/lib/utils";
import { Briefcase, UserPlus, FileText, CheckCircle } from "lucide-react";
import type { ActivityEvent } from "@/services/AdminService";

interface ActivityTimelineProps {
    events: ActivityEvent[];
}

const typeConfig: Record<string, { icon: React.ElementType; color: string; bg: string }> = {
    job_posted: { icon: Briefcase, color: "text-blue-500", bg: "bg-blue-50" },
    job_approved: { icon: CheckCircle, color: "text-emerald-500", bg: "bg-emerald-50" },
    user_registered: { icon: UserPlus, color: "text-purple-500", bg: "bg-purple-50" },
    application_submitted: { icon: FileText, color: "text-amber-500", bg: "bg-amber-50" },
};

function timeAgo(dateStr: string): string {
    const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
    if (seconds < 60) return "Just now";
    const minutes = Math.floor(seconds / 60);
    if (minutes < 60) return `${minutes}m ago`;
    const hours = Math.floor(minutes / 60);
    if (hours < 24) return `${hours}h ago`;
    const days = Math.floor(hours / 24);
    if (days < 7) return `${days}d ago`;
    return new Date(dateStr).toLocaleDateString("en-US", { month: "short", day: "numeric" });
}

export function ActivityTimeline({ events }: ActivityTimelineProps) {
    if (events.length === 0) {
        return (
            <div className="flex flex-col items-center justify-center py-12 text-center">
                <div className="flex h-12 w-12 items-center justify-center rounded-full bg-slate-50">
                    <FileText className="h-6 w-6 text-slate-300" />
                </div>
                <p className="mt-3 text-sm text-slate-500">No recent activity</p>
            </div>
        );
    }

    return (
        <div className="space-y-1">
            {events.map((event) => {
                const config = typeConfig[event.type] || typeConfig.job_posted;
                const Icon = config.icon;

                return (
                    <div
                        key={event.id}
                        className="group flex items-start gap-3 rounded-lg px-3 py-2.5 hover:bg-slate-50 transition-colors"
                    >
                        {/* Icon */}
                        <div className={cn(
                            "mt-0.5 flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-lg",
                            config.bg
                        )}>
                            <Icon className={cn("h-4 w-4", config.color)} />
                        </div>

                        {/* Content */}
                        <div className="min-w-0 flex-1">
                            <p className="text-sm text-slate-700">
                                {event.description}{" "}
                                <span className="font-semibold text-slate-900">{event.entityName}</span>
                            </p>
                            <p className="mt-0.5 text-xs text-slate-400">
                                {timeAgo(event.timestamp)}
                                {event.actor && ` · ${event.actor}`}
                            </p>
                        </div>

                        {/* View link */}
                        <Link
                            href={
                                event.type === 'user_registered'
                                    ? `/admin/users`
                                    : `/jobs/${event.entityId}`
                            }
                            className="flex-shrink-0 rounded-md px-2 py-1 text-xs font-medium text-blue-600 opacity-0 hover:bg-blue-50 group-hover:opacity-100 transition-all"
                        >
                            View
                        </Link>
                    </div>
                );
            })}
        </div>
    );
}
