import React, { useState } from 'react';
import { Plus, Search, Download, ShoppingCart, Truck, DollarSign, Calendar, Clock, CheckCircle, AlertCircle, Package, Eye, Edit } from 'lucide-react';
import { Button, Input, Card, Badge, StatsGrid } from '../../../../components/ui';
import { formatters } from '../../../../components/ui/Stats/StatsPresets';
import { PageHeader } from '../../../../components/layout';

const ProcurementPage: React.FC = () => {
  const [activeTab, setActiveTab] = useState('orders');
  const [searchTerm, setSearchTerm] = useState('');

  const mockPurchaseOrders = [
    {
      id: 'PO-2024-001',
      supplier: 'Molinos del Sur',
      status: 'pending',
      orderDate: '2024-01-25',
      deliveryDate: '2024-01-28',
      totalAmount: 1250.00,
      items: [
        { name: 'Harina de Trigo', quantity: 50, unit: 'kg', price: 1.20, total: 60.00 },
        { name: 'Harina Integral', quantity: 100, unit: 'kg', price: 1.30, total: 130.00 },
      ],
      paymentStatus: 'pending',
      notes: 'Entrega en horario de mañana',
    },
    {
      id: 'PO-2024-002',
      supplier: 'Levaduras SA',
      status: 'delivered',
      orderDate: '2024-01-20',
      deliveryDate: '2024-01-23',
      totalAmount: 425.50,
      items: [
        { name: 'Levadura Fresca', quantity: 5, unit: 'kg', price: 8.50, total: 42.50 },
        { name: 'Mejorante', quantity: 10, unit: 'kg', price: 12.30, total: 123.00 },
      ],
      paymentStatus: 'paid',
      notes: '',
    },
    {
      id: 'PO-2024-003',
      supplier: 'Lácteos Frescos',
      status: 'in_transit',
      orderDate: '2024-01-24',
      deliveryDate: '2024-01-26',
      totalAmount: 320.75,
      items: [
        { name: 'Mantequilla', quantity: 20, unit: 'kg', price: 5.80, total: 116.00 },
        { name: 'Nata', quantity: 15, unit: 'L', price: 3.25, total: 48.75 },
      ],
      paymentStatus: 'pending',
      notes: 'Producto refrigerado',
    },
  ];

  const mockSuppliers = [
    {
      id: '1',
      name: 'Molinos del Sur',
      contact: 'Juan Pérez',
      email: 'juan@molinosdelsur.com',
      phone: '+34 91 234 5678',
      category: 'Harinas',
      rating: 4.8,
      totalOrders: 24,
      totalSpent: 15600.00,
      paymentTerms: '30 días',
      leadTime: '2-3 días',
      location: 'Sevilla',
      status: 'active',
    },
    {
      id: '2',
      name: 'Levaduras SA',
      contact: 'María González',
      email: 'maria@levaduras.com',
      phone: '+34 93 456 7890',
      category: 'Levaduras',
      rating: 4.6,
      totalOrders: 18,
      totalSpent: 8450.00,
      paymentTerms: '15 días',
      leadTime: '1-2 días',
      location: 'Barcelona',
      status: 'active',
    },
    {
      id: '3',
      name: 'Lácteos Frescos',
      contact: 'Carlos Ruiz',
      email: 'carlos@lacteosfrescos.com',
      phone: '+34 96 789 0123',
      category: 'Lácteos',
      rating: 4.4,
      totalOrders: 32,
      totalSpent: 12300.00,
      paymentTerms: '20 días',
      leadTime: '1 día',
      location: 'Valencia',
      status: 'active',
    },
  ];

  const getStatusBadge = (status: string) => {
    const statusConfig = {
      pending: { color: 'warning', text: 'Pendiente', icon: Clock },
      approved: { color: 'info', text: 'Aprobado', icon: CheckCircle },
      in_transit: { color: 'secondary', text: 'En Tránsito', icon: Truck },
      delivered: { color: 'success', text: 'Entregado', icon: CheckCircle },
      cancelled: { color: 'error', text: 'Cancelado', icon: AlertCircle },
    };
    
    const config = statusConfig[status as keyof typeof statusConfig];
    const Icon = config?.icon;
    return (
      <Badge 
        variant={config?.color as any}
        icon={Icon && <Icon size={12} />}
        text={config?.text || status}
      />
    );
  };

  const getPaymentStatusBadge = (status: string) => {
    const statusConfig = {
      pending: { color: 'warning', text: 'Pendiente', icon: Clock },
      paid: { color: 'success', text: 'Pagado', icon: CheckCircle },
      overdue: { color: 'error', text: 'Vencido', icon: AlertCircle },
    };
    
    const config = statusConfig[status as keyof typeof statusConfig];
    const Icon = config?.icon;
    return (
      <Badge 
        variant={config?.color as any}
        icon={Icon && <Icon size={12} />}
        text={config?.text || status}
      />
    );
  };

  const filteredOrders = mockPurchaseOrders.filter(order => {
    const matchesSearch = order.supplier.toLowerCase().includes(searchTerm.toLowerCase()) ||
      order.id.toLowerCase().includes(searchTerm.toLowerCase()) ||
      order.notes.toLowerCase().includes(searchTerm.toLowerCase());
    
    return matchesSearch;
  });

  const mockPurchaseStats = {
    totalOrders: mockPurchaseOrders.length,
    pendingOrders: mockPurchaseOrders.filter(o => o.status === 'pending').length,
    inTransit: mockPurchaseOrders.filter(o => o.status === 'in_transit').length,
    delivered: mockPurchaseOrders.filter(o => o.status === 'delivered').length,
    totalSpent: mockPurchaseOrders.reduce((sum, order) => sum + order.totalAmount, 0),
    activeSuppliers: mockSuppliers.filter(s => s.status === 'active').length,
  };

  const purchaseOrderStats = [
    {
      title: 'Total Órdenes',
      value: mockPurchaseStats.totalOrders,
      variant: 'default' as const,
      icon: ShoppingCart,
    },
    {
      title: 'Pendientes',
      value: mockPurchaseStats.pendingOrders,
      variant: 'warning' as const,
      icon: Clock,
    },
    {
      title: 'En Tránsito',
      value: mockPurchaseStats.inTransit,
      variant: 'info' as const,
      icon: Truck,
    },
    {
      title: 'Entregadas',
      value: mockPurchaseStats.delivered,
      variant: 'success' as const,
      icon: CheckCircle,
    },
    {
      title: 'Gasto Total',
      value: formatters.currency(mockPurchaseStats.totalSpent),
      variant: 'success' as const,
      icon: DollarSign,
    },
    {
      title: 'Proveedores',
      value: mockPurchaseStats.activeSuppliers,
      variant: 'info' as const,
      icon: Package,
    },
  ];

  return (
    <div className="p-6 space-y-6">
      <PageHeader
        title="Gestión de Compras"
        description="Administra órdenes de compra, proveedores y seguimiento de entregas"
        actions={[
          {
            id: "export",
            label: "Exportar",
            variant: "outline" as const,
            icon: Download,
            onClick: () => console.log('Export purchase orders')
          },
          {
            id: "new",
            label: "Nueva Orden de Compra",
            variant: "primary" as const,
            icon: Plus,
            onClick: () => console.log('New purchase order')
          }
        ]}
      />

      {/* Stats Grid */}
      <StatsGrid 
        stats={purchaseOrderStats}
        columns={6}
      />

      {/* Tabs Navigation */}
      <div className="border-b border-[var(--border-primary)]">
        <nav className="-mb-px flex space-x-8">
          <button
            onClick={() => setActiveTab('orders')}
            className={`py-2 px-1 border-b-2 font-medium text-sm ${
              activeTab === 'orders'
                ? 'border-orange-500 text-[var(--color-primary)]'
                : 'border-transparent text-[var(--text-tertiary)] hover:text-[var(--text-secondary)] hover:border-[var(--border-secondary)]'
            }`}
          >
            Órdenes de Compra
          </button>
          <button
            onClick={() => setActiveTab('suppliers')}
            className={`py-2 px-1 border-b-2 font-medium text-sm ${
              activeTab === 'suppliers'
                ? 'border-orange-500 text-[var(--color-primary)]'
                : 'border-transparent text-[var(--text-tertiary)] hover:text-[var(--text-secondary)] hover:border-[var(--border-secondary)]'
            }`}
          >
            Proveedores
          </button>
          <button
            onClick={() => setActiveTab('analytics')}
            className={`py-2 px-1 border-b-2 font-medium text-sm ${
              activeTab === 'analytics'
                ? 'border-orange-500 text-[var(--color-primary)]'
                : 'border-transparent text-[var(--text-tertiary)] hover:text-[var(--text-secondary)] hover:border-[var(--border-secondary)]'
            }`}
          >
            Análisis
          </button>
        </nav>
      </div>

      {activeTab === 'orders' && (
        <Card className="p-4">
          <div className="flex flex-col sm:flex-row gap-4">
            <div className="flex-1">
              <Input
                placeholder="Buscar órdenes por proveedor, ID o notas..."
                value={searchTerm}
                onChange={(e) => setSearchTerm(e.target.value)}
                className="w-full"
              />
            </div>
            <Button variant="outline" onClick={() => console.log('Export filtered')}>
              <Download className="w-4 h-4 mr-2" />
              Exportar
            </Button>
          </div>
        </Card>
      )}

      {/* Purchase Orders Grid */}
      {activeTab === 'orders' && (
        <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
          {filteredOrders.map((order) => (
            <Card key={order.id} className="p-4">
              <div className="space-y-4">
                {/* Header */}
                <div className="flex items-start justify-between">
                  <div className="flex items-start gap-3">
                    <div className="flex-shrink-0 bg-[var(--color-primary)]/10 p-2 rounded-lg">
                      <ShoppingCart className="w-4 h-4 text-[var(--text-tertiary)]" />
                    </div>
                    <div>
                      <div className="font-mono text-sm font-semibold text-[var(--color-primary)]">
                        {order.id}
                      </div>
                      <span className="font-medium text-[var(--text-primary)]">
                        {order.supplier}
                      </span>
                    </div>
                  </div>
                  {getStatusBadge(order.status)}
                </div>

                {/* Key Info */}
                <div className="flex items-center justify-between">
                  <div className="text-right">
                    <div className="text-lg font-bold text-[var(--text-primary)]">
                      {formatters.currency(order.totalAmount)}
                    </div>
                    <div className="text-xs text-[var(--text-tertiary)]">
                      {order.items?.length} artículos
                    </div>
                  </div>
                  <div className="text-right">
                    <div className="text-sm text-[var(--text-primary)]">
                      {new Date(order.deliveryDate).toLocaleDateString('es-ES')}
                    </div>
                    <div className="text-xs text-[var(--text-tertiary)]">
                      Entrega prevista
                    </div>
                  </div>
                </div>

                {/* Payment Status */}
                <div className="flex items-center justify-between">
                  <div className="text-sm text-[var(--text-secondary)]">
                    Estado del pago:
                  </div>
                  {getPaymentStatusBadge(order.paymentStatus)}
                </div>

                {/* Notes */}
                {order.notes && (
                  <div className="bg-[var(--bg-secondary)] p-2 rounded text-xs text-[var(--text-secondary)] italic">
                    "{order.notes}"
                  </div>
                )}

                {/* Actions */}
                <div className="flex gap-2 pt-2 border-t border-[var(--border-primary)]">
                  <Button 
                    variant="outline" 
                    size="sm" 
                    className="flex-1"
                    onClick={() => console.log('View order', order.id)}
                  >
                    <Eye className="w-4 h-4 mr-1" />
                    Ver
                  </Button>
                  <Button 
                    variant="outline" 
                    size="sm" 
                    className="flex-1"
                    onClick={() => console.log('Edit order', order.id)}
                  >
                    <Edit className="w-4 h-4 mr-1" />
                    Editar
                  </Button>
                </div>
              </div>
            </Card>
          ))}
        </div>
      )}

      {/* Empty State for Purchase Orders */}
      {activeTab === 'orders' && filteredOrders.length === 0 && (
        <div className="text-center py-12">
          <ShoppingCart className="mx-auto h-12 w-12 text-[var(--text-tertiary)] mb-4" />
          <h3 className="text-lg font-medium text-[var(--text-primary)] mb-2">
            No se encontraron órdenes de compra
          </h3>
          <p className="text-[var(--text-secondary)] mb-4">
            Intenta ajustar la búsqueda o crear una nueva orden de compra
          </p>
          <Button onClick={() => console.log('New purchase order')}>
            <Plus className="w-4 h-4 mr-2" />
            Nueva Orden de Compra
          </Button>
        </div>
      )}

      {activeTab === 'suppliers' && (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
          {mockSuppliers.map((supplier) => (
            <Card key={supplier.id} className="p-6">
              <div className="flex items-start justify-between mb-4">
                <div>
                  <h3 className="text-lg font-semibold text-[var(--text-primary)]">{supplier.name}</h3>
                  <p className="text-sm text-[var(--text-secondary)]">{supplier.category}</p>
                </div>
                <Badge variant="green">Activo</Badge>
              </div>

              <div className="space-y-2 mb-4">
                <div className="flex justify-between text-sm">
                  <span className="text-[var(--text-secondary)]">Contacto:</span>
                  <span className="font-medium">{supplier.contact}</span>
                </div>
                <div className="flex justify-between text-sm">
                  <span className="text-[var(--text-secondary)]">Email:</span>
                  <span className="font-medium">{supplier.email}</span>
                </div>
                <div className="flex justify-between text-sm">
                  <span className="text-[var(--text-secondary)]">Teléfono:</span>
                  <span className="font-medium">{supplier.phone}</span>
                </div>
                <div className="flex justify-between text-sm">
                  <span className="text-[var(--text-secondary)]">Ubicación:</span>
                  <span className="font-medium">{supplier.location}</span>
                </div>
              </div>

              <div className="border-t pt-4">
                <div className="grid grid-cols-2 gap-4 mb-4">
                  <div>
                    <p className="text-xs text-[var(--text-secondary)]">Valoración</p>
                    <p className="text-sm font-medium flex items-center">
                      <span className="text-yellow-500">★</span>
                      <span className="ml-1">{supplier.rating}</span>
                    </p>
                  </div>
                  <div>
                    <p className="text-xs text-[var(--text-secondary)]">Pedidos</p>
                    <p className="text-sm font-medium">{supplier.totalOrders}</p>
                  </div>
                </div>

                <div className="grid grid-cols-2 gap-4 mb-4">
                  <div>
                    <p className="text-xs text-[var(--text-secondary)]">Total Gastado</p>
                    <p className="text-sm font-medium">€{supplier.totalSpent.toLocaleString()}</p>
                  </div>
                  <div>
                    <p className="text-xs text-[var(--text-secondary)]">Tiempo Entrega</p>
                    <p className="text-sm font-medium">{supplier.leadTime}</p>
                  </div>
                </div>

                <div className="mb-4">
                  <p className="text-xs text-[var(--text-secondary)]">Condiciones de Pago</p>
                  <p className="text-sm font-medium">{supplier.paymentTerms}</p>
                </div>

                <div className="flex space-x-2">
                  <Button variant="outline" size="sm" className="flex-1">
                    Ver Detalles
                  </Button>
                  <Button size="sm" className="flex-1">
                    Nuevo Pedido
                  </Button>
                </div>
              </div>
            </Card>
          ))}
        </div>
      )}

      {activeTab === 'analytics' && (
        <div className="space-y-6">
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            <Card className="p-6">
              <h3 className="text-lg font-semibold text-[var(--text-primary)] mb-4">Gastos por Mes</h3>
              <div className="h-64 flex items-center justify-center bg-[var(--bg-secondary)] rounded-lg">
                <p className="text-[var(--text-tertiary)]">Gráfico de gastos mensuales</p>
              </div>
            </Card>

            <Card className="p-6">
              <h3 className="text-lg font-semibold text-[var(--text-primary)] mb-4">Top Proveedores</h3>
              <div className="space-y-3">
                {mockSuppliers
                  .sort((a, b) => b.totalSpent - a.totalSpent)
                  .slice(0, 5)
                  .map((supplier, index) => (
                    <div key={supplier.id} className="flex items-center justify-between">
                      <div className="flex items-center">
                        <span className="text-sm font-medium text-[var(--text-tertiary)] w-4">
                          {index + 1}.
                        </span>
                        <span className="ml-3 text-sm text-[var(--text-primary)]">{supplier.name}</span>
                      </div>
                      <span className="text-sm font-medium text-[var(--text-primary)]">
                        €{supplier.totalSpent.toLocaleString()}
                      </span>
                    </div>
                  ))}
              </div>
            </Card>
          </div>

          <Card className="p-6">
            <h3 className="text-lg font-semibold text-[var(--text-primary)] mb-4">Gastos por Categoría</h3>
            <div className="h-64 flex items-center justify-center bg-[var(--bg-secondary)] rounded-lg">
              <p className="text-[var(--text-tertiary)]">Gráfico de gastos por categoría</p>
            </div>
          </Card>
        </div>
      )}
    </div>
  );
};

export default ProcurementPage;