'use client';

import { useState } from 'react';
import { Download, Upload, FileText, AlertCircle, CheckCircle } from 'lucide-react';
import { useToast } from '@/components/Toast';

interface ImportResult {
  success: boolean;
  message: string;
  results: {
    imported: number;
    skipped: number;
    errors: string[];
  };
}

export default function ImportExport() {
  const [isExporting, setIsExporting] = useState(false);
  const [isImporting, setIsImporting] = useState(false);
  const [importResult, setImportResult] = useState<ImportResult | null>(null);
  const { addToast } = useToast();

  const handleExport = async () => {
    setIsExporting(true);
    try {
      const sessionToken = sessionStorage.getItem('admin_session_token') || '';
      const response = await fetch('/api/projects/export', {
        headers: {
          'x-admin-request': 'true',
          'x-session-token': sessionToken,
        }
      });
      if (!response.ok) throw new Error('Export failed');
      
      const blob = await response.blob();
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = `portfolio-projects-${new Date().toISOString().split('T')[0]}.json`;
      document.body.appendChild(a);
      a.click();
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
      
      addToast({
        type: 'success',
        title: 'Export erfolgreich',
        message: 'Projekte wurden erfolgreich exportiert'
      });
    } catch {
      addToast({
        type: 'error',
        title: 'Export fehlgeschlagen',
        message: 'Fehler beim Exportieren der Projekte'
      });
    } finally {
      setIsExporting(false);
    }
  };

  const handleImport = async (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (!file) return;

    setIsImporting(true);
    setImportResult(null);

    try {
      const text = await file.text();
      const data = JSON.parse(text);
      
      const sessionToken = sessionStorage.getItem('admin_session_token') || '';
      const response = await fetch('/api/projects/import', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-admin-request': 'true',
          'x-session-token': sessionToken,
        },
        body: JSON.stringify(data)
      });

      const result: ImportResult = await response.json();
      setImportResult(result);
      
      if (result.success) {
        addToast({
          type: 'success',
          title: 'Import erfolgreich',
          message: result.message
        });
      } else {
        addToast({
          type: 'error',
          title: 'Import fehlgeschlagen',
          message: result.message
        });
      }
    } catch {
      addToast({
        type: 'error',
        title: 'Import fehlgeschlagen',
        message: 'Ungültige Datei oder Format'
      });
    } finally {
      setIsImporting(false);
      // Reset file input
      event.target.value = '';
    }
  };

  return (
    <div className="bg-white border border-stone-200 rounded-xl p-6">
      <h3 className="text-lg font-semibold text-stone-900 mb-4 flex items-center">
        <FileText className="w-5 h-5 mr-2 text-stone-600" />
        Import & Export
      </h3>
      
      <div className="space-y-4">
        {/* Export Section */}
        <div className="bg-stone-50 border border-stone-200 rounded-xl p-4">
          <h4 className="font-medium text-stone-900 mb-2">Export Projekte</h4>
          <p className="text-sm text-stone-600 mb-3">
            Alle Projekte als JSON-Datei herunterladen
          </p>
          <button
            onClick={handleExport}
            disabled={isExporting}
            className="flex items-center px-4 py-2 bg-stone-900 text-white rounded-lg hover:bg-stone-800 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
          >
            <Download className="w-4 h-4 mr-2" />
            {isExporting ? 'Exportiere...' : 'Exportieren'}
          </button>
        </div>

        {/* Import Section */}
        <div className="bg-stone-50 border border-stone-200 rounded-xl p-4">
          <h4 className="font-medium text-stone-900 mb-2">Import Projekte</h4>
          <p className="text-sm text-stone-600 mb-3">
            JSON-Datei mit Projekten hochladen
          </p>
          <label className="flex items-center px-4 py-2 bg-stone-900 text-white rounded-lg hover:bg-stone-800 transition-colors cursor-pointer w-fit">
            <Upload className="w-4 h-4 mr-2" />
            {isImporting ? 'Importiere...' : 'Datei auswählen'}
            <input
              type="file"
              accept=".json"
              onChange={handleImport}
              disabled={isImporting}
              className="hidden"
            />
          </label>
        </div>

        {/* Import Results */}
        {importResult && (
          <div className="bg-stone-50 border border-stone-200 rounded-xl p-4">
            <h4 className="font-medium text-stone-900 mb-2 flex items-center">
              {importResult.success ? (
                <CheckCircle className="w-5 h-5 mr-2 text-green-600" />
              ) : (
                <AlertCircle className="w-5 h-5 mr-2 text-red-600" />
              )}
              Import Ergebnis
            </h4>
            <div className="text-sm text-stone-600 space-y-1">
              <p><strong>Importiert:</strong> {importResult.results.imported}</p>
              <p><strong>Übersprungen:</strong> {importResult.results.skipped}</p>
              {importResult.results.errors.length > 0 && (
                <div>
                  <p><strong>Fehler:</strong></p>
                  <ul className="list-disc list-inside ml-4">
                    {importResult.results.errors.map((error, index) => (
                      <li key={index} className="text-red-600">{error}</li>
                    ))}
                  </ul>
                </div>
              )}
            </div>
          </div>
        )}
      </div>
    </div>
  );
}
