import { useState } from 'react';
import { 
  Star, 
  ShieldCheck, 
  Activity, 
  Clock, 
  BarChart2, 
  Plus 
} from "lucide-react";
import Button from "@/utils/button";
import StatCard from "@/utils/dashStatCard";
import InfoBanner from "@/utils/infoBanner";

export default function VendorServicePortal() {
  const vendorData = [
    {
      name: "Voltas Ltd",
      id: "CTR-700",
      scope: "HVAC AMC & breakdown",
      rating: "4.6",
      sla: "95%",
      slaColor: "text-emerald-400",
      response: "4.0h",
      openJobs: "2",
    },
    {
      name: "Blue Star",
      id: "CTR-701",
      scope: "Chiller & VRV service",
      rating: "4.1",
      sla: "88%",
      slaColor: "text-amber-400",
      response: "1.2h",
      openJobs: "2",
    },
    {
      name: "Sterling FM",
      id: "CTR-702",
      scope: "Integrated FM / soft services",
      rating: "3.4",
      sla: "94%",
      slaColor: "text-emerald-400",
      response: "2.4h",
      openJobs: "4",
    },
    {
      name: "OTIS Elevators",
      id: "CTR-704",
      scope: "Elevator AMC",
      rating: "4.8",
      sla: "95%",
      slaColor: "text-emerald-400",
      response: "2.8h",
      openJobs: "—",
    },
    {
      name: "Sodexo",
      id: "CTR-705",
      scope: "Soft services & cafeteria",
      rating: "4.3",
      sla: "90%",
      slaColor: "text-amber-400",
      response: "2.8h",
      openJobs: "1",
    },
    {
      name: "In-house team",
      id: "—",
      scope: "Day-to-day maintenance",
      rating: "4.0",
      sla: "93%",
      slaColor: "text-amber-400",
      response: "3.2h",
      openJobs: "3",
    },
  ];

  const fieldCrewData = [
    {
      initials: "RK",
      name: "Ravi Kumar",
      role: "M&E Technician • Chiller plant",
      status: "ON-SITE",
      statusType: "onsite",
      time: "6.4h today",
    },
    {
      initials: "SI",
      name: "Sana Iqbal",
      role: "HVAC Engineer • AHU room L7",
      status: "ON-SITE",
      statusType: "onsite",
      time: "6.1h today",
    },
    {
      initials: "MD",
      name: "Mohan D.",
      role: "Electrician • En route",
      status: "EN-ROUTE",
      statusType: "enroute",
      time: "—",
    },
    {
      initials: "LN",
      name: "Lakshmi N.",
      role: "Housekeeping Lead • Lobby + L2",
      status: "ON-SITE",
      statusType: "onsite",
      time: "6.7h today",
    },
    {
      initials: "AP",
      name: "Arjun P.",
      role: "Plumber • Off-site",
      status: "OFF-SITE",
      statusType: "offsite",
      time: "—",
    },
    {
      initials: "FR",
      name: "Fatima R.",
      role: "BMS Operator • Control room",
      status: "ON-SITE",
      statusType: "onsite",
      time: "7.6h today",
    },
  ];

  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-[10px] font-bold tracking-widest text-slate-500 uppercase font-mono">
            <span className="text-teal-400">Apple One — HQ Tower</span>
            <span>•</span>
            <span>Operations</span>
            <span>•</span>
            <span>Vendors</span>
          </div>
          <h1 className="text-2xl font-bold text-white tracking-tight mt-2">
            Vendor & field service
          </h1>
          <p className="text-sm font-medium text-slate-400 mt-2 max-w-xl leading-relaxed">
            Vendor performance scorecards and live field-crew status in one view — SLA adherence, response times and job dispatch across your service partners.
          </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">
            <BarChart2 className="w-4 h-4 text-slate-300" />
            <span>SLA report</span>
          </Button>

          <Button className="bg-teal-400 hover:bg-teal-300 text-slate-950 font-bold text-xs px-4 py-2 rounded-lg flex items-center gap-2 transition-colors">
            <Plus className="w-4 h-4 stroke-[3]" />
            <span>Dispatch job</span>
          </Button>
        </div>
      </div>

      {/* --- STAT CARDS --- */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        {/* Active vendors */}
        <StatCard
          variant="icon"
          title="Active vendors"
          value="6"
          icon={Star}
          badgeText="contracted"
          badgeClassName="text-slate-400 lowercase font-mono"
          iconContainerClassName="bg-cyan-950/40 border-cyan-800/30 text-cyan-400"
        />

        {/* Avg SLA adherence */}
        <StatCard
          variant="icon"
          title="Avg SLA adherence"
          value="93%"
          icon={ShieldCheck}
          badgeText="this month"
          badgeClassName="text-teal-400 lowercase font-mono font-medium"
          iconContainerClassName="bg-emerald-950/60 border-emerald-800/40 text-emerald-400"
        />

        {/* Open field jobs */}
        <StatCard
          variant="icon"
          title="Open field jobs"
          value="12"
          icon={Activity}
          badgeText="in progress"
          badgeClassName="text-cyan-400 lowercase font-mono"
          iconContainerClassName="bg-blue-950/50 border-blue-800/30 text-blue-400"
        />

        {/* Avg response time */}
        <StatCard
          variant="icon"
          title="Avg response time"
          value="2.7h"
          icon={Clock}
          badgeText="to site"
          badgeClassName="text-slate-400 lowercase font-mono"
          iconContainerClassName="bg-amber-950/40 border-amber-800/30 text-amber-500"
        />
      </div>

      {/* --- TABLES GRID (SIDE-BY-SIDE) --- */}
      <div className="grid grid-cols-1 xl:grid-cols-3 gap-6 items-start">
        
        {/* LEFT TABLE: VENDOR SCORECARD */}
        <div className="xl:col-span-2 bg-[#0b1329] border border-slate-800/80 rounded-lg overflow-hidden">
          <div className="flex items-center justify-between px-6 py-4 border-b border-slate-800/80">
            <h2 className="text-base font-semibold text-white">Vendor scorecard</h2>
            <span className="text-xs font-mono text-slate-500 lowercase">
              SLA • rating • jobs
            </span>
          </div>

          <div className="overflow-x-auto">
            <table className="w-full text-left text-sm">
              <thead>
                <tr className="border-b border-slate-800/60 text-[11px] font-bold uppercase tracking-wider text-slate-400">
                  <th className="py-3 px-6">Vendor</th>
                  <th className="py-3 px-4">Scope</th>
                  <th className="py-3 px-4">Rating</th>
                  <th className="py-3 px-4">SLA</th>
                  <th className="py-3 px-4">Response</th>
                  <th className="py-3 px-6">Open</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-800/40">
                {vendorData.map((row, index) => (
                  <tr key={index} className="hover:bg-slate-800/30 transition-colors">
                    <td className="py-4 px-6">
                      <div className="font-semibold text-slate-100">{row.name}</div>
                      <div className="text-xs font-mono text-slate-500 mt-0.5">{row.id}</div>
                    </td>
                    <td className="py-4 px-4 text-slate-300 font-medium whitespace-nowrap">
                      {row.scope}
                    </td>
                    <td className="py-4 px-4 font-medium text-slate-200 whitespace-nowrap">
                      <div className="flex items-center gap-1">
                        <span>{row.rating}</span>
                        <Star className="w-3.5 h-3.5 fill-amber-400 text-amber-400" />
                      </div>
                    </td>
                    <td className={`py-4 px-4 font-semibold whitespace-nowrap ${row.slaColor}`}>
                      {row.sla}
                    </td>
                    <td className="py-4 px-4 text-slate-300 font-medium whitespace-nowrap">
                      {row.response}
                    </td>
                    <td className="py-4 px-6 font-semibold text-blue-400 whitespace-nowrap">
                      {row.openJobs}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>

        {/* RIGHT TABLE: FIELD CREW • LIVE */}
        <div className="bg-[#0b1329] border border-slate-800/80 rounded-lg overflow-hidden">
          <div className="flex items-center justify-between px-6 py-4 border-b border-slate-800/80">
            <h2 className="text-base font-semibold text-white">Field crew • live</h2>
            <span className="text-xs font-mono text-slate-500 lowercase">
              4 on site
            </span>
          </div>

          <div className="p-4 space-y-3">
            {fieldCrewData.map((crew, index) => {
              return (
                <div
                  key={index}
                  className="flex items-center justify-between p-3 rounded-lg bg-[#0d1835]/50 hover:bg-slate-800/30 transition-colors border border-slate-800/40"
                >
                  <div className="flex items-center gap-3">
                    <div className="w-9 h-9 rounded-lg bg-teal-950/60 border border-teal-800/30 text-teal-400 text-xs font-bold flex items-center justify-center shrink-0">
                      {crew.initials}
                    </div>
                    <div>
                      <div className="font-semibold text-slate-100 text-sm">{crew.name}</div>
                      <div className="text-xs text-slate-400 mt-0.5">{crew.role}</div>
                    </div>
                  </div>

                  <div className="text-right shrink-0">
                    {crew.statusType === "onsite" && (
                      <span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-bold tracking-wider bg-emerald-950/60 text-emerald-400 border border-emerald-800/30">
                        <span className="w-1.5 h-1.5 rounded-full bg-emerald-400"></span>
                        {crew.status}
                      </span>
                    )}
                    {crew.statusType === "enroute" && (
                      <span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-bold tracking-wider bg-amber-950/60 text-amber-400 border border-amber-800/30">
                        <span className="w-1.5 h-1.5 rounded-full bg-amber-400"></span>
                        {crew.status}
                      </span>
                    )}
                    {crew.statusType === "offsite" && (
                      <span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-[10px] font-bold tracking-wider bg-slate-800/80 text-slate-400 border border-slate-700/50">
                        <span className="w-1.5 h-1.5 rounded-full bg-slate-500"></span>
                        {crew.status}
                      </span>
                    )}
                    <div className="text-[11px] font-mono text-slate-500 mt-1">
                      {crew.time}
                    </div>
                  </div>
                </div>
              );
            })}
          </div>
        </div>

      </div>

      <div className="mb-10 mt-10">
        <InfoBanner 
          boldText="Closes the loop:" 
          normalText="a breached SLA or new request dispatches a field job to the right crew, tracks response-to-site time, and feeds completion back into the vendor scorecard — the vendor-facing half of the help desk." 
        />
      </div>
    </div>
  );
}