'use client'

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

interface DeploymentsTabProps {
  dockerStats: DockerStats | null
  filteredContainers: ContainerInfo[]
  containerFilter: ContainerFilter
  setContainerFilter: (f: ContainerFilter) => void
  actionLoading: string | null
  containerAction: (containerId: string, action: 'start' | 'stop' | 'restart') => Promise<void>
  loadContainerData: () => Promise<void>
}

export function DeploymentsTab({
  dockerStats,
  filteredContainers,
  containerFilter,
  setContainerFilter,
  actionLoading,
  containerAction,
  loadContainerData,
}: DeploymentsTabProps) {
  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 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={loadContainerData}
            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) => (
            <ContainerCard
              key={container.id}
              container={container}
              actionLoading={actionLoading}
              containerAction={containerAction}
            />
          ))}
        </div>
      )}
    </div>
  )
}

// ============================================================================
// Container Card Sub-component
// ============================================================================

function ContainerCard({
  container,
  actionLoading,
  containerAction,
}: {
  container: ContainerInfo
  actionLoading: string | null
  containerAction: (containerId: string, action: 'start' | 'stop' | 'restart') => Promise<void>
}) {
  return (
    <div
      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={() => containerAction(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={() => containerAction(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={() => containerAction(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>
  )
}
