import React, { useState, useMemo } from 'react';
import { Calendar, TrendingUp, AlertTriangle, BarChart3, Settings, Loader, Zap, Brain, Target, Activity, Package } from 'lucide-react';
import { Button, Card, Badge, StatusCard, getStatusColor } from '../../../../components/ui';
import { AnalyticsPageLayout, AnalyticsCard } from '../../../../components/analytics';
import { LoadingSpinner } from '../../../../components/ui';
import { DemandChart } from '../../../../components/domain/forecasting';
import { useTenantForecasts, useCreateSingleForecast } from '../../../../api/hooks/forecasting';
import { useIngredients } from '../../../../api/hooks/inventory';
import { useModels } from '../../../../api/hooks/training';
import { useTenantId } from '../../../../hooks/useTenantId';
import { ForecastResponse } from '../../../../api/types/forecasting';
import { forecastingService } from '../../../../api/services/forecasting';
import { formatters } from '../../../../components/ui/Stats/StatsPresets';

const ForecastingPage: React.FC = () => {
  const [selectedProduct, setSelectedProduct] = useState('');
  const [forecastPeriod, setForecastPeriod] = useState('7');
  const [isGenerating, setIsGenerating] = useState(false);
  const [hasGeneratedForecast, setHasGeneratedForecast] = useState(false);
  const [currentForecastData, setCurrentForecastData] = useState<ForecastResponse[]>([]);

  // Get tenant ID from tenant store
  const tenantId = useTenantId();

  // Calculate date range based on selected period
  const endDate = new Date();
  const startDate = new Date();
  startDate.setDate(startDate.getDate() - parseInt(forecastPeriod));

  // NOTE: We don't need to fetch forecasts from API because we already have them
  // from the multi-day forecast response stored in currentForecastData
  // Keeping this disabled to avoid unnecessary API calls
  const {
    data: forecastsData,
    isLoading: forecastsLoading,
    error: forecastsError
  } = useTenantForecasts(tenantId, {
    start_date: startDate.toISOString().split('T')[0],
    end_date: endDate.toISOString().split('T')[0],
    ...(selectedProduct && { inventory_product_id: selectedProduct }),
    limit: 100
  }, {
    enabled: false  // Disabled - we use currentForecastData from multi-day API response
  });


  // Fetch real inventory data
  const {
    data: ingredientsData,
    isLoading: ingredientsLoading,
    error: ingredientsError
  } = useIngredients(tenantId);

  // Fetch trained models to filter products
  const {
    data: modelsData,
    isLoading: modelsLoading,
    error: modelsError
  } = useModels(tenantId, { active_only: true });

  // Forecast generation mutation
  const createForecastMutation = useCreateSingleForecast({
    onSuccess: (data) => {
      setIsGenerating(false);
      setHasGeneratedForecast(true);
      // Store the generated forecast data locally for immediate display
      setCurrentForecastData([data]);
    },
    onError: (error) => {
      setIsGenerating(false);
      console.error('Error generating forecast:', error);
    },
  });

  // Build products list from ingredients that have trained models
  const products = useMemo(() => {
    if (!ingredientsData || !modelsData) {
      return [];
    }

    // Handle both array and paginated response formats
    const modelsList = Array.isArray(modelsData) ? modelsData : (modelsData.models || modelsData.items || []);

    // Get inventory product IDs that have trained models
    const modelProductIds = new Set(modelsList.map((model: any) => model.inventory_product_id));

    // Filter ingredients to only those with models
    const ingredientsWithModels = ingredientsData.filter(ingredient =>
      modelProductIds.has(ingredient.id)
    );

    return ingredientsWithModels.map(ingredient => ({
      id: ingredient.id,
      name: ingredient.name,
      category: ingredient.category,
      hasModel: true
    }));
  }, [ingredientsData, modelsData]);

  const periods = [
    { value: '7', label: '7 días' },
    { value: '14', label: '14 días' },
    { value: '30', label: '30 días' },
    { value: '90', label: '3 meses' },
  ];

  // Handle forecast generation
  const handleGenerateForecast = async () => {
    if (!tenantId || !selectedProduct) {
      alert('Por favor, selecciona un ingrediente para generar predicciones.');
      return;
    }

    setIsGenerating(true);

    try {
      const today = new Date();
      const forecastRequest = {
        inventory_product_id: selectedProduct,
        forecast_date: today.toISOString().split('T')[0],
        forecast_days: parseInt(forecastPeriod),
        location: 'default',
        confidence_level: 0.8,
      };

      // Use the new multi-day endpoint for all forecasts
      const multiDayResult = await forecastingService.createMultiDayForecast(tenantId, forecastRequest);
      setIsGenerating(false);
      setHasGeneratedForecast(true);
      // Use the forecasts from the multi-day response
      setCurrentForecastData(multiDayResult.forecasts);
    } catch (error) {
      console.error('Failed to generate forecast:', error);
      setIsGenerating(false);
    }
  };

  // Use current forecast data from multi-day API response
  const forecasts = currentForecastData;
  // Separate initial data loading from forecast generation
  const isInitialLoading = ingredientsLoading || modelsLoading;
  const hasError = ingredientsError || modelsError;

  // Calculate metrics from real data
  const totalDemand = forecasts.reduce((sum, f) => sum + f.predicted_demand, 0);
  const averageConfidence = forecasts.length > 0
    ? Math.round((forecasts.reduce((sum, f) => sum + f.confidence_level, 0) / forecasts.length) * 100)
    : 0;

  // Get simplified forecast insights
  const getForecastInsights = (forecasts: ForecastResponse[]) => {
    if (!forecasts || forecasts.length === 0) return [];

    const insights = [];
    const avgConfidence = forecasts.reduce((sum, f) => sum + f.confidence_level, 0) / forecasts.length;

    // Model confidence
    insights.push({
      type: 'model',
      icon: Target,
      title: 'Confianza del Modelo',
      description: `${Math.round(avgConfidence * 100)}%`,
      impact: avgConfidence > 0.8 ? 'positive' : avgConfidence > 0.6 ? 'moderate' : 'high'
    });

    // Algorithm used
    if (forecasts[0]?.algorithm) {
      insights.push({
        type: 'algorithm',
        icon: Brain,
        title: 'Algoritmo',
        description: forecasts[0].algorithm,
        impact: 'info'
      });
    }

    // Forecast period
    insights.push({
      type: 'period',
      icon: Calendar,
      title: 'Período',
      description: `${forecasts.length} días predichos`,
      impact: 'info'
    });

    return insights;
  };

  const currentInsights = getForecastInsights(forecasts);


  // Loading and error states - using project patterns
  // Only show full-page loading for initial data fetch, not for forecast generation
  if (isInitialLoading || !tenantId) {
    return (
      <div className="flex items-center justify-center min-h-64">
        <LoadingSpinner text="Cargando datos de predicción..." />
      </div>
    );
  }

  if (hasError) {
    return (
      <div className="text-center py-12">
        <TrendingUp className="mx-auto h-12 w-12 text-[var(--color-error)] mb-4" />
        <h3 className="text-lg font-medium text-[var(--text-primary)] mb-2">
          Error al cargar datos
        </h3>
        <p className="text-[var(--text-secondary)] mb-4">
          {forecastsError?.message || ingredientsError?.message || modelsError?.message || 'Ha ocurrido un error inesperado'}
        </p>
        <Button onClick={() => window.location.reload()}>
          Reintentar
        </Button>
      </div>
    );
  }

  return (
    <AnalyticsPageLayout
      title="Predicción de Demanda"
      description="Sistema inteligente de predicción de demanda basado en IA"
      subscriptionLoading={false}
      hasAccess={true}
      dataLoading={isInitialLoading}
      stats={[
        {
          title: 'Ingredientes con Modelos',
          value: products.length,
          variant: 'default' as const,
          icon: Brain,
        },
        {
          title: 'Predicciones Generadas',
          value: forecasts.length,
          variant: 'info' as const,
          icon: TrendingUp,
        },
        {
          title: 'Confianza Promedio',
          value: `${averageConfidence}%`,
          variant: 'success' as const,
          icon: Target,
        },
        {
          title: 'Demanda Total',
          value: formatters.number(Math.round(totalDemand)),
          variant: 'warning' as const,
          icon: BarChart3,
        },
      ]}
      statsColumns={4}
      showMobileNotice={true}
    >
      <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {/* Ingredient Selection Section */}
        <div className="lg:col-span-2 space-y-6">
          {/* Ingredients Grid - Similar to POSPage products */}
          <Card className="p-6">
            <h3 className="text-lg font-semibold mb-4 flex items-center text-[var(--text-primary)]">
              <Brain className="w-5 h-5 mr-2 text-[var(--color-primary)]" />
              Ingredientes Disponibles ({products.length})
            </h3>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {products.map(product => {
                const isSelected = selectedProduct === product.id;

                const getStatusConfig = () => {
                  if (isSelected) {
                    return {
                      color: getStatusColor('completed'),
                      text: 'Seleccionado',
                      icon: Target,
                      isCritical: false,
                      isHighlight: true
                    };
                  } else {
                    return {
                      color: getStatusColor('pending'),
                      text: 'Disponible',
                      icon: Brain,
                      isCritical: false,
                      isHighlight: false
                    };
                  }
                };

                return (
                  <StatusCard
                    key={product.id}
                    id={product.id}
                    statusIndicator={getStatusConfig()}
                    title={product.name}
                    subtitle={product.category}
                    primaryValue="Modelo IA"
                    primaryValueLabel="entrenado"
                    actions={[
                      {
                        label: isSelected ? 'Seleccionado' : 'Seleccionar',
                        icon: isSelected ? Target : Zap,
                        variant: isSelected ? 'success' : 'primary',
                        priority: 'primary',
                        disabled: isGenerating,
                        onClick: () => setSelectedProduct(product.id)
                      }
                    ]}
                  />
                );
              })}
            </div>

            {/* Empty State */}
            {products.length === 0 && (
              <div className="text-center py-12">
                <Brain 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 hay modelos entrenados
                </h3>
                <p className="text-[var(--text-secondary)] mb-4">
                  Para generar predicciones, necesitas modelos IA entrenados para tus ingredientes
                </p>
                <Button
                  onClick={() => window.location.href = '/app/database/models'}
                  variant="primary"
                >
                  <Settings className="w-4 h-4 mr-2" />
                  Entrenar Modelos
                </Button>
              </div>
            )}
          </Card>
        </div>

        {/* Configuration and Generation Section */}
        <div className="space-y-6">
          {/* Period Selection */}
          <Card className="p-6">
            <h3 className="text-lg font-semibold mb-4 flex items-center text-[var(--text-primary)]">
              <Calendar className="w-5 h-5 mr-2 text-[var(--color-info)]" />
              Configuración
            </h3>
            <div className="space-y-4">
              <div>
                <label className="block text-sm font-medium text-[var(--text-secondary)] mb-2">
                  Período de Predicción
                </label>
                <select
                  value={forecastPeriod}
                  onChange={(e) => setForecastPeriod(e.target.value)}
                  className="w-full px-4 py-3 border border-[var(--border-secondary)] rounded-lg bg-[var(--bg-primary)] text-[var(--text-primary)] focus:ring-2 focus:ring-[var(--color-primary)] focus:border-transparent transition-all"
                  disabled={isGenerating}
                >
                  {periods.map(period => (
                    <option key={period.value} value={period.value}>{period.label}</option>
                  ))}
                </select>
              </div>

              {selectedProduct && (
                <div className="p-4 bg-gradient-to-br from-[var(--color-primary)]/10 to-[var(--color-primary-light)]/5 rounded-lg border border-[var(--color-primary)]/20">
                  <div className="flex items-center gap-2 mb-1">
                    <Package className="w-4 h-4 text-[var(--color-primary)]" />
                    <p className="text-sm font-semibold text-[var(--text-primary)]">
                      {products.find(p => p.id === selectedProduct)?.name}
                    </p>
                  </div>
                  <p className="text-xs text-[var(--text-secondary)] ml-6">
                    Predicción para {forecastPeriod} días
                  </p>
                </div>
              )}
            </div>
          </Card>

          {/* Generate Forecast */}
          <Card className="p-6 bg-gradient-to-br from-[var(--bg-primary)] to-[var(--bg-secondary)]">
            <h3 className="text-lg font-semibold mb-4 flex items-center text-[var(--text-primary)]">
              <Zap className="w-5 h-5 mr-2 text-[var(--color-warning)]" />
              Generar Predicción
            </h3>
            <Button
              onClick={handleGenerateForecast}
              disabled={!selectedProduct || !forecastPeriod || isGenerating}
              className="w-full"
              size="lg"
            >
              {isGenerating ? (
                <>
                  <Loader className="w-5 h-5 mr-2 animate-spin" />
                  Generando Predicción...
                </>
              ) : (
                <>
                  <Zap className="w-5 h-5 mr-2" />
                  Generar Predicción IA
                </>
              )}
            </Button>

            {!selectedProduct && (
              <div className="mt-3 p-3 bg-blue-50 dark:bg-blue-900/20 rounded-lg">
                <p className="text-xs text-[var(--color-info)] text-center flex items-center justify-center gap-2">
                  <AlertTriangle className="w-4 h-4" />
                  Selecciona un ingrediente para continuar
                </p>
              </div>
            )}
          </Card>
        </div>
      </div>

      {/* Results Section */}
      {hasGeneratedForecast && forecasts.length > 0 && (
        <div className="space-y-6">
          {/* Results Header */}
          <Card className="p-6 bg-gradient-to-br from-[var(--bg-primary)] to-[var(--bg-secondary)]">
            <div className="flex items-center justify-between mb-6">
              <div>
                <h3 className="text-2xl font-bold text-[var(--text-primary)] flex items-center gap-2">
                  <TrendingUp className="w-6 h-6 text-[var(--color-success)]" />
                  Resultados de Predicción
                </h3>
                <p className="text-sm text-[var(--text-secondary)] mt-1">
                  {products.find(p => p.id === selectedProduct)?.name} • {forecastPeriod} días
                </p>
              </div>
            </div>

            {/* Chart View */}
            <div className="min-h-96 p-4 bg-[var(--bg-primary)] rounded-lg">
              <DemandChart
                data={forecasts}
                product={selectedProduct}
                period={forecastPeriod}
                loading={isGenerating}
                error={hasError ? 'Error al cargar las predicciones' : null}
                height={450}
                title=""
              />
            </div>
          </Card>

          {/* Insights */}
          {currentInsights.length > 0 && (
            <Card className="p-6">
              <h3 className="text-lg font-semibold text-[var(--text-primary)] mb-4 flex items-center">
                <Activity className="w-5 h-5 mr-2 text-[var(--color-info)]" />
                Factores de Influencia
              </h3>
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                {currentInsights.map((insight, index) => {
                  const IconComponent = insight.icon;
                  return (
                    <div key={index} className="flex items-start space-x-3 p-4 bg-[var(--bg-secondary)] rounded-lg hover:bg-[var(--bg-tertiary)] transition-all border border-transparent hover:border-[var(--border-primary)]">
                      <div className={`p-2 rounded-lg ${
                        insight.impact === 'positive' ? 'bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300' :
                        insight.impact === 'high' ? 'bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300' :
                        insight.impact === 'moderate' ? 'bg-orange-100 dark:bg-orange-900/30 text-orange-700 dark:text-orange-300' :
                        'bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-300'
                      }`}>
                        <IconComponent className="w-5 h-5" />
                      </div>
                      <div className="flex-1">
                        <p className="text-sm font-semibold text-[var(--text-primary)]">{insight.title}</p>
                        <p className="text-xs text-[var(--text-secondary)] mt-1">{insight.description}</p>
                      </div>
                    </div>
                  );
                })}
              </div>
            </Card>
          )}
        </div>
      )}

      {/* Help Section - Only when no models available */}
      {!isInitialLoading && !hasError && products.length === 0 && (
        <Card className="p-6 text-center">
          <div className="py-8">
            <Brain className="h-12 w-12 text-[var(--color-info)] mx-auto mb-4" />
            <h3 className="text-lg font-medium text-[var(--text-primary)] mb-2">
              No hay modelos entrenados
            </h3>
            <p className="text-[var(--text-secondary)] mb-6">
              Para generar predicciones, necesitas entrenar modelos de IA para tus ingredientes.
            </p>
            <Button
              onClick={() => window.location.href = '/app/database/models'}
              variant="primary"
            >
              <Settings className="w-4 h-4 mr-2" />
              Entrenar Modelos IA
            </Button>
          </div>
        </Card>
      )}
    </AnalyticsPageLayout>
  );
};

export default ForecastingPage;