'use client';

import { Button } from './ui/button';
import { StatusBadge } from './Badge';
import { getContrastColor } from '../../lib/colorUtils';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  DropdownMenuSeparator,
} from './ui/dropdown-menu';

interface InstalledScript {
  id: number;
  script_name: string;
  script_path: string;
  container_id: string | null;
  server_id: number | null;
  server_name: string | null;
  server_ip: string | null;
  server_user: string | null;
  server_password: string | null;
  server_auth_type: string | null;
  server_ssh_key: string | null;
  server_ssh_key_passphrase: string | null;
  server_ssh_port: number | null;
  server_color: string | null;
  installation_date: string;
  status: 'in_progress' | 'success' | 'failed';
  output_log: string | null;
  execution_mode: 'local' | 'ssh';
  container_status?: 'running' | 'stopped' | 'unknown';
  web_ui_ip: string | null;
  web_ui_port: number | null;
}

interface ScriptInstallationCardProps {
  script: InstalledScript;
  isEditing: boolean;
  editFormData: { script_name: string; container_id: string; web_ui_ip: string; web_ui_port: string };
  onInputChange: (field: 'script_name' | 'container_id' | 'web_ui_ip' | 'web_ui_port', value: string) => void;
  onEdit: () => void;
  onSave: () => void;
  onCancel: () => void;
  onUpdate: () => void;
  onShell: () => void;
  onDelete: () => void;
  isUpdating: boolean;
  isDeleting: boolean;
  // New container control props
  containerStatus?: 'running' | 'stopped' | 'unknown';
  onStartStop: (action: 'start' | 'stop') => void;
  onDestroy: () => void;
  isControlling: boolean;
  // Web UI props
  onOpenWebUI: () => void;
  onAutoDetectWebUI: () => void;
  isAutoDetecting: boolean;
}

