import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { Layers, Sparkles, Search, ArrowLeft, Bot, Wand2, ArrowRight } from 'lucide-react';
import toolsData from '../data/tools.json';

interface ToolsProps {
  onOpenAuth?: () => void;
}

export const Tools: React.FC<ToolsProps> = ({ onOpenAuth }) => {
  const [selectedCategory, setSelectedCategory] = useState<string>('all');
  const [searchQuery, setSearchQuery] = useState<string>('');

  const categories = [
    { id: 'all', name: 'همه ابزارها' },
    { id: 'تولید محتوا', name: 'تولید محتوا و متن' },
    { id: 'طراحی و تصویر', name: 'طراحی و تصویر' },
    { id: 'برنامه‌نویسی', name: 'برنامه‌نویسی و وب' },
    { id: 'صدا و موسیقی', name: 'صدا و موسیقی' },
    { id: 'کسب‌وکار و سئو', name: 'کسب‌وکار و سئو' },
  ];

  const filteredTools = toolsData.filter(t => {
    const matchCat = selectedCategory === 'all' || t.category.includes(selectedCategory);
    const q = searchQuery.toLowerCase();
    const matchSearch = !q || t.title.toLowerCase().includes(q) || t.description.toLowerCase().includes(q);
    return matchCat && matchSearch;
  });

  return (
    <div className="pt-28 pb-20 bg-slate-950 min-h-screen">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 relative z-10">
        
        {/* Header */}
        <div className="donya-section-header">
          <div className="inline-flex items-center gap-2 px-3.5 py-1.5 rounded-full bg-indigo-500/10 text-indigo-300 border border-indigo-500/20 text-xs font-bold mb-4">
            <Layers className="w-3.5 h-3.5" />
            <span>ویترین تخصصی ابزارهای هوش مصنوعی</span>
          </div>
          <h1 className="donya-section-title">
            <Sparkles className="w-7 h-7 text-indigo-400" />
            <span>دستیارها و ابزارهای هوش مصنوعی (+ هوشان)</span>
          </h1>
          <div className="donya-divider">
            <span className="donya-divider-line"></span>
            <span className="donya-divider-icon"></span>
            <span className="donya-divider-line"></span>
          </div>
          <p className="text-slate-400 text-sm sm:text-base max-w-2xl mx-auto mt-3">
            ابزارهای آماده برای تولید خودکار محتوا، طراحی لوگو، بهینه‌سازی سئو، کدنویسی و تبدیل صوت
          </p>
        </div>

        {/* Filter Bar */}
        <div className="glass-panel p-4 sm:p-6 rounded-2xl border border-white/5 mb-8 flex flex-col sm:flex-row items-center justify-between gap-4">
          <div className="flex flex-wrap gap-2 w-full sm:w-auto">
            {categories.map(c => (
              <button
                key={c.id}
                onClick={() => setSelectedCategory(c.id)}
                className={`px-4 py-2 rounded-xl text-xs sm:text-sm font-bold transition-all ${
                  selectedCategory === c.id
                    ? 'btn-donya-primary text-white shadow-md'
                    : 'bg-slate-900/60 text-slate-400 hover:text-white border border-white/5'
                }`}
              >
                {c.name}
              </button>
            ))}
          </div>

          <div className="relative w-full sm:w-72">
            <input
              type="text"
              placeholder="جستجو در ابزارها..."
              value={searchQuery}
              onChange={(e) => setSearchQuery(e.target.value)}
              className="w-full bg-slate-900/90 border border-white/10 rounded-xl pr-10 pl-4 py-2.5 text-xs sm:text-sm text-white focus:outline-none focus:border-indigo-500"
            />
            <Search className="absolute right-3.5 top-3 w-4 h-4 text-slate-500" />
          </div>
        </div>

        {/* Tools Grid (Elementor Page #4349) */}
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-16">
          {filteredTools.map((tool) => (
            <div
              key={tool.id}
              className="glass-panel rounded-2xl p-6 flex flex-col justify-between hover:border-indigo-500/40 transition-all duration-300 hover:-translate-y-1 shadow-xl group border border-white/5"
            >
              <div>
                <div className="flex items-center justify-between mb-4">
                  <div className="w-12 h-12 rounded-xl bg-indigo-600/20 border border-indigo-500/30 flex items-center justify-center text-2xl group-hover:scale-110 transition-transform">
                    {tool.icon || '⚡'}
                  </div>
                  <span className="px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-800 text-slate-300 border border-white/5">
                    {tool.category}
                  </span>
                </div>

                <h3 className="text-lg font-bold text-white mb-2 group-hover:text-indigo-300 transition-colors">
                  {tool.title}
                </h3>
                <p className="text-xs sm:text-sm text-slate-400 leading-relaxed mb-6">
                  {tool.description}
                </p>
              </div>

              <div className="pt-4 border-t border-white/5 flex items-center justify-between">
                <span className="text-xs text-emerald-400 font-medium">آماده استفاده</span>
                <Link
                  to={`/chat?tool=${encodeURIComponent(tool.title)}`}
                  className="btn-donya-primary px-4 py-2 rounded-xl text-xs font-bold flex items-center gap-1.5"
                >
                  <Wand2 className="w-3.5 h-3.5" />
                  <span>اجرای ابزار</span>
                </Link>
              </div>
            </div>
          ))}
        </div>

      </div>
    </div>
  );
};
