'use client'

import type { ContainerInfo, DockerStats } from '../types'

export function DeploymentsTab({
  dockerStats,
  containerFilter,
  setContainerFilter,
  filteredContainers,
  onContainerAction,
  actionLoading,
  onRefresh,
}: {
  dockerStats: DockerStats | null
  containerFilter: 'all' | 'running' | 'stopped'
  setContainerFilter: (filter: 'all' | 'running' | 'stopped') => void
  filteredContainers: ContainerInfo[]
  onContainerAction: (containerId: string, action: 'start' | 'stop' | 'restart') => void
  actionLoading: string | null
  onRefresh: () => void
}) {
  const getStateColor = (state: string) => {
    switch (state) {
      case 'running': return 'bg-green-100 text-green-800'
      case 'exited':
      case 'dead': return 'bg-red-100 text-red-800'
      case 'paused': return 'bg-yellow-100 text-yellow-800'
      case 'restarting': return 'bg-blue-100 text-blue-800'
      default: return 'bg-slate-100 text-slate-600'
    }
  }

  return (
    <div className="space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h3 className="text-lg font-semibold text-slate-800">Docker Container</h3>
          {dockerStats && (
            <p className="text-sm text-slate-600">
              {dockerStats.running_containers} laufend, {dockerStats.stopped_containers} gestoppt, {dockerStats.total_containers} gesamt
            </p>
          )}
        </div>
        <div className="flex items-center gap-2">
          <select
            value={containerFilter}
            onChange={(e) => setContainerFilter(e.target.value as typeof containerFilter)}
            className="px-3 py-1.5 text-sm border border-slate-300 rounded-lg bg-white"
          >
            <option value="all">Alle</option>
            <option value="running">Laufend</option>
            <option value="stopped">Gestoppt</option>
          </select>
          <button
            onClick={onRefresh}
            className="px-3 py-1.5 text-sm border border-slate-300 text-slate-700 rounded-lg hover:bg-slate-50"
          >
            Aktualisieren
          </button>
        </div>
      </div>

      {/* Container List */}
      {filteredContainers.length === 0 ? (
        <div className="text-center py-8 text-slate-500">Keine Container gefunden</div>
      ) : (
        <div className="space-y-3">
          {filteredContainers.map((container) => (
            <div
              key={container.id}
              className={`border rounded-xl p-4 transition-colors ${
                container.state === 'running'
                  ? 'border-green-200 bg-green-50/30'
                  : 'border-slate-200 bg-slate-50/50'
              }`}
            >
              <div className="flex items-start justify-between gap-4">
                <div className="flex-1 min-w-0">
                  <div className="flex items-center gap-2 mb-1">
                    <span className="font-semibold text-slate-900 truncate">{container.name}</span>
                    <span className={`px-2 py-0.5 text-xs font-medium rounded-full ${getStateColor(container.state)}`}>
                      {container.state}
                    </span>
                  </div>
                  <div className="text-sm text-slate-500 mb-2">
                    <span className="font-mono">{container.image}</span>
                    {container.ports.length > 0 && (
                      <span className="ml-2 text-slate-400">
                        | {container.ports.slice(0, 2).join(', ')}
                        {container.ports.length > 2 && ` +${container.ports.length - 2}`}
                      </span>
                    )}
                  </div>

                  {container.state === 'running' && (
                    <div className="flex flex-wrap gap-4 text-sm">
                      <div className="flex items-center gap-1">
                        <span className="text-slate-500">CPU:</span>
                        <span className={`font-medium ${container.cpu_percent > 80 ? 'text-red-600' : 'text-slate-700'}`}>
                          {container.cpu_percent.toFixed(1)}%
                        </span>
                      </div>
                      <div className="flex items-center gap-1">
                        <span className="text-slate-500">RAM:</span>
                        <span className={`font-medium ${container.memory_percent > 80 ? 'text-red-600' : 'text-slate-700'}`}>
                          {container.memory_usage}
                        </span>
                        <span className="text-slate-400">({container.memory_percent.toFixed(1)}%)</span>
                      </div>
                      <div className="flex items-center gap-1">
                        <span className="text-slate-500">Net:</span>
                        <span className="text-slate-700">{container.network_rx} / {container.network_tx}</span>
                      </div>
                    </div>
                  )}
                </div>

                <div className="flex items-center gap-2 flex-shrink-0">
                  {container.state === 'running' ? (
                    <>
                      <button
                        onClick={() => onContainerAction(container.id, 'restart')}
                        disabled={actionLoading !== null}
                        className="px-3 py-1.5 text-sm bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 transition-colors"
                      >
                        {actionLoading === `${container.id}-restart` ? '...' : 'Restart'}
                      </button>
                      <button
                        onClick={() => onContainerAction(container.id, 'stop')}
                        disabled={actionLoading !== null}
                        className="px-3 py-1.5 text-sm bg-red-600 text-white rounded-lg hover:bg-red-700 disabled:opacity-50 transition-colors"
                      >
                        {actionLoading === `${container.id}-stop` ? '...' : 'Stop'}
                      </button>
                    </>
                  ) : (
                    <button
                      onClick={() => onContainerAction(container.id, 'start')}
                      disabled={actionLoading !== null}
                      className="px-3 py-1.5 text-sm bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50 transition-colors"
                    >
                      {actionLoading === `${container.id}-start` ? '...' : 'Start'}
                    </button>
                  )}
                </div>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  )
}
