import { useState } from 'react';
import {
  ShoppingBag,
  Plus,
  MoreVertical,
  Eye,
  CheckCircle,
  XCircle,
  Calendar,
  DollarSign,
  RefreshCw,
  Search,
  FileText,
  Package,
  Download,
} from 'lucide-react';
import { Button } from '@components/atoms/Button';
import { Card, CardHeader, CardTitle, CardContent } from '@components/molecules/Card';
import { DataTable, type Column } from '@components/organisms/DataTable';
import { Dropdown, type DropdownItem } from '@components/organisms/Dropdown';
import { Breadcrumbs } from '@components/organisms/Breadcrumbs';
import { ConfirmModal } from '@components/organisms/Modal';
import { NoDataEmptyState, ErrorEmptyState } from '@components/templates/EmptyState';
import { usePurchaseOrders } from '@features/purchases/hooks';
import type { PurchaseOrder, PurchaseOrderStatus } from '@features/purchases/types';
import { formatDate, formatNumber } from '@utils/formatters';

const statusLabels: Record<PurchaseOrderStatus, string> = {
  draft: 'Borrador',
  sent: 'Enviado',
  confirmed: 'Confirmado',
  done: 'Completado',
  cancelled: 'Cancelado',
};

const statusColors: Record<PurchaseOrderStatus, string> = {
  draft: 'bg-gray-100 text-gray-700',
  sent: 'bg-blue-100 text-blue-700',
  confirmed: 'bg-green-100 text-green-700',
  done: 'bg-purple-100 text-purple-700',
  cancelled: 'bg-red-100 text-red-700',
};

