'use client';

import { Card } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Progress } from '@/components/ui/progress';
import { Button } from '@/components/ui/button';
import { WBSKPIData } from '@/lib/types';
import { getProjectWBSKPIs } from '@/lib/mock-data';
import { 
  BarChart3, 
  CheckCircle2, 
  Circle, 
  AlertCircle, 
  Pause,
  Clock,
  TrendingUp,
  Users,
  Target,
  Calendar,
  ArrowRight
} from 'lucide-react';
import Link from 'next/link';
import { formatDate as formatDateUtil } from '@/lib/date-utils';

interface WBSProgressWidgetProps {
  projectId: string;
  variant?: 'card' | 'detailed';
  showActions?: boolean;
}

export function WBSProgressWidget({ 
  projectId, 
  variant = 'card', 
  showActions = false 
}: WBSProgressWidgetProps) {
  const wbsKPIs = getProjectWBSKPIs(projectId);

  if (wbsKPIs.totalTasks === 0) {
    return null;
  }

  const getProgressColor = (progress: number) => {
    if (progress >= 80) return 'bg-green-500';
    if (progress >= 60) return 'bg-primary/100';
    if (progress >= 40) return 'bg-yellow-500';
    return 'bg-red-500';
  };

  const getProgressTextColor = (progress: number) => {
    if (progress >= 80) return 'text-green-600';
    if (progress >= 60) return 'text-primary';
    if (progress >= 40) return 'text-yellow-600';
    return 'text-red-600';
  };

  const completionRate = wbsKPIs.totalEstimatedHours > 0 
    ? Math.round((wbsKPIs.totalActualHours / wbsKPIs.totalEstimatedHours) * 100)
    : 0;

  const efficiency = wbsKPIs.totalEstimatedHours > 0 && wbsKPIs.totalActualHours > 0
    ? wbsKPIs.totalActualHours <= wbsKPIs.totalEstimatedHours ? 'On Track' : 'Over Budget'
    : 'N/A';

  if (variant === 'card') {
    return (
      <div className="p-4 bg-gradient-to-br from-primary/10 to-primary/5 rounded-lg border border-primary/20">
        <div className="flex items-center justify-between mb-3">
          <div className="flex items-center space-x-2">
            <div className="p-1.5 bg-primary/15 rounded-lg">
              <BarChart3 className="h-4 w-4 text-primary" />
            </div>
            <span className="text-sm font-semibold text-foreground">WBS Progress</span>
          </div>
          <div className={`text-lg font-bold ${getProgressTextColor(wbsKPIs.overallProgress)}`}>
            {wbsKPIs.overallProgress}%
          </div>
        </div>
        
        <div className="mb-3">
          <Progress 
            value={wbsKPIs.overallProgress} 
            className="h-2.5"
          />
        </div>
        
        <div className="grid grid-cols-4 gap-2 mb-3">
          <div className="text-center">
            <div className="flex items-center justify-center space-x-1 mb-1">
              <CheckCircle2 className="h-3 w-3 text-green-500" />
              <span className="text-xs font-medium text-foreground">{wbsKPIs.completedTasks}</span>
            </div>
            <span className="text-xs text-muted-foreground">Done</span>
          </div>
          <div className="text-center">
            <div className="flex items-center justify-center space-x-1 mb-1">
              <AlertCircle className="h-3 w-3 text-primary" />
              <span className="text-xs font-medium text-foreground">{wbsKPIs.inProgressTasks}</span>
            </div>
            <span className="text-xs text-muted-foreground">Active</span>
          </div>
          <div className="text-center">
            <div className="flex items-center justify-center space-x-1 mb-1">
              <Circle className="h-3 w-3 text-muted-foreground" />
              <span className="text-xs font-medium text-foreground">{wbsKPIs.notStartedTasks}</span>
            </div>
            <span className="text-xs text-muted-foreground">Pending</span>
          </div>
          <div className="text-center">
            <div className="flex items-center justify-center space-x-1 mb-1">
              <Target className="h-3 w-3 text-primary" />
              <span className="text-xs font-medium text-foreground">{wbsKPIs.totalTasks}</span>
            </div>
            <span className="text-xs text-muted-foreground">Total</span>
          </div>
        </div>
        
        {wbsKPIs.totalEstimatedHours > 0 && (
          <div className="flex items-center justify-between text-xs">
            <div className="flex items-center space-x-1 text-muted-foreground">
              <Clock className="h-3 w-3" />
              <span>{wbsKPIs.totalActualHours}h / {wbsKPIs.totalEstimatedHours}h</span>
            </div>
            <Badge 
              variant="outline" 
              className={`text-xs ${efficiency === 'On Track' ? 'bg-green-50 text-green-700 border-green-200' : 'bg-red-50 text-red-700 border-red-200'}`}
            >
              {efficiency}
            </Badge>
          </div>
        )}
      </div>
    );
  }

  return (
    <Card className="p-6">
      <div className="flex items-center justify-between mb-6">
        <div className="flex items-center space-x-3">
          <div className="p-2 bg-primary/10 rounded-lg">
            <BarChart3 className="h-6 w-6 text-primary" />
          </div>
          <div>
            <h3 className="text-lg font-semibold text-foreground">Work Breakdown Structure</h3>
            <p className="text-sm text-muted-foreground">Project progress and task breakdown</p>
          </div>
        </div>
        {showActions && (
          <div className="flex items-center space-x-2">
            <Link href={`/dashboard/projects/${projectId}/wbs`}>
              <Button variant="outline" size="sm">
                <BarChart3 className="h-4 w-4 mr-2" />
                View WBS
              </Button>
            </Link>
          </div>
        )}
      </div>

      {/* Overall Progress */}
      <div className="mb-6">
        <div className="flex items-center justify-between mb-2">
          <span className="text-sm font-medium text-foreground/80">Overall Progress</span>
          <span className={`text-2xl font-bold ${getProgressTextColor(wbsKPIs.overallProgress)}`}>
            {wbsKPIs.overallProgress}%
          </span>
        </div>
        <Progress 
          value={wbsKPIs.overallProgress} 
          className="h-3"
        />
      </div>

      {/* Task Status Grid */}
      <div className="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        <div className="p-4 bg-green-50 rounded-lg border border-green-200">
          <div className="flex items-center space-x-3">
            <CheckCircle2 className="h-8 w-8 text-green-500" />
            <div>
              <div className="text-2xl font-bold text-green-700">{wbsKPIs.completedTasks}</div>
              <div className="text-sm text-green-600">Completed</div>
            </div>
          </div>
        </div>

        <div className="p-4 bg-primary/10 rounded-lg border border-primary/20">
          <div className="flex items-center space-x-3">
            <AlertCircle className="h-8 w-8 text-primary" />
            <div>
              <div className="text-2xl font-bold text-primary">{wbsKPIs.inProgressTasks}</div>
              <div className="text-sm text-primary">In Progress</div>
            </div>
          </div>
        </div>

        <div className="p-4 bg-muted/50 rounded-lg border border-border">
          <div className="flex items-center space-x-3">
            <Circle className="h-8 w-8 text-muted-foreground" />
            <div>
              <div className="text-2xl font-bold text-foreground/80">{wbsKPIs.notStartedTasks}</div>
              <div className="text-sm text-muted-foreground">Not Started</div>
            </div>
          </div>
        </div>

        <div className="p-4 bg-primary/5 rounded-lg border border-primary/20">
          <div className="flex items-center space-x-3">
            <Target className="h-8 w-8 text-primary" />
            <div>
              <div className="text-2xl font-bold text-primary">{wbsKPIs.totalTasks}</div>
              <div className="text-sm text-primary">Total Tasks</div>
            </div>
          </div>
        </div>
      </div>

      {/* Time Tracking */}
      {wbsKPIs.totalEstimatedHours > 0 && (
        <div className="grid grid-cols-1 lg:grid-cols-3 gap-4 mb-6">
          <div className="p-4 bg-primary/5 rounded-lg border border-primary/20">
            <div className="flex items-center space-x-3">
              <Clock className="h-6 w-6 text-primary" />
              <div>
                <div className="text-lg font-bold text-primary">{wbsKPIs.totalEstimatedHours}h</div>
                <div className="text-sm text-muted-foreground">Estimated</div>
              </div>
            </div>
          </div>

          <div className="p-4 bg-orange-50 rounded-lg border border-orange-200">
            <div className="flex items-center space-x-3">
              <TrendingUp className="h-6 w-6 text-orange-500" />
              <div>
                <div className="text-lg font-bold text-orange-700">{wbsKPIs.totalActualHours}h</div>
                <div className="text-sm text-orange-600">Actual</div>
              </div>
            </div>
          </div>

          <div className="p-4 bg-teal-50 rounded-lg border border-teal-200">
            <div className="flex items-center space-x-3">
              <Users className="h-6 w-6 text-teal-500" />
              <div>
                <div className="text-lg font-bold text-teal-700">{completionRate}%</div>
                <div className="text-sm text-teal-600">Time Efficiency</div>
              </div>
            </div>
          </div>
        </div>
      )}

      {/* Progress Insights */}
      <div className="p-4 bg-muted/50 rounded-lg">
        <h4 className="text-sm font-semibold text-foreground mb-3">Progress Insights</h4>
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-4 text-sm">
          <div className="flex items-center space-x-2">
            <div className="w-2 h-2 bg-green-500 rounded-full"></div>
            <span className="text-foreground/80">
              <strong>{Math.round((wbsKPIs.completedTasks / wbsKPIs.totalTasks) * 100)}%</strong> of tasks completed
            </span>
          </div>
          <div className="flex items-center space-x-2">
            <div className="w-2 h-2 bg-primary/100 rounded-full"></div>
            <span className="text-foreground/80">
              <strong>{wbsKPIs.inProgressTasks}</strong> tasks currently active
            </span>
          </div>
          {wbsKPIs.blockedTasks > 0 && (
            <div className="flex items-center space-x-2">
              <div className="w-2 h-2 bg-red-500 rounded-full"></div>
              <span className="text-foreground/80">
                <strong>{wbsKPIs.blockedTasks}</strong> tasks blocked - needs attention
              </span>
            </div>
          )}
          {wbsKPIs.totalEstimatedHours > 0 && (
            <div className="flex items-center space-x-2">
              <div className="w-2 h-2 bg-primary/50 rounded-full"></div>
              <span className="text-foreground/80">
                <strong>{efficiency}</strong> - Time tracking status
              </span>
            </div>
          )}
        </div>
      </div>

      {showActions && (
        <div className="mt-6 pt-4 border-t border-border">
          <div className="flex items-center justify-between">
            <span className="text-sm text-muted-foreground">
                              Last updated: {formatDateUtil(new Date(), { format: 'short' })}
            </span>
            <Link href={`/dashboard/projects/${projectId}/wbs`}>
              <Button variant="outline" size="sm">
                Manage Tasks
                <ArrowRight className="h-4 w-4 ml-2" />
              </Button>
            </Link>
          </div>
        </div>
      )}
    </Card>
  );
}