'use client';

import React from 'react';
import { Building2, FileClock, Map, Layers, TrendingUp, PlusCircle } from 'lucide-react';
import Link from 'next/link';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { useDashboardMetrics } from '@/services/superAdmin/queries';

interface Props {
  userContext: any;
}

export default function BdmView({ userContext }: Props) {
  const { data: metrics, isLoading, isError } = useDashboardMetrics();

  if (isLoading) {
    return (
      <div className="p-8 space-y-4 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 animate-pulse max-w-7xl mx-auto">
        {[...Array(4)].map((_, i) => (
          <div key={i} className="h-24 bg-muted/20 rounded-xl border border-border/10" />
        ))}
      </div>
    );
  }

  if (isError || !metrics) {
    return (
      <div className="p-8 text-center text-xs font-semibold text-destructive border border-dashed border-destructive/20 rounded-xl bg-destructive/5 max-w-7xl mx-auto">
        Could not load Business Development metrics dashboard.
      </div>
    );
  }

  return (
    <div className="space-y-8 max-w-7xl mx-auto animate-in fade-in slide-in-from-bottom-2 duration-500 select-none">
      {/* HEADER */}
      <div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 border-b border-border/40 pb-6">
        <div className="space-y-0.5">
          <h1 className="text-sm font-black tracking-widest text-foreground uppercase">
            BDM EXPANSION OPERATIONS
          </h1>
          <p className="text-[11px] font-medium text-muted-foreground/70">
            Institutional Onboarding, Sector Mapping, & Network Growth Management.
          </p>
        </div>
        <div className="flex gap-2">
          <Link href="/institutes/new">
            <Button className="h-8 rounded-md px-3 text-[11px] font-bold tracking-wider uppercase shadow-sm gap-1.5 bg-primary text-primary-foreground cursor-pointer">
              <PlusCircle className="h-3.5 w-3.5" />
              <span>Provision Institute</span>
            </Button>
          </Link>
        </div>
      </div>

      {/* METRICS GRID */}
      <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
        {[
          { title: 'Total Institutes', value: metrics.widgets?.totalInstitutes || 0, sub: 'Onboarded Campus Nodes', icon: Building2, highlight: true },
          { title: 'Institute Approvals Pending', value: metrics.widgets?.pendingInstituteKyc || 0, sub: 'Agreement Approval Queue', icon: FileClock, danger: true },
          { title: 'Active Districts', value: metrics.widgets?.totalDistricts || 0, sub: 'Regional Footprint', icon: Map },
          { title: 'Active Sectors', value: metrics.widgets?.totalSectors || 0, sub: 'Business Scope Categories', icon: Layers },
        ].map((w, idx) => (
          <Card key={idx} className={`shadow-none rounded-xl border border-border/40 bg-surface backdrop-blur-md transition-all hover:border-border/80 ${w.highlight ? 'border-l-primary border-l-2' : ''}`}>
            <CardHeader className="flex flex-row items-center justify-between pb-1.5 space-y-0 pt-4 px-4">
              <CardTitle className="text-[10px] font-bold uppercase tracking-wider text-muted-foreground/80">{w.title}</CardTitle>
              <w.icon className={`h-3.5 w-3.5 ${w.danger ? 'text-destructive' : 'text-muted-foreground/60'}`} />
            </CardHeader>
            <CardContent className="pb-4 px-4">
              <div className={`text-xl font-bold tracking-tight ${w.danger ? 'text-destructive' : 'text-foreground'}`}>
                {w.value}
              </div>
              <p className="text-[10px] font-medium text-muted-foreground/50 mt-0.5">{w.sub}</p>
            </CardContent>
          </Card>
        ))}
      </div>
    </div>
  );
}