// Helper function to format currency with 2 decimals
const formatCurrency = (value: number): string => {
  return formatNumber(value, 'es-MX', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
};

export function PurchaseOrdersPage() {
  const [selectedStatus, setSelectedStatus] = useState<PurchaseOrderStatus | ''>('');
  const [searchTerm, setSearchTerm] = useState('');
  const [dateFrom, setDateFrom] = useState('');
  const [dateTo, setDateTo] = useState('');
  const [orderToConfirm, setOrderToConfirm] = useState<PurchaseOrder | null>(null);
  const [orderToCancel, setOrderToCancel] = useState<PurchaseOrder | null>(null);

  const {
    orders,
    total,
    page,
    totalPages,
    isLoading,
    error,
    setPage,
    refresh,
    confirmOrder,
    cancelOrder,
    downloadPdf,
  } = usePurchaseOrders({
    status: selectedStatus || undefined,
    search: searchTerm || undefined,
    dateFrom: dateFrom || undefined,
    dateTo: dateTo || undefined,
    limit: 20,
  });

  const getActionsMenu = (order: PurchaseOrder): DropdownItem[] => {
    const items: DropdownItem[] = [
      {
        key: 'view',
        label: 'Ver detalle',
        icon: <Eye className="h-4 w-4" />,
        onClick: () => console.log('View', order.id),
      },
      {
        key: 'pdf',
        label: 'Descargar PDF',
        icon: <Download className="h-4 w-4" />,
        onClick: () => downloadPdf(order.id, `OC-${order.name}.pdf`),
      },
    ];

    if (order.status === 'draft') {
      items.push({
        key: 'confirm',
        label: 'Confirmar orden',
        icon: <CheckCircle className="h-4 w-4" />,
        onClick: () => setOrderToConfirm(order),
      });
      items.push({
        key: 'cancel',
        label: 'Cancelar',
        icon: <XCircle className="h-4 w-4" />,
        danger: true,
        onClick: () => setOrderToCancel(order),
      });
    }

    if (order.status === 'confirmed') {
      items.push({
        key: 'receive',
        label: 'Recibir productos',
        icon: <Package className="h-4 w-4" />,
        onClick: () => console.log('Receive', order.id),
      });
      items.push({
        key: 'invoice',
        label: 'Crear factura',
        icon: <FileText className="h-4 w-4" />,
        onClick: () => console.log('Invoice', order.id),
      });
    }

    return items;
  };

  const columns: Column<PurchaseOrder>[] = [
    {
      key: 'name',
      header: 'Orden',
      render: (order) => (
        <div className="flex items-center gap-3">
          <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-amber-50">
            <ShoppingBag className="h-5 w-5 text-amber-600" />
          </div>
          <div>
            <div className="font-medium text-gray-900">{order.name}</div>
            {order.ref && (
              <div className="text-sm text-gray-500">Ref: {order.ref}</div>
            )}
          </div>
        </div>
      ),
    },
    {
      key: 'partner',
      header: 'Proveedor',
      render: (order) => (
        <div>
          <div className="text-sm text-gray-900">{order.partnerName || order.partnerId}</div>
        </div>
      ),
    },
    {
      key: 'date',
      header: 'Fecha',
      sortable: true,
      render: (order) => (
        <span className="text-sm text-gray-600">
          {formatDate(order.orderDate, 'short')}
        </span>
      ),
    },
    {
      key: 'expectedDate',
      header: 'Entrega Esperada',
      render: (order) => (
        <span className="text-sm text-gray-600">
          {order.expectedDate ? formatDate(order.expectedDate, 'short') : '-'}
        </span>
      ),
    },
    {
      key: 'amount',
      header: 'Total',
      sortable: true,
      render: (order) => (
        <div className="text-right">
          <div className="font-medium text-gray-900">
            ${formatCurrency(order.amountTotal)}
          </div>
          {order.currencyCode && order.currencyCode !== 'MXN' && (
            <div className="text-xs text-gray-500">{order.currencyCode}</div>
          )}
        </div>
      ),
    },
    {
      key: 'receiptStatus',
      header: 'Recepcion',
      render: (order) => {
        const status = order.receiptStatus || 'pending';
        const colors: Record<string, string> = {
          pending: 'bg-amber-100 text-amber-700',
          partial: 'bg-blue-100 text-blue-700',
          received: 'bg-green-100 text-green-700',
        };
        const labels: Record<string, string> = {
          pending: 'Pendiente',
          partial: 'Parcial',
          received: 'Recibido',
        };
        return (
          <span className={`inline-flex rounded-full px-2 py-1 text-xs font-medium ${colors[status] || 'bg-gray-100 text-gray-700'}`}>
            {labels[status] || status}
          </span>
        );
      },
    },
    {
      key: 'status',
      header: 'Estado',
      render: (order) => (
        <span className={`inline-flex rounded-full px-2 py-1 text-xs font-medium ${statusColors[order.status]}`}>
          {statusLabels[order.status]}
        </span>
      ),
    },
    {
      key: 'actions',
      header: '',
      render: (order) => (
        <Dropdown
          trigger={
            <button className="rounded p-1 hover:bg-gray-100">
              <MoreVertical className="h-4 w-4 text-gray-500" />
            </button>
          }
          items={getActionsMenu(order)}
          align="right"
        />
      ),
    },
  ];

  const handleConfirm = async () => {
    if (orderToConfirm) {
      await confirmOrder(orderToConfirm.id);
      setOrderToConfirm(null);
    }
  };

  const handleCancel = async () => {
    if (orderToCancel) {
      await cancelOrder(orderToCancel.id);
      setOrderToCancel(null);
    }
  };

  // Calculate summary stats
  const draftCount = orders.filter(o => o.status === 'draft').length;
  const confirmedCount = orders.filter(o => o.status === 'confirmed').length;
  const totalAmount = orders.reduce((sum, o) => sum + o.amountTotal, 0);
  const pendingReceipt = orders.filter(o => o.receiptStatus === 'pending' && o.status === 'confirmed').length;

  if (error) {
    return (
      <div className="p-6">
        <ErrorEmptyState onRetry={refresh} />
      </div>
    );
  }

  return (
    <div className="space-y-6 p-6">
      <Breadcrumbs items={[
        { label: 'Compras', href: '/purchases' },
        { label: 'Ordenes de Compra' },
      ]} />

      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-gray-900">Ordenes de Compra</h1>
          <p className="text-sm text-gray-500">
            Gestiona ordenes de compra y recepciones de proveedores
          </p>
        </div>
        <div className="flex gap-2">
          <Button variant="outline" onClick={refresh} disabled={isLoading}>
            <RefreshCw className={`mr-2 h-4 w-4 ${isLoading ? 'animate-spin' : ''}`} />
            Actualizar
          </Button>
          <Button>
            <Plus className="mr-2 h-4 w-4" />
            Nueva orden
          </Button>
        </div>
      </div>

      {/* Summary Stats */}
      <div className="grid grid-cols-1 gap-4 sm:grid-cols-4">
        <Card className="cursor-pointer hover:shadow-md transition-shadow" onClick={() => setSelectedStatus('draft')}>
          <CardContent className="p-4">
            <div className="flex items-center gap-3">
              <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-gray-100">
                <ShoppingBag className="h-5 w-5 text-gray-600" />
              </div>
              <div>
                <div className="text-sm text-gray-500">Borradores</div>
                <div className="text-xl font-bold text-gray-900">{draftCount}</div>
              </div>
            </div>
          </CardContent>
        </Card>

        <Card className="cursor-pointer hover:shadow-md transition-shadow" onClick={() => setSelectedStatus('confirmed')}>
          <CardContent className="p-4">
            <div className="flex items-center gap-3">
              <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-green-100">
                <CheckCircle className="h-5 w-5 text-green-600" />
              </div>
              <div>
                <div className="text-sm text-gray-500">Confirmadas</div>
                <div className="text-xl font-bold text-green-600">{confirmedCount}</div>
              </div>
            </div>
          </CardContent>
        </Card>

        <Card>
          <CardContent className="p-4">
            <div className="flex items-center gap-3">
              <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-amber-100">
                <Package className="h-5 w-5 text-amber-600" />
              </div>
              <div>
                <div className="text-sm text-gray-500">Por Recibir</div>
                <div className="text-xl font-bold text-amber-600">{pendingReceipt}</div>
              </div>
            </div>
          </CardContent>
        </Card>

        <Card>
          <CardContent className="p-4">
            <div className="flex items-center gap-3">
              <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-blue-100">
                <DollarSign className="h-5 w-5 text-blue-600" />
              </div>
              <div>
                <div className="text-sm text-gray-500">Total Compras</div>
                <div className="text-xl font-bold text-blue-600">${formatCurrency(totalAmount)}</div>
              </div>
            </div>
          </CardContent>
        </Card>
      </div>

      <Card>
        <CardHeader>
          <CardTitle>Lista de Ordenes</CardTitle>
        </CardHeader>
        <CardContent>
          <div className="space-y-4">
            {/* Filters */}
            <div className="flex flex-wrap items-center gap-4">
              <div className="relative flex-1 min-w-[200px]">
                <Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-gray-400" />
                <input
                  type="text"
                  placeholder="Buscar ordenes..."
                  value={searchTerm}
                  onChange={(e) => setSearchTerm(e.target.value)}
                  className="w-full rounded-md border border-gray-300 py-2 pl-10 pr-4 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
                />
              </div>

              <select
                value={selectedStatus}
                onChange={(e) => setSelectedStatus(e.target.value as PurchaseOrderStatus | '')}
                className="rounded-md border border-gray-300 px-3 py-2 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
              >
                <option value="">Todos los estados</option>
                {Object.entries(statusLabels).map(([value, label]) => (
                  <option key={value} value={value}>{label}</option>
                ))}
              </select>

              <div className="flex items-center gap-2">
                <Calendar className="h-4 w-4 text-gray-400" />
                <input
                  type="date"
                  value={dateFrom}
                  onChange={(e) => setDateFrom(e.target.value)}
                  className="rounded-md border border-gray-300 px-3 py-2 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
                />
                <span className="text-gray-400">-</span>
                <input
                  type="date"
                  value={dateTo}
                  onChange={(e) => setDateTo(e.target.value)}
                  className="rounded-md border border-gray-300 px-3 py-2 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
                />
              </div>

              {(selectedStatus || searchTerm || dateFrom || dateTo) && (
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => {
                    setSelectedStatus('');
                    setSearchTerm('');
                    setDateFrom('');
                    setDateTo('');
                  }}
                >
                  Limpiar filtros
                </Button>
              )}
            </div>

            {/* Table */}
            {orders.length === 0 && !isLoading ? (
              <NoDataEmptyState
                entityName="ordenes de compra"
              />
            ) : (
              <DataTable
                data={orders}
                columns={columns}
                isLoading={isLoading}
                pagination={{
                  page,
                  totalPages,
                  total,
                  limit: 20,
                  onPageChange: setPage,
                }}
              />
            )}
          </div>
        </CardContent>
      </Card>

      {/* Confirm Order Modal */}
      <ConfirmModal
        isOpen={!!orderToConfirm}
        onClose={() => setOrderToConfirm(null)}
        onConfirm={handleConfirm}
        title="Confirmar orden de compra"
        message={`¿Confirmar la orden ${orderToConfirm?.name}? Esta accion enviara la orden al proveedor.`}
        variant="success"
        confirmText="Confirmar"
      />

      {/* Cancel Order Modal */}
      <ConfirmModal
        isOpen={!!orderToCancel}
        onClose={() => setOrderToCancel(null)}
        onConfirm={handleCancel}
        title="Cancelar orden de compra"
        message={`¿Cancelar la orden ${orderToCancel?.name}? Esta accion no se puede deshacer.`}
        variant="danger"
        confirmText="Cancelar orden"
      />
    </div>
  );
}

export default PurchaseOrdersPage;
