"use client";

import { useState } from "react";
import { FileDown, Loader2, FileSpreadsheet, FileText } from "lucide-react";
import { useExport } from "@/hooks/useExport";
import { toast } from "react-hot-toast";

export function ExportPanel() {
  const { exportSnapshot, exportAuditTrail, isLoading, error } = useExport();

  const handleExportSnapshot = async (format: "csv" | "xlsx") => {
    try {
      await exportSnapshot(format);
      const formatName = format === "csv" ? "CSV" : "Excel";
      toast.success(`Inventory snapshot exported as ${formatName}`);
    } catch (err) {
      // Error handled by hook
    }
  };

  const handleExportAuditTrail = async (format: "csv" | "xlsx") => {
    try {
      await exportAuditTrail(format);
      const formatName = format === "csv" ? "CSV" : "Excel";
      toast.success(`Audit trail exported as ${formatName}`);
    } catch (err) {
      // Error handled by hook
    }
  };

  return (
    <div data-testid="admin-tab-exports" className="level-1 p-4 md:p-6 flex flex-col h-full">
      <div className="flex items-center gap-3 mb-4 md:mb-6">
        <div className="w-10 h-10 bg-black flex items-center justify-center text-primary border border-border">
          <FileDown size={20} />
        </div>
        <div>
          <h2 className="text-xl font-normal text-white tracking-tight">Export & Reports</h2>
          <p className="text-[10px] md:text-xs text-muted font-normal tracking-tight">Download system snapshots and action logs</p>
        </div>
      </div>

      <div className="grid md:grid-cols-2 gap-4">
        {/* Inventory Snapshot Section */}
        <div className="p-4 border border-border hover:border-primary/20 transition-all group flex flex-col justify-between">
          <div className="mb-4">
            <h3 className="text-sm font-normal text-secondary flex items-center gap-2">
              <FileSpreadsheet size={14} className="text-primary" />
              Inventory Snapshot
            </h3>
            <p className="text-xs text-muted mt-1 leading-relaxed">
              Export current inventory state with all item details and locations.
            </p>
          </div>
          
          <div className="grid grid-cols-2 gap-2">
            <button
              onClick={() => handleExportSnapshot("csv")}
              disabled={isLoading}
              className="btn-primary text-sm px-5 py-1.5 disabled:opacity-50"
              aria-label="Export inventory snapshot as CSV"
            >
              {isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileText size={14} />}
              CSV
            </button>
            <button
              onClick={() => handleExportSnapshot("xlsx")}
              disabled={isLoading}
              className="btn-primary text-sm px-5 py-1.5 disabled:opacity-50"
              aria-label="Export inventory snapshot as Excel"
            >
              {isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileSpreadsheet size={14} />}
              Excel
            </button>
          </div>
        </div>

        {/* Audit Trail Section */}
        <div className="p-4 border border-border hover:border-primary/20 transition-all group flex flex-col justify-between">
          <div className="mb-4">
            <h3 className="text-sm font-normal text-secondary flex items-center gap-2">
              <FileText size={14} className="text-primary" />
              Audit Trail
            </h3>
            <p className="text-xs text-muted mt-1 leading-relaxed">
              Complete history of system actions, changes, and user operations.
            </p>
          </div>
          <div className="grid grid-cols-2 gap-2">
            <button
              onClick={() => handleExportAuditTrail("csv")}
              disabled={isLoading}
              className="btn-primary text-sm px-5 py-1.5 disabled:opacity-50"
              aria-label="Export audit trail as CSV"
            >
              {isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileText size={14} />}
              CSV
            </button>
            <button
              onClick={() => handleExportAuditTrail("xlsx")}
              disabled={isLoading}
              className="btn-primary text-sm px-5 py-1.5 disabled:opacity-50"
              aria-label="Export audit trail as Excel"
            >
              {isLoading ? <Loader2 size={14} className="animate-spin" /> : <FileSpreadsheet size={14} />}
              Excel
            </button>
          </div>
        </div>
      </div>

      {error && (
        <div className="mt-4 p-3 bg-rose-500/10 border border-rose-500/20 text-rose-500 text-[10px] flex items-center gap-2 animate-in slide-in-from-top-2 duration-300">
          <div className="w-1.5 h-1.5 bg-rose-500 animate-pulse" />
          <span className="font-normal truncate">Error: {error}</span>
        </div>
      )}
    </div>
  );
}
