"use client";

import { useState } from "react";
import {
  
  Pencil,
  Plus,
  
} from "lucide-react";
import { initialModules } from "@/constants/constant";
import Button from "@/components/common/button";


export default function ModulesPortal() {
  const [selectedModuleId, setSelectedModuleId] = useState("MOD-01");

  const activeModule = initialModules.find((m) => m.id === selectedModuleId);

  return (
    <div className="my-15 text-slate-100  font-saas">
      {/* Header Section */}
      <div className="mb-6">
        <div className="flex items-center gap-2 text-[12px] font-semibold tracking-wider text-slate-400 uppercase font-mono mb-1">
          <span className="text-gray-400">PLATFORM</span>
          <span className="text-gray-600">•</span>
          <span className="text-gray-400">MODULES</span>
        </div>
        <h1 className="text-2xl font-bold text-white tracking-tight">Modules</h1>
        <p className="text-sm text-slate-400 mt-1 max-w-2xl">
          The catalog of system modules and their menus. These are what you switch on per tenant under feature access.
        </p>
      </div>

      {/* Main Grid Container */}
      <div className="grid grid-cols-1 lg:grid-cols-12 gap-6 items-start">
        {/* Left Column: Modules List */}
        <div className="lg:col-span-6 bg-[#0E1A2B]/60 border border-slate-800/80 rounded-xl p-5 backdrop-blur-sm">
          {/* Section Header */}
          <div className="flex items-center justify-between mb-4">
            <div>
              <h2 className="text-base font-semibold text-white">Modules</h2>
              <span className="text-xs font-mono text-slate-400">
                {initialModules.length} available
              </span>
            </div>
            <Button className="flex items-center gap-1.5 text-emerald-400 hover:text-emerald-300 text-xs font-semibold transition-colors">
              <Plus className="w-4 h-4" />
              <span>Add module</span>
            </Button>
          </div>

          {/* Module Items */}
          <div className="space-y-2 max-h-[620px] overflow-y-auto pr-1 custom-scrollbar">
            {initialModules.map((module) => {
              const Icon = module.icon;
              const isSelected = module.id === selectedModuleId;

              return (
                <div
                  key={module.id}
                  onClick={() => setSelectedModuleId(module.id)}
                  className={`group relative flex items-center justify-between p-3.5 rounded-lg border cursor-pointer transition-all ${
                    isSelected
                      ? "bg-teal-950/30 border-teal-500/40"
                      : "bg-[#09111E]/40 border-slate-800/60 hover:border-slate-700 hover:bg-[#0D1726]"
                  }`}
                >
                  <div className="flex items-center gap-3.5 min-w-0">
                    <div
                      className={`p-2.5 rounded-lg border shrink-0 ${
                        isSelected
                          ? "bg-teal-500/10 border-teal-500/30 text-teal-400"
                          : "bg-slate-900/80 border-slate-800 text-slate-400 group-hover:text-slate-200"
                      }`}
                    >
                      <Icon className="w-4 h-4" />
                    </div>
                    <div className="truncate">
                      <h3 className="text-sm font-semibold text-white truncate">
                        {module.title}
                      </h3>
                      <p className="text-xs text-slate-400 truncate mt-0.5">
                        {module.description}
                      </p>
                    </div>
                  </div>

                  <div className="flex items-center gap-2 shrink-0 ml-3">
                    <span className="text-[11px] font-mono bg-slate-900 border border-slate-800 text-slate-400 px-2 py-0.5 rounded">
                      {module.id}
                    </span>
                    <button
                      onClick={(e) => {
                        e.stopPropagation();
                        // Edit logic here
                      }}
                      className="p-1.5 text-slate-400 hover:text-white rounded-md hover:bg-slate-800 transition-colors"
                      title="Edit module"
                    >
                      <Pencil className="w-3.5 h-3.5" />
                    </button>
                  </div>
                </div>
              );
            })}
          </div>
        </div>

        {/* Right Column: Menus Panel */}
        <div className="lg:col-span-6 bg-[#0E1A2B]/60 border border-slate-800/80 rounded-xl p-5 min-h-[500px] flex flex-col justify-between backdrop-blur-sm">
          <div>
            {/* Menus Header */}
            <div className="flex items-center justify-between mb-4">
              <div>
                <h2 className="text-base font-semibold text-white">Menus</h2>
                <span className="text-xs font-mono text-slate-400">
                  Under {activeModule?.title || "Module"}
                </span>
              </div>
              <Button className="flex items-center gap-1.5 text-teal-400 hover:text-teal-300 text-xs font-semibold transition-colors">
                <Plus className="w-4 h-4" />
                <span>Add menu</span>
              </Button>
            </div>

            {/* Empty State / Menus List */}
            {activeModule?.menus && activeModule.menus.length > 0 ? (
              <div className="space-y-2 mt-4">
                {activeModule.menus.map((menu, idx) => (
                  <div
                    key={idx}
                    className="flex items-center justify-between p-3 rounded-lg bg-[#09111E]/40 border border-slate-800/60"
                  >
                    <span className="text-sm font-medium text-slate-200">{menu}</span>
                    <button className="text-xs text-slate-400 hover:text-white">
                      Edit
                    </button>
                  </div>
                ))}
              </div>
            ) : (
              <div className="flex flex-col items-center justify-center h-80 text-center">
                <p className="text-sm text-slate-400 font-medium">
                  No menus under this module yet.
                </p>
              </div>
            )}
          </div>
        </div>
      </div>

     
    </div>
  );
}