import React, { useState } from 'react';
import { Settings, Save, RotateCcw, AlertCircle, Loader } from 'lucide-react';
import { Button, Card } from '../../../../components/ui';
import { PageHeader } from '../../../../components/layout';
import { showToast } from '../../../../utils/toast';
import { useSettings, useUpdateSettings } from '../../../../api/hooks/settings';
import { useCurrentTenant } from '../../../../stores/tenant.store';
import type {
  TenantSettings,
  ProcurementSettings,
  InventorySettings,
  ProductionSettings,
  SupplierSettings,
  POSSettings,
  OrderSettings,
  ReplenishmentSettings,
  SafetyStockSettings,
  MOQSettings,
  SupplierSelectionSettings,
} from '../../../../api/types/settings';
import ProcurementSettingsCard from './cards/ProcurementSettingsCard';
import InventorySettingsCard from './cards/InventorySettingsCard';
import ProductionSettingsCard from './cards/ProductionSettingsCard';
import SupplierSettingsCard from './cards/SupplierSettingsCard';
import POSSettingsCard from './cards/POSSettingsCard';
import OrderSettingsCard from './cards/OrderSettingsCard';
import ReplenishmentSettingsCard from './cards/ReplenishmentSettingsCard';
import SafetyStockSettingsCard from './cards/SafetyStockSettingsCard';
import MOQSettingsCard from './cards/MOQSettingsCard';
import SupplierSelectionSettingsCard from './cards/SupplierSelectionSettingsCard';

