import React from 'react';
import { X } from 'lucide-react';
import { Workflow } from '../services/workflowService';

interface WorkflowDetailModalProps {
  workflow: Workflow;
  onClose: () => void;
}

const WorkflowDetailModal: React.FC<WorkflowDetailModalProps> = ({ workflow, onClose }) => {
  return (
    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
      <div className="bg-white rounded-xl shadow-2xl max-w-3xl w-full max-h-[90vh] overflow-y-auto">
        <div className="sticky top-0 bg-white border-b border-gray-200 p-6 flex items-center justify-between">
          <h2 className="text-2xl font-bold text-gray-900">{workflow.name}</h2>
          <button
            onClick={onClose}
            className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        <div className="p-6 space-y-6">
          {workflow.description && (
            <div>
              <h3 className="text-sm font-semibold text-gray-700 mb-2">Description</h3>
              <p className="text-gray-600">{workflow.description}</p>
            </div>
          )}

          <div className="grid grid-cols-2 gap-4">
            <div>
              <p className="text-xs text-gray-500 mb-1">Type</p>
              <p className="text-sm font-medium text-gray-900">{workflow.workflow_type.replace('_', ' ')}</p>
            </div>
            <div>
              <p className="text-xs text-gray-500 mb-1">Trigger</p>
              <p className="text-sm font-medium text-gray-900">{workflow.trigger.replace('_', ' ')}</p>
            </div>
            <div>
              <p className="text-xs text-gray-500 mb-1">Status</p>
              <p className="text-sm font-medium text-gray-900">{workflow.status}</p>
            </div>
            {workflow.sla_hours && (
              <div>
                <p className="text-xs text-gray-500 mb-1">SLA</p>
                <p className="text-sm font-medium text-gray-900">{workflow.sla_hours} hours</p>
              </div>
            )}
          </div>

          <div>
            <h3 className="text-sm font-semibold text-gray-700 mb-4">Workflow Steps ({workflow.steps.length})</h3>
            <div className="space-y-3">
              {workflow.steps.map((step, index) => (
                <div key={index} className="bg-gray-50 rounded-lg p-4 border border-gray-200">
                  <div className="flex items-center gap-3 mb-2">
                    <div className="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center">
                      <span className="text-sm font-semibold text-indigo-600">{index + 1}</span>
                    </div>
                    <div className="flex-1">
                      <p className="font-semibold text-gray-900">{step.title}</p>
                      {step.description && (
                        <p className="text-sm text-gray-600 mt-1">{step.description}</p>
                      )}
                    </div>
                  </div>
                  <div className="flex items-center gap-4 text-xs text-gray-500 ml-11">
                    <span>Type: {step.task_type.replace('_', ' ')}</span>
                    <span>Priority: {step.priority}</span>
                    {step.estimated_duration_minutes && (
                      <span>Duration: {step.estimated_duration_minutes} min</span>
                    )}
                  </div>
                </div>
              ))}
            </div>
          </div>

          <div className="flex items-center justify-end gap-3 pt-4 border-t border-gray-200">
            <button
              onClick={onClose}
              className="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300"
            >
              Close
            </button>
          </div>
        </div>
      </div>
    </div>
  );
};

export default WorkflowDetailModal;

