import React from "react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { AlertTriangle, UserMinus, TrendingDown, CheckCircle } from "lucide-react";
import { Alert, AlertDescription } from "@/components/ui/alert";

export default function OrderReductionAlert({ 
  originalRequested, 
  newRequested, 
  currentAssigned,
  onAutoUnassign,
  onManualUnassign,
  lowReliabilityStaff = []
}) {
  const excessStaff = currentAssigned - newRequested;
  
  if (excessStaff <= 0) return null;

  return (
    <Card className="border-2 border-orange-500 bg-orange-50 shadow-lg">
      <CardHeader className="bg-gradient-to-r from-orange-100 to-red-50 border-b border-orange-200">
        <div className="flex items-center gap-3">
          <div className="w-12 h-12 bg-orange-500 rounded-xl flex items-center justify-center">
            <AlertTriangle className="w-6 h-6 text-white" />
          </div>
          <div>
            <CardTitle className="text-xl font-bold text-orange-900">
              Order Size Reduction Detected
            </CardTitle>
            <p className="text-sm text-orange-700 mt-1">
              Client reduced headcount from {originalRequested} to {newRequested}
            </p>
          </div>
        </div>
      </CardHeader>
      
      <CardContent className="p-6 space-y-4">
        <Alert className="bg-white border-orange-300">
          <AlertDescription className="text-slate-900">
            <div className="flex items-center gap-2 mb-2">
              <TrendingDown className="w-5 h-5 text-orange-600" />
              <span className="font-bold">Action Required:</span>
            </div>
            <p className="text-sm">
              You have <strong className="text-orange-700">{excessStaff} staff member{excessStaff !== 1 ? 's' : ''}</strong> assigned 
              that exceed{excessStaff === 1 ? 's' : ''} the new request. 
              You must unassign {excessStaff} worker{excessStaff !== 1 ? 's' : ''} to match the new headcount.
            </p>
          </AlertDescription>
        </Alert>

        <div className="grid grid-cols-3 gap-4">
          <div className="bg-white border-2 border-slate-200 rounded-xl p-4 text-center">
            <p className="text-xs text-slate-500 mb-1">Original Request</p>
            <p className="text-2xl font-bold text-slate-900">{originalRequested}</p>
          </div>
          <div className="bg-white border-2 border-orange-300 rounded-xl p-4 text-center">
            <p className="text-xs text-orange-600 mb-1">New Request</p>
            <p className="text-2xl font-bold text-orange-700">{newRequested}</p>
          </div>
          <div className="bg-white border-2 border-red-300 rounded-xl p-4 text-center">
            <p className="text-xs text-red-600 mb-1">Must Remove</p>
            <p className="text-2xl font-bold text-red-700">{excessStaff}</p>
          </div>
        </div>

        <div className="space-y-3">
          <Button
            onClick={onManualUnassign}
            variant="outline"
            className="w-full border-2 border-slate-300 hover:bg-slate-50"
          >
            <UserMinus className="w-4 h-4 mr-2" />
            Manually Select Which Staff to Remove
          </Button>

          {lowReliabilityStaff.length > 0 && (
            <Button
              onClick={onAutoUnassign}
              className="w-full bg-orange-600 hover:bg-orange-700 text-white"
            >
              <CheckCircle className="w-4 h-4 mr-2" />
              Auto-Remove {excessStaff} Lowest Reliability Staff
            </Button>
          )}
        </div>

        {lowReliabilityStaff.length > 0 && (
          <div className="bg-white border border-orange-200 rounded-lg p-4">
            <p className="text-xs font-bold text-slate-700 mb-3 uppercase">
              Suggested for Auto-Removal (Lowest Reliability):
            </p>
            <div className="space-y-2">
              {lowReliabilityStaff.slice(0, excessStaff).map((staff, idx) => (
                <div key={idx} className="flex items-center justify-between p-2 bg-red-50 rounded-lg border border-red-200">
                  <span className="text-sm font-medium text-slate-900">{staff.name}</span>
                  <Badge variant="outline" className="border-red-400 text-red-700">
                    Reliability: {staff.reliability}%
                  </Badge>
                </div>
              ))}
            </div>
          </div>
        )}
      </CardContent>
    </Card>
  );
}