'use client'

import { useState } from 'react'
import { services } from '../data'
import { getServiceTypeColor } from '../helpers'

export default function DockerTab() {
  const [copiedEndpoint, setCopiedEndpoint] = useState<string | null>(null)

  const copyToClipboard = (text: string, id: string) => {
    navigator.clipboard.writeText(text)
    setCopiedEndpoint(id)
    setTimeout(() => setCopiedEndpoint(null), 2000)
  }

  const commonCommands = [
    { label: 'Alle Services starten', cmd: 'docker compose up -d' },
    { label: 'Logs anzeigen', cmd: 'docker compose logs -f [service]' },
    { label: 'Service neu bauen', cmd: 'docker compose build [service] --no-cache' },
    { label: 'Container Status', cmd: 'docker ps --format "table {{.Names}}\\t{{.Status}}\\t{{.Ports}}"' },
    { label: 'In Container einloggen', cmd: 'docker exec -it [container] /bin/sh' },
  ]

  return (
    <div className="space-y-6">
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-lg font-semibold text-slate-900 mb-4">Docker Compose Services</h2>

        <div className="overflow-x-auto">
          <table className="w-full text-sm">
            <thead>
              <tr className="border-b border-slate-200">
                <th className="text-left py-3 px-4 font-medium text-slate-700">Container</th>
                <th className="text-left py-3 px-4 font-medium text-slate-700">Port</th>
                <th className="text-left py-3 px-4 font-medium text-slate-700">Type</th>
                <th className="text-left py-3 px-4 font-medium text-slate-700">Health Check</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-slate-100">
              {services.map((service) => (
                <tr key={service.id} className="hover:bg-slate-50">
                  <td className="py-3 px-4">
                    <code className="text-sm bg-slate-100 px-2 py-0.5 rounded">{service.container}</code>
                  </td>
                  <td className="py-3 px-4 font-mono">{service.port}</td>
                  <td className="py-3 px-4">
                    <span className={`text-xs px-2 py-0.5 rounded-full ${getServiceTypeColor(service.type)}`}>
                      {service.type}
                    </span>
                  </td>
                  <td className="py-3 px-4">
                    {service.healthEndpoint ? (
                      <code className="text-xs bg-green-50 text-green-700 px-2 py-0.5 rounded">
                        {service.healthEndpoint}
                      </code>
                    ) : (
                      <span className="text-slate-400">-</span>
                    )}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>

      {/* Common Commands */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-lg font-semibold text-slate-900 mb-4">Haeufige Befehle</h2>

        <div className="space-y-4">
          {commonCommands.map((item, idx) => (
            <div key={idx} className="flex items-center gap-4 p-3 bg-slate-50 rounded-lg">
              <div className="text-sm text-slate-600 w-40">{item.label}</div>
              <code className="flex-1 text-sm font-mono bg-slate-900 text-green-400 px-3 py-2 rounded">
                {item.cmd}
              </code>
              <button
                onClick={() => copyToClipboard(item.cmd, `cmd-${idx}`)}
                className="text-slate-400 hover:text-slate-600"
              >
                {copiedEndpoint === `cmd-${idx}` ? (
                  <span className="text-xs text-green-600">Copied!</span>
                ) : (
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
                  </svg>
                )}
              </button>
            </div>
          ))}
        </div>
      </div>

      {/* Environment Variables */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-lg font-semibold text-slate-900 mb-4">Wichtige Umgebungsvariablen</h2>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          {services.filter(s => s.envVars.length > 0).map((service) => (
            <div key={service.id} className="p-4 bg-slate-50 rounded-lg">
              <h4 className="font-medium text-slate-900 mb-2">{service.name}</h4>
              <div className="flex flex-wrap gap-2">
                {service.envVars.map((env) => (
                  <code key={env} className="text-xs bg-slate-200 text-slate-700 px-2 py-1 rounded">
                    {env}
                  </code>
                ))}
              </div>
            </div>
          ))}
        </div>
      </div>
    </div>
  )
}
