'use client';

import React from 'react';
import { Users, UserCheck, ShieldAlert, UserPlus, PieChart } from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { useDashboardMetrics } from '@/services/superAdmin/queries';

interface Props {
  userContext: any;
}

export default function HrView({ 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 retrieve HR telemetry matrix.
      </div>
    );
  }

  const usersByRole = metrics.charts?.usersByRole || [];

  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">
            HR HUMAN CAPITAL DASHBOARD
          </h1>
          <p className="text-[11px] font-medium text-muted-foreground/70">
            System Identity Provisioning, Active Personnel, and Verification Pipeline Metrics.
          </p>
        </div>
      </div>

      {/* SUMMARY WIDGETS */}
      <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
        {[
          { title: 'Total Registered Users', value: metrics.widgets?.totalUsers || 0, sub: 'All Enterprise Accounts', icon: Users },
          { title: 'Active Accounts', value: metrics.widgets?.activeUsers || 0, sub: 'Validated System Access', icon: UserCheck, success: true },
          { title: 'Pending Personal KYC', value: metrics.widgets?.pendingKyc || 0, sub: 'Awaiting Document Approval', icon: ShieldAlert, danger: true },
          { title: '7-Day Onboarding', value: metrics.widgets?.recentRegistrations || 0, sub: 'New Account Influx', icon: UserPlus, highlight: true },
        ].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.success ? 'text-emerald-500' : w.danger ? 'text-destructive' : 'text-muted-foreground/60'}`} />
            </CardHeader>
            <CardContent className="pb-4 px-4">
              <div className={`text-xl font-bold tracking-tight ${w.success ? 'text-emerald-600' : 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>

      {/* USER ROLE DISTRIBUTION BREAKDOWN */}
      <Card className="shadow-none rounded-xl border border-border/40 bg-surface">
        <CardHeader className="pb-3 border-b border-border/10">
          <CardTitle className="text-xs font-bold uppercase tracking-wider text-foreground flex items-center gap-2">
            <PieChart className="h-4 w-4 text-primary" /> Active Workforce Breakdown by Role
          </CardTitle>
        </CardHeader>
        <CardContent className="p-4 grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-3">
          {usersByRole.map((r: any) => (
            <div key={r.role} className="p-3 border border-border/30 rounded-lg bg-muted/10 space-y-1">
              <span className="text-[9px] font-extrabold uppercase text-muted-foreground block truncate">
                {r.role.replace(/_/g, ' ')}
              </span>
              <span className="text-lg font-black text-foreground">{r.count}</span>
            </div>
          ))}
        </CardContent>
      </Card>
    </div>
  );
}