'use client'

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

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

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

  return (
    <div className="space-y-6">
      {services.filter(s => s.endpoints.length > 0).map((service) => (
        <div key={service.id} className="bg-white rounded-xl border border-slate-200 overflow-hidden">
          <div className="px-6 py-4 bg-slate-50 border-b border-slate-200">
            <div className="flex items-center justify-between">
              <div>
                <h3 className="font-semibold text-slate-900">{service.name}</h3>
                <div className="text-sm text-slate-500">Base URL: http://localhost:{service.port}</div>
              </div>
              <span className={`text-xs px-2 py-0.5 rounded-full ${getServiceTypeColor(service.type)}`}>
                {service.type}
              </span>
            </div>
          </div>

          <div className="divide-y divide-slate-100">
            {service.endpoints.map((endpoint, idx) => {
              const endpointId = `${service.id}-${idx}`
              const curlCommand = `curl -X ${endpoint.method} http://localhost:${service.port}${endpoint.path}`

              return (
                <div key={idx} className="px-6 py-3 hover:bg-slate-50 transition-colors">
                  <div className="flex items-center gap-3">
                    <span className={`text-xs font-mono font-semibold px-2 py-1 rounded ${getMethodColor(endpoint.method)}`}>
                      {endpoint.method}
                    </span>
                    <code className="text-sm font-mono text-slate-700 flex-1">{endpoint.path}</code>
                    <button
                      onClick={() => copyToClipboard(curlCommand, endpointId)}
                      className="text-xs text-slate-400 hover:text-slate-600 transition-colors"
                      title="Copy curl command"
                    >
                      {copiedEndpoint === endpointId ? (
                        <span className="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 className="text-sm text-slate-500 mt-1 ml-14">{endpoint.description}</div>
                </div>
              )
            })}
          </div>
        </div>
      ))}
    </div>
  )
}