const AjustesPage: React.FC = () => {
  
  const currentTenant = useCurrentTenant();
  const tenantId = currentTenant?.id || '';

  const { data: settings, isLoading, error, isFetching } = useSettings(tenantId, {
    enabled: !!tenantId,
    retry: 2,
    staleTime: 5 * 60 * 100,
  });
  
  // Debug logging
  React.useEffect(() => {
    console.log('🔍 AjustesPage - tenantId:', tenantId);
    console.log('🔍 AjustesPage - settings:', settings);
    console.log('🔍 AjustesPage - isLoading:', isLoading);
    console.log('🔍 AjustesPage - isFetching:', isFetching);
    console.log('🔍 AjustesPage - error:', error);
  }, [tenantId, settings, isLoading, isFetching, error]);
  const updateSettingsMutation = useUpdateSettings();

  const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
  const [isSaving, setIsSaving] = useState(false);

  // Local state for each category
  const [procurementSettings, setProcurementSettings] = useState<ProcurementSettings | null>(null);
  const [inventorySettings, setInventorySettings] = useState<InventorySettings | null>(null);
  const [productionSettings, setProductionSettings] = useState<ProductionSettings | null>(null);
  const [supplierSettings, setSupplierSettings] = useState<SupplierSettings | null>(null);
  const [posSettings, setPosSettings] = useState<POSSettings | null>(null);
  const [orderSettings, setOrderSettings] = useState<OrderSettings | null>(null);
  const [replenishmentSettings, setReplenishmentSettings] = useState<ReplenishmentSettings | null>(null);
  const [safetyStockSettings, setSafetyStockSettings] = useState<SafetyStockSettings | null>(null);
  const [moqSettings, setMoqSettings] = useState<MOQSettings | null>(null);
  const [supplierSelectionSettings, setSupplierSelectionSettings] = useState<SupplierSelectionSettings | null>(null);

  // Load settings into local state when data is fetched
  React.useEffect(() => {
    if (settings) {
      setProcurementSettings(settings.procurement_settings);
      setInventorySettings(settings.inventory_settings);
      setProductionSettings(settings.production_settings);
      setSupplierSettings(settings.supplier_settings);
      setPosSettings(settings.pos_settings);
      setOrderSettings(settings.order_settings);
      setReplenishmentSettings(settings.replenishment_settings);
      setSafetyStockSettings(settings.safety_stock_settings);
      setMoqSettings(settings.moq_settings);
      setSupplierSelectionSettings(settings.supplier_selection_settings);
      setHasUnsavedChanges(false);
    }
  }, [settings]);

  const handleSaveAll = async () => {
    if (!tenantId || !procurementSettings || !inventorySettings || !productionSettings ||
        !supplierSettings || !posSettings || !orderSettings || !replenishmentSettings || 
        !safetyStockSettings || !moqSettings || !supplierSelectionSettings) {
      return;
    }

    setIsSaving(true);

    try {
      await updateSettingsMutation.mutateAsync({
        tenantId,
        updates: {
          procurement_settings: procurementSettings,
          inventory_settings: inventorySettings,
          production_settings: productionSettings,
          supplier_settings: supplierSettings,
          pos_settings: posSettings,
          order_settings: orderSettings,
          replenishment_settings: replenishmentSettings,
          safety_stock_settings: safetyStockSettings,
          moq_settings: moqSettings,
          supplier_selection_settings: supplierSelectionSettings,
        },
      });

      setHasUnsavedChanges(false);
      showToast.success('Ajustes guardados correctamente');
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : 'Error desconocido';
      showToast.error(`Error al guardar ajustes: ${errorMessage}`);
    } finally {
      setIsSaving(false);
    }
  };

  const handleResetAll = () => {
    if (settings) {
      setProcurementSettings(settings.procurement_settings);
      setInventorySettings(settings.inventory_settings);
      setProductionSettings(settings.production_settings);
      setSupplierSettings(settings.supplier_settings);
      setPosSettings(settings.pos_settings);
      setOrderSettings(settings.order_settings);
      setReplenishmentSettings(settings.replenishment_settings);
      setSafetyStockSettings(settings.safety_stock_settings);
      setMoqSettings(settings.moq_settings);
      setSupplierSelectionSettings(settings.supplier_selection_settings);
      setHasUnsavedChanges(false);
    }
  };

  const handleCategoryChange = (category: string) => {
    setHasUnsavedChanges(true);
  };

  if (isLoading || !currentTenant) {
    return (
      <div className="p-6 space-y-6">
        <PageHeader
          title="Ajustes"
          description="Configura los parámetros operativos de tu panadería"
        />
        <div className="flex items-center justify-center h-64">
          <Loader className="w-8 h-8 animate-spin text-[var(--color-primary)]" />
          <span className="ml-2 text-[var(--text-secondary)]">Cargando ajustes...</span>
        </div>
      </div>
    );
  }

  if (error) {
    return (
      <div className="p-6 space-y-6">
        <PageHeader
          title="Ajustes"
          description="Error al cargar los ajustes"
        />
        <Card className="p-6">
          <div className="text-red-600">
            Error al cargar los ajustes: {error.message || 'Error desconocido'}
          </div>
        </Card>
      </div>
    );
  }

  return (
    <div className="p-6 space-y-6 pb-32">
      <PageHeader
        title="Ajustes"
        description="Configura los parámetros operativos de tu panadería"
      />

      {/* Top Action Bar */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-2 text-sm">
          <Settings className="w-4 h-4 text-[var(--color-primary)]" />
          <span className="text-[var(--text-secondary)]">
            Ajusta los parámetros según las necesidades de tu negocio
          </span>
        </div>
        <div className="flex gap-2">
          <Button
            variant="outline"
            size="sm"
            onClick={handleResetAll}
            disabled={!hasUnsavedChanges || isSaving}
          >
            <RotateCcw className="w-4 h-4" />
            Restablecer Todo
          </Button>
          <Button
            variant="primary"
            size="sm"
            onClick={handleSaveAll}
            isLoading={isSaving}
            disabled={!hasUnsavedChanges}
            loadingText="Guardando..."
          >
            <Save className="w-4 h-4" />
            Guardar Cambios
          </Button>
        </div>
      </div>

      {/* Settings Categories */}
      <div className="space-y-6">
        {/* Procurement Settings */}
        {procurementSettings && (
          <ProcurementSettingsCard
            settings={procurementSettings}
            onChange={(newSettings) => {
              setProcurementSettings(newSettings);
              handleCategoryChange('procurement');
            }}
            disabled={isSaving}
          />
        )}

        {/* Inventory Settings */}
        {inventorySettings && (
          <InventorySettingsCard
            settings={inventorySettings}
            onChange={(newSettings) => {
              setInventorySettings(newSettings);
              handleCategoryChange('inventory');
            }}
            disabled={isSaving}
          />
        )}

        {/* Production Settings */}
        {productionSettings && (
          <ProductionSettingsCard
            settings={productionSettings}
            onChange={(newSettings) => {
              setProductionSettings(newSettings);
              handleCategoryChange('production');
            }}
            disabled={isSaving}
          />
        )}

        {/* Supplier Settings */}
        {supplierSettings && (
          <SupplierSettingsCard
            settings={supplierSettings}
            onChange={(newSettings) => {
              setSupplierSettings(newSettings);
              handleCategoryChange('supplier');
            }}
            disabled={isSaving}
          />
        )}

        {/* POS Settings */}
        {posSettings && (
          <POSSettingsCard
            settings={posSettings}
            onChange={(newSettings) => {
              setPosSettings(newSettings);
              handleCategoryChange('pos');
            }}
            disabled={isSaving}
          />
        )}

        {/* Order Settings */}
        {orderSettings && (
          <OrderSettingsCard
            settings={orderSettings}
            onChange={(newSettings) => {
              setOrderSettings(newSettings);
              handleCategoryChange('order');
            }}
            disabled={isSaving}
          />
        )}

        {/* Replenishment Settings */}
        {replenishmentSettings && (
          <ReplenishmentSettingsCard
            settings={replenishmentSettings}
            onChange={(newSettings) => {
              setReplenishmentSettings(newSettings);
              handleCategoryChange('replenishment');
            }}
            disabled={isSaving}
          />
        )}

        {/* Safety Stock Settings */}
        {safetyStockSettings && (
          <SafetyStockSettingsCard
            settings={safetyStockSettings}
            onChange={(newSettings) => {
              setSafetyStockSettings(newSettings);
              handleCategoryChange('safety_stock');
            }}
            disabled={isSaving}
          />
        )}

        {/* MOQ Settings */}
        {moqSettings && (
          <MOQSettingsCard
            settings={moqSettings}
            onChange={(newSettings) => {
              setMoqSettings(newSettings);
              handleCategoryChange('moq');
            }}
            disabled={isSaving}
          />
        )}

        {/* Supplier Selection Settings */}
        {supplierSelectionSettings && (
          <SupplierSelectionSettingsCard
            settings={supplierSelectionSettings}
            onChange={(newSettings) => {
              setSupplierSelectionSettings(newSettings);
              handleCategoryChange('supplier_selection');
            }}
            disabled={isSaving}
          />
        )}
      </div>

      {/* Floating Save Banner */}
      {hasUnsavedChanges && (
        <div className="fixed bottom-6 right-6 z-50">
          <Card className="p-4 shadow-lg border-2 border-[var(--color-primary)]">
            <div className="flex items-center gap-3">
              <div className="flex items-center gap-2 text-sm text-[var(--text-secondary)]">
                <AlertCircle className="w-4 h-4 text-yellow-500" />
                Tienes cambios sin guardar
              </div>
              <div className="flex gap-2">
                <Button
                  variant="outline"
                  size="sm"
                  onClick={handleResetAll}
                  disabled={isSaving}
                >
                  <RotateCcw className="w-4 h-4" />
                  Descartar
                </Button>
                <Button
                  variant="primary"
                  size="sm"
                  onClick={handleSaveAll}
                  isLoading={isSaving}
                  loadingText="Guardando..."
                >
                  <Save className="w-4 h-4" />
                  Guardar
                </Button>
              </div>
            </div>
          </Card>
        </div>
      )}
    </div>
  );
};

export default AjustesPage;
