import { useState } from 'react';
import { 
  ShieldCheck,
  MapPin,
  Check,
  Clock,
  BarChart2, 
  Map
} from "lucide-react";
import Button from "@/components/common/button";
import StatCard from "@/components/common/dashStatCard";
import NavigationTabs from '@/components/common/tabs';

// Card Components
import { Card } from "@/components/common/cardWrapper"; 
import { CardHeader } from "@/components/common/cardHeader"; 

import InfoBanner from '@/components/common/infoBanner';
import { STAFF_DATA } from '@/constants/constant';
import { gpsTrackingtabs } from '@/constants/tabConstant';



export default function GpsTrackingPortal() {
  const [activeTab, setActiveTab] = useState<string>('Live tracking');

  return (
    <div className="my-15 text-slate-100 font-sans">
      {/* --- HEADER SECTION --- */}
      <div className="flex flex-col md:flex-row md:items-start md:justify-between gap-4 mb-6">
        <div>
          <div className="flex items-center gap-2 text-[12px] font-bold tracking-widest uppercase font-mono">
            <span className="text-teal-400">Apple One — HQ Tower</span>
            <span className="text-slate-600">•</span>
            <span className="text-slate-400">INTELLIGENCE</span>
            <span className="text-slate-600">•</span>
            <span className="text-slate-400">GPS TRACKING</span>
          </div>
          <h1 className="text-2xl font-bold text-white tracking-tight mt-2">
            GPS staff tracking & geo-fencing
          </h1>
          <p className="text-sm font-medium text-slate-400 mt-2 max-w-xl leading-relaxed">
            Live geo-tagged locations, geo-fence attendance validation, route history and site entry / exit logs — battery-optimised with cell-tower fallback and mobile-app capture.
          </p>
        </div>

        {/* --- HEADER BUTTONS --- */}
        <div className="flex items-center gap-3 self-start md:mt-10">
          <Button className="bg-[#0b1727] hover:bg-slate-800/60 text-slate-200 border border-slate-800 text-xs px-4 py-2 rounded-lg flex items-center gap-2 font-semibold transition-colors">
            <Map className="w-4 h-4 text-slate-300" />
            <span>Geo-fences</span>
          </Button>

          <Button className="bg-[#2dd4bf] hover:bg-teal-300 text-slate-950 font-bold text-xs px-4 py-2 rounded-lg flex items-center gap-2 transition-colors">
            <BarChart2 className="w-4 h-4 stroke-[2.5]" />
            <span>Export</span>
          </Button>
        </div>
      </div>

      {/* --- STAT CARDS --- */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        <StatCard
          variant="icon"
          title="On-site now"
          value="4"
          icon={ShieldCheck}
          badgeText=""
          badgeClassName="text-emerald-400 lowercase font-mono text-xs"
          iconContainerClassName="bg-emerald-950/40 border-emerald-800/30 text-emerald-400"
          subtitle=""
        />

        <StatCard
          variant="icon"
          title="En route"
          value="1"
          icon={MapPin}
          badgeText=""
          badgeClassName="text-slate-400 lowercase font-mono text-xs"
          iconContainerClassName="bg-sky-950/40 border-sky-800/30 text-sky-400"
          subtitle=""
        />

        <StatCard
          variant="icon"
          title="Auto check-ins"
          value="4"
          icon={Check}
          badgeText=""
          badgeClassName="text-teal-400 lowercase font-mono text-xs"
          iconContainerClassName="bg-emerald-950/40 border-emerald-800/30 text-emerald-400"
          subtitle=""
        />

        <StatCard
          variant="icon"
          title="Avg on-site"
          value="6.7 h"
          icon={Clock}
          badgeText=""
          badgeClassName="text-slate-400 lowercase font-mono text-xs"
          iconContainerClassName="bg-amber-950/40 border-amber-800/30 text-amber-400"
          subtitle=""
        />
      </div>

      {/* --- NAVIGATION TABS --- */}
      <NavigationTabs
        tabs={gpsTrackingtabs}
        activeTab={activeTab}
        onTabChange={setActiveTab}
        className="mb-4"
      />

      {/* --- MAP & STAFF STATUS SECTION BELOW TABS --- */}
      <div className="grid grid-cols-1 lg:grid-cols-12 gap-4 mb-6">
        {/* Left: Live Staff Map */}
        <div className="lg:col-span-7">
          <Card className="bg-[#08121e] border-[#132235] p-5 rounded-xl h-full flex flex-col justify-between min-h-[380px]">
            <CardHeader title="Live staff map · Apple One — HQ Tower" subtitle="GPS · 1s refresh" />
            
            {/* Map Canvas Visual */}
            <div className="relative flex-1 mt-4 rounded-lg border border-teal-500/20 bg-[#060e17] overflow-hidden p-4 flex items-center justify-center">
              {/* Grid Background Lines */}
              <div 
                className="absolute inset-0 opacity-15"
                style={{
                  backgroundImage: `radial-gradient(#2dd4bf 1px, transparent 1px)`,
                  backgroundSize: '20px 20px'
                }}
              />

              {/* Geo-Fence Boundary Box */}
              <div className="relative w-[85%] h-[70%] border-2 border-dashed border-teal-400/60 rounded-xl p-2">
                <span className="absolute -top-3 left-4 bg-[#060e17] px-2 text-[10px] font-mono text-teal-400 tracking-wider">
                  GEO-FENCE · SITE BOUNDARY
                </span>

                {/* On-Site Staff Dots */}
                <div className="absolute top-[35%] left-[25%] w-3 h-3 bg-teal-400 rounded-full shadow-[0_0_10px_#2dd4bf]" />
                <div className="absolute top-[50%] left-[45%] w-3 h-3 bg-teal-400 rounded-full shadow-[0_0_10px_#2dd4bf]" />
                <div className="absolute top-[70%] left-[65%] w-3 h-3 bg-teal-400 rounded-full shadow-[0_0_10px_#2dd4bf]" />
                <div className="absolute top-[30%] left-[80%] w-3 h-3 bg-teal-400 rounded-full shadow-[0_0_10px_#2dd4bf]" />
              </div>

              {/* En-Route Staff Dot (Outside Fence) */}
              <div className="absolute left-6 top-1/2 -translate-y-1/2 w-3.5 h-3.5 bg-amber-400 rounded-full shadow-[0_0_10px_#fbbf24]" />
            </div>
          </Card>
        </div>

        {/* Right: Staff Status Table */}
        <div className="lg:col-span-5">
          <Card className="bg-[#08121e] border-[#132235] p-5 rounded-xl h-full flex flex-col justify-between">
            <CardHeader title="Staff status" subtitle="6 tracked" />
            
            <div className="mt-4 overflow-x-auto">
              <table className="w-full text-left text-xs">
                <thead>
                  <tr className="border-b border-[#132235] text-[10px] font-mono text-slate-500 uppercase tracking-wider">
                    <th className="pb-2 font-medium">NAME</th>
                    <th className="pb-2 font-medium">ROLE</th>
                    <th className="pb-2 font-medium">ZONE</th>
                    <th className="pb-2 font-medium">IN</th>
                    <th className="pb-2 font-medium">HRS</th>
                    <th className="pb-2 font-medium text-right">STATUS</th>
                  </tr>
                </thead>
                <tbody className="divide-y divide-[#132235]/60">
                  {STAFF_DATA.map((staff, idx) => (
                    <tr key={idx} className="hover:bg-slate-800/20 transition-colors">
                      <td className="py-2.5 font-semibold text-slate-200 pr-2">{staff.name}</td>
                      <td className="py-2.5 text-slate-400 max-w-[90px] truncate pr-2">{staff.role}</td>
                      <td className="py-2.5 text-slate-300 pr-2">{staff.zone}</td>
                      <td className="py-2.5 font-mono text-slate-400">{staff.inTime}</td>
                      <td className="py-2.5 font-mono text-slate-300">{staff.hrs}</td>
                      <td className="py-2.5 text-right">
                        <span 
                          className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[9px] font-mono font-semibold uppercase ${
                            staff.statusType === 'success' 
                              ? 'bg-emerald-950/80 text-emerald-400 border border-emerald-800/50' 
                              : staff.statusType === 'warning'
                              ? 'bg-amber-950/80 text-amber-400 border border-amber-800/50'
                              : 'bg-slate-800/80 text-slate-400 border border-slate-700/50'
                          }`}
                        >
                          <span className={`w-1 h-1 rounded-full ${
                            staff.statusType === 'success' ? 'bg-emerald-400' :
                            staff.statusType === 'warning' ? 'bg-amber-400' : 'bg-slate-400'
                          }`} />
                          {staff.status}
                        </span>
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
          </Card>
        </div>
      </div>

      <div className="mt-5">
        <InfoBanner 
          boldText='Geo-fencing:' 
          normalText='crossing a site boundary auto-marks check-in / check-out; GPS sampling adapts to battery and signal, with a cell-tower fallback when satellite accuracy drops. Click a technician to view their route.'
        />
      </div>
      
    </div>
  );
}