'use client';

import React from 'react';
import { CreditCard, Building2, UserCheck, GraduationCap, Download } from 'lucide-react';
import toast from 'react-hot-toast';
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 AccountsView({ 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-3 gap-4 animate-pulse max-w-7xl mx-auto">
        {[...Array(3)].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 Accounts dashboard telemetry.
      </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">
            FINANCIAL & ACCOUNTS DESK
          </h1>
          <p className="text-[11px] font-medium text-muted-foreground/70">
            Institutional Billing Contexts, Certified Advisors, and Active Enrollment Financial Units.
          </p>
        </div>
        <Button
          onClick={() => toast.success('Exporting accounts ledger...')}
          variant="outline"
          className="h-8 rounded-md px-3 text-[11px] font-bold tracking-wider uppercase shadow-sm gap-1.5 border-border/80 hover:bg-muted/60 text-foreground cursor-pointer"
        >
          <Download className="h-3 w-3" />
          <span>Export Ledger</span>
        </Button>
      </div>

      {/* SUMMARY CARDS */}
      <div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
        {[
          { title: 'Billing Institutes', value: metrics.widgets?.totalInstitutes || 0, sub: 'Active Billing Campus Units', icon: Building2 },
          { title: 'Certified Advisors Pool', value: metrics.widgets?.certifiedAdvisors || 0, sub: 'Commission-Eligible Advisors', icon: UserCheck, success: true },
          { title: 'Active Billable Enrollments', value: metrics.widgets?.totalStudentsEnrolled || 0, sub: 'Revenue Generating Students', icon: GraduationCap, 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' : 'text-muted-foreground/60'}`} />
            </CardHeader>
            <CardContent className="pb-4 px-4">
              <div className={`text-xl font-bold tracking-tight ${w.success ? 'text-emerald-600' : '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>
  );
}