'use client'

import type { Document, Version } from './types'

interface VersionsTabProps {
  documents: Document[]
  versions: Version[]
  selectedDocument: string
  setSelectedDocument: (id: string) => void
  loading: boolean
}

export default function VersionsTab({ documents, versions, selectedDocument, setSelectedDocument, loading }: VersionsTabProps) {
  return (
    <div className="p-6">
      <div className="flex items-center justify-between mb-6">
        <div className="flex items-center gap-4">
          <h2 className="text-lg font-semibold text-slate-900">Versionen</h2>
          <select
            value={selectedDocument}
            onChange={(e) => setSelectedDocument(e.target.value)}
            className="px-3 py-2 border border-slate-300 rounded-lg text-sm"
          >
            <option value="">Dokument auswählen...</option>
            {documents.map((doc) => (
              <option key={doc.id} value={doc.id}>
                {doc.name}
              </option>
            ))}
          </select>
        </div>
        {selectedDocument && (
          <button className="px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors text-sm font-medium">
            + Neue Version
          </button>
        )}
      </div>

      {!selectedDocument ? (
        <div className="text-center py-12 text-slate-500">
          Bitte wählen Sie ein Dokument aus
        </div>
      ) : loading ? (
        <div className="text-center py-12 text-slate-500">Lade Versionen...</div>
      ) : versions.length === 0 ? (
        <div className="text-center py-12 text-slate-500">
          Keine Versionen vorhanden
        </div>
      ) : (
        <div className="space-y-4">
          {versions.map((version) => (
            <div
              key={version.id}
              className="border border-slate-200 rounded-lg p-4 hover:border-primary-300 transition-colors"
            >
              <div className="flex items-start justify-between">
                <div>
                  <div className="flex items-center gap-2 mb-1">
                    <span className="font-semibold text-slate-900">v{version.version}</span>
                    <span className="px-2 py-0.5 bg-slate-100 text-slate-600 rounded text-xs">
                      {version.language.toUpperCase()}
                    </span>
                    <span
                      className={`px-2 py-0.5 rounded text-xs ${
                        version.status === 'published'
                          ? 'bg-green-100 text-green-700'
                          : version.status === 'draft'
                          ? 'bg-yellow-100 text-yellow-700'
                          : 'bg-slate-100 text-slate-600'
                      }`}
                    >
                      {version.status}
                    </span>
                  </div>
                  <h3 className="text-slate-700">{version.title}</h3>
                  <p className="text-sm text-slate-500 mt-1">
                    Erstellt: {new Date(version.created_at).toLocaleDateString('de-DE')}
                  </p>
                </div>
                <div className="flex gap-2">
                  <button className="px-3 py-1.5 text-sm text-slate-600 hover:text-slate-900 border border-slate-300 rounded-lg hover:border-slate-400">
                    Bearbeiten
                  </button>
                  {version.status === 'draft' && (
                    <button className="px-3 py-1.5 text-sm text-white bg-green-600 hover:bg-green-700 rounded-lg">
                      Veröffentlichen
                    </button>
                  )}
                </div>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  )
}
