"use client";

import Link from "next/link";
import { ArrowRight } from "lucide-react";
import { motion } from "framer-motion";

export interface LandingCategory {
    id: string;
    name: string;
    count: number;
}

interface CategoriesProps {
    categories: LandingCategory[];
}

const getIconForCategory = (name: string) => {
    const lower = name.toLowerCase();
    if (lower.includes('analytic') || lower.includes('data')) return 'analytics';
    if (lower.includes('bank') || lower.includes('financ') || lower.includes('account')) return 'account_balance';
    if (lower.includes('design') || lower.includes('creativ') || lower.includes('art')) return 'palette';
    if (lower.includes('develop') || lower.includes('code') || lower.includes('it') || lower.includes('software')) return 'code';
    if (lower.includes('market')) return 'campaign';
    if (lower.includes('sale')) return 'storefront';
    if (lower.includes('hr') || lower.includes('human') || lower.includes('personnel')) return 'groups';
    if (lower.includes('grad') || lower.includes('education') || lower.includes('school')) return 'school';
    if (lower.includes('manage') || lower.includes('admin')) return 'manage_accounts';
    return 'work'; // Default icon
};

export function Categories({ categories }: CategoriesProps) {
    if (!categories || categories.length === 0) return null;

    // Limit to 8 categories for the grid layout
    const displayCategories = categories.slice(0, 8);

    return (
        <section className="py-24 bg-background-light relative z-10 border-b border-slate-100">
            <div className="max-w-7xl mx-auto px-6">
                <div className="flex flex-col md:flex-row items-center justify-between mb-12 gap-4">
                    <div className="text-left">
                        <h2 className="text-3xl md:text-4xl font-extrabold text-deep-navy font-display">
                            Find Jobs that matches your Interest
                        </h2>
                        <p className="text-lg text-slate-500 mt-2">
                            Search your career opportunity with our categories
                        </p>
                    </div>
                    <Link 
                        href="/jobs"
                        className="flex items-center gap-2 text-primary font-bold hover:gap-3 transition-all group"
                    >
                        Browse all
                        <ArrowRight className="h-5 w-5" />
                    </Link>
                </div>

                <motion.div 
                    initial="hidden"
                    whileInView="visible"
                    viewport={{ once: true, margin: "-100px" }}
                    variants={{
                        hidden: {},
                        visible: { transition: { staggerChildren: 0.05 } }
                    }}
                    className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6"
                >
                    {displayCategories.map((category) => (
                        <Link href={`/jobs?category=${category.name}`} key={category.id}>
                            <motion.div 
                                variants={{
                                    hidden: { opacity: 0, y: 15 },
                                    visible: { opacity: 1, y: 0, transition: { duration: 0.4 } }
                                }}
                                whileHover={{ scale: 1.03, y: -4 }}
                                className="bg-white p-6 rounded-xl border border-slate-200 hover:shadow-xl hover:shadow-primary/5 hover:border-primary/20 transition-all duration-300 cursor-pointer group"
                            >
                                <div className="w-12 h-12 bg-primary/5 text-primary rounded-xl flex items-center justify-center mb-6 group-hover:bg-primary group-hover:text-white transition-colors duration-300">
                                    <span className="material-symbols-outlined">{getIconForCategory(category.name)}</span>
                                </div>
                                <h3 className="text-lg font-bold text-deep-navy mb-2 line-clamp-1" title={category.name}>
                                    {category.name}
                                </h3>
                                <p className="text-slate-400 text-sm font-medium">
                                    {category.count} {category.count === 1 ? 'position' : 'positions'}
                                </p>
                            </motion.div>
                        </Link>
                    ))}
                </motion.div>
            </div>
        </section>
    );
}