export function ScriptInstallationCard({
  script,
  isEditing,
  editFormData,
  onInputChange,
  onEdit,
  onSave,
  onCancel,
  onUpdate,
  onShell,
  onDelete,
  isUpdating,
  isDeleting,
  containerStatus,
  onStartStop,
  onDestroy,
  isControlling,
  onOpenWebUI,
  onAutoDetectWebUI,
  isAutoDetecting
}: ScriptInstallationCardProps) {
  const formatDate = (dateString: string) => {
    return new Date(dateString).toLocaleString();
  };

  // Helper function to check if a script has any actions available
  const hasActions = (script: InstalledScript) => {
    if (script.container_id && script.execution_mode === 'ssh') return true;
    if (script.web_ui_ip != null) return true;
    if (!script.container_id || script.execution_mode !== 'ssh') return true;
    return false;
  };

  return (
    <div 
      className="bg-card border border-border rounded-lg p-4 shadow-sm hover:shadow-md transition-shadow"
      style={{ borderLeft: `4px solid ${script.server_color ?? 'transparent'}` }}
    >
      {/* Header with Script Name and Status */}
      <div className="flex items-start justify-between mb-3">
        <div className="flex-1 min-w-0">
          {isEditing ? (
            <div className="space-y-2">
              <input
                type="text"
                value={editFormData.script_name}
                onChange={(e) => onInputChange('script_name', e.target.value)}
                className="w-full px-2 py-1 text-sm border border-border rounded bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-primary"
                placeholder="Script name"
              />
              <div className="text-xs text-muted-foreground">{script.script_path}</div>
            </div>
          ) : (
            <div>
              <div className="text-sm font-medium text-foreground truncate">{script.script_name}</div>
              <div className="text-xs text-muted-foreground truncate">{script.script_path}</div>
            </div>
          )}
        </div>
        <div className="ml-2 flex-shrink-0">
          <StatusBadge status={script.status}>
            {script.status.replace('_', ' ').toUpperCase()}
          </StatusBadge>
        </div>
      </div>

      {/* Details Grid */}
      <div className="grid grid-cols-1 gap-3 mb-4">
        {/* Container ID */}
        <div>
          <div className="text-xs font-medium text-muted-foreground mb-1">Container ID</div>
          {isEditing ? (
            <input
              type="text"
              value={editFormData.container_id}
              onChange={(e) => onInputChange('container_id', e.target.value)}
              className="w-full px-2 py-1 text-sm font-mono border border-border rounded bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-primary"
              placeholder="Container ID"
            />
          ) : (
            <div className="text-sm font-mono text-foreground break-all">
              {script.container_id ? (
                <div className="flex items-center space-x-2">
                  <span>{script.container_id}</span>
                  {script.container_status && (
                    <div className="flex items-center space-x-1">
                      <div className={`w-2 h-2 rounded-full ${
                        script.container_status === 'running' ? 'bg-green-500' : 
                        script.container_status === 'stopped' ? 'bg-red-500' : 
                        'bg-gray-400'
                      }`}></div>
                      <span className={`text-xs font-medium ${
                        script.container_status === 'running' ? 'text-green-700 dark:text-green-300' : 
                        script.container_status === 'stopped' ? 'text-red-700 dark:text-red-300' : 
                        'text-gray-500 dark:text-gray-400'
                      }`}>
                        {script.container_status === 'running' ? 'Running' : 
                         script.container_status === 'stopped' ? 'Stopped' : 
                         'Unknown'}
                      </span>
                    </div>
                  )}
                </div>
              ) : '-'}
            </div>
          )}
        </div>

        {/* Web UI */}
        <div>
          <div className="text-xs font-medium text-muted-foreground mb-1">IP:PORT</div>
          {isEditing ? (
            <div className="flex items-center space-x-2">
              <input
                type="text"
                value={editFormData.web_ui_ip}
                onChange={(e) => onInputChange('web_ui_ip', e.target.value)}
                className="flex-1 px-2 py-1 text-sm font-mono border border-border rounded bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-primary"
                placeholder="IP"
              />
              <span className="text-muted-foreground">:</span>
              <input
                type="number"
                value={editFormData.web_ui_port}
                onChange={(e) => onInputChange('web_ui_port', e.target.value)}
                className="w-20 px-2 py-1 text-sm font-mono border border-border rounded bg-background text-foreground focus:outline-none focus:ring-2 focus:ring-primary"
                placeholder="Port"
              />
            </div>
          ) : (
            <div className="text-sm font-mono text-foreground">
              {script.web_ui_ip ? (
                <div className="flex items-center justify-between w-full">
                  <button
                    onClick={onOpenWebUI}
                    disabled={containerStatus === 'stopped'}
                    className={`text-blue-600 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-300 hover:underline flex-shrink-0 ${
                      containerStatus === 'stopped' ? 'opacity-50 cursor-not-allowed' : ''
                    }`}
                  >
                    {script.web_ui_ip}:{script.web_ui_port ?? 80}
                  </button>
                  {script.container_id && script.execution_mode === 'ssh' && (
                    <button
                      onClick={onAutoDetectWebUI}
                      disabled={isAutoDetecting}
                      className="text-xs px-2 py-1 bg-blue-900 hover:bg-blue-800 text-blue-300 border border-blue-700 rounded disabled:opacity-50 transition-colors flex-shrink-0 ml-2"
                      title="Re-detect IP and port"
                    >
                      {isAutoDetecting ? '...' : 'Re-detect'}
                    </button>
                  )}
                </div>
              ) : (
                <div className="flex items-center space-x-2">
                  <span className="text-muted-foreground">-</span>
                  {script.container_id && script.execution_mode === 'ssh' && (
                    <button
                      onClick={onAutoDetectWebUI}
                      disabled={isAutoDetecting}
                      className="text-xs px-2 py-1 bg-blue-900 hover:bg-blue-800 text-blue-300 border border-blue-700 rounded disabled:opacity-50 transition-colors"
                      title="Re-detect IP and port"
                    >
                      {isAutoDetecting ? '...' : 'Re-detect'}
                    </button>
                  )}
                </div>
              )}
            </div>
          )}
        </div>

        {/* Server */}
        <div>
          <div className="text-xs font-medium text-muted-foreground mb-1">Server</div>
          <span 
            className="text-sm px-3 py-1 rounded inline-block"
            style={{
              backgroundColor: script.server_color ?? 'transparent',
              color: script.server_color ? getContrastColor(script.server_color) : 'inherit'
            }}
          >
            {script.server_name ?? '-'}
          </span>
        </div>

        {/* Installation Date */}
        <div>
          <div className="text-xs font-medium text-muted-foreground mb-1">Installation Date</div>
          <div className="text-sm text-muted-foreground">
            {formatDate(String(script.installation_date))}
          </div>
        </div>
      </div>

      {/* Action Buttons */}
      <div className="flex flex-wrap gap-2">
        {isEditing ? (
          <>
            <Button
              onClick={onSave}
              disabled={isUpdating}
              variant="save"
              size="sm"
              className="flex-1 min-w-0"
            >
              {isUpdating ? 'Saving...' : 'Save'}
            </Button>
            <Button
              onClick={onCancel}
              variant="cancel"
              size="sm"
              className="flex-1 min-w-0"
            >
              Cancel
            </Button>
          </>
        ) : (
          <>
            <Button
              onClick={onEdit}
              variant="edit"
              size="sm"
              className="flex-1 min-w-0"
            >
              Edit
            </Button>
            {hasActions(script) && (
              <DropdownMenu>
                <DropdownMenuTrigger asChild>
                  <Button
                    variant="outline"
                    size="sm"
                    className="flex-1 min-w-0 bg-gray-800/20 hover:bg-gray-800/30 border border-gray-600/50 text-gray-300 hover:text-gray-200 hover:border-gray-500/60 transition-all duration-200 hover:scale-105 hover:shadow-md"
                  >
                    Actions
                  </Button>
                </DropdownMenuTrigger>
                <DropdownMenuContent className="w-48 bg-gray-900 border-gray-700">
                  {script.container_id && (
                    <DropdownMenuItem
                      onClick={onUpdate}
                      disabled={containerStatus === 'stopped'}
                      className="text-cyan-300 hover:text-cyan-200 hover:bg-cyan-900/20 focus:bg-cyan-900/20"
                    >
                      Update
                    </DropdownMenuItem>
                  )}
                  {script.container_id && script.execution_mode === 'ssh' && (
                    <DropdownMenuItem
                      onClick={onShell}
                      disabled={containerStatus === 'stopped'}
                      className="text-gray-300 hover:text-gray-200 hover:bg-gray-800/20 focus:bg-gray-800/20"
                    >
                      Shell
                    </DropdownMenuItem>
                  )}
                  {script.web_ui_ip && (
                    <DropdownMenuItem
                      onClick={onOpenWebUI}
                      disabled={containerStatus === 'stopped'}
                      className="text-blue-300 hover:text-blue-200 hover:bg-blue-900/20 focus:bg-blue-900/20"
                    >
                      Open UI
                    </DropdownMenuItem>
                  )}
                  {script.container_id && script.execution_mode === 'ssh' && (
                    <>
                      <DropdownMenuSeparator className="bg-gray-700" />
                      <DropdownMenuItem
                        onClick={() => onStartStop(containerStatus === 'running' ? 'stop' : 'start')}
                        disabled={isControlling || containerStatus === 'unknown'}
                        className={containerStatus === 'running' 
                          ? "text-red-300 hover:text-red-200 hover:bg-red-900/20 focus:bg-red-900/20"
                          : "text-green-300 hover:text-green-200 hover:bg-green-900/20 focus:bg-green-900/20"
                        }
                      >
                        {isControlling ? 'Working...' : containerStatus === 'running' ? 'Stop' : 'Start'}
                      </DropdownMenuItem>
                      <DropdownMenuItem
                        onClick={onDestroy}
                        disabled={isControlling}
                        className="text-red-300 hover:text-red-200 hover:bg-red-900/20 focus:bg-red-900/20"
                      >
                        {isControlling ? 'Working...' : 'Destroy'}
                      </DropdownMenuItem>
                    </>
                  )}
                  {(!script.container_id || script.execution_mode !== 'ssh') && (
                    <>
                      <DropdownMenuSeparator className="bg-gray-700" />
                      <DropdownMenuItem
                        onClick={onDelete}
                        disabled={isDeleting}
                        className="text-red-300 hover:text-red-200 hover:bg-red-900/20 focus:bg-red-900/20"
                      >
                        {isDeleting ? 'Deleting...' : 'Delete'}
                      </DropdownMenuItem>
                    </>
                  )}
                </DropdownMenuContent>
              </DropdownMenu>
            )}
          </>
        )}
      </div>
    </div>
  );
}
