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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import { Play, CheckCircle, AlertTriangle, Calculator, DollarSign, TrendingUp, Database, Target, Award } from 'lucide-react';
import { LotomaniaAlgorithm } from '../utils/lotomaniaAlgorithm';
import { useLotomaniaAPI } from '../hooks/useLotomaniaAPI';
import { LotomaniaGame, LotomaniaResult } from '../types';

interface GameAnalysisResult {
  game: LotomaniaGame;
  numbers: number[];
  matches: number;
  points: number;
  isWinning: boolean;
  prizeValue: number;
  prizeDescription: string;
}

interface FinancialSummary {
  totalCost: number;
  totalPrizes: number;
  netResult: number;
  profitMargin: number;
  winningGames: number;
  pointsDistribution: Record<number, number>;
  prizeDistribution: Record<number, { count: number; totalValue: number }>;
}

export const CompleteSystemAnalysis: React.FC = () => {
  const [algorithm] = useState(() => new LotomaniaAlgorithm());
  const [allGames, setAllGames] = useState<LotomaniaGame[]>([]);
  const [isAnalyzing, setIsAnalyzing] = useState(false);
  const [analysisResults, setAnalysisResults] = useState<GameAnalysisResult[]>([]);
  const [financialSummary, setFinancialSummary] = useState<FinancialSummary | null>(null);
  const [selectedConcurso, setSelectedConcurso] = useState<number | ''>('');
  
  const { latestResult, fetchLatestResult, analyzeGameResult } = useLotomaniaAPI();

  // Verificar e gerar todos os jogos
  useEffect(() => {
    console.log('🚀 Iniciando geração completa de jogos...');
    const startTime = performance.now();
    
    const games = algorithm.generateAllGames();
    const endTime = performance.now();
    
    console.log(`✅ Jogos gerados em ${(endTime - startTime).toFixed(2)}ms`);
    console.log(`📊 Total de jogos: ${games.length}`);
    
    const verticalGames = algorithm.getVerticalGames();
    const horizontalGames = algorithm.getHorizontalGames();
    
    console.log(`🔹 Jogos Verticais: ${verticalGames.length}`);
    console.log(`🔸 Jogos Horizontais: ${horizontalGames.length}`);
    
    setAllGames(games);
  }, [algorithm]);

  // Estatísticas dos jogos
  const gameStats = useMemo(() => {
    if (allGames.length === 0) return null;
    
    const verticalGames = allGames.filter(g => g.type === 'vertical');
    const horizontalGames = allGames.filter(g => g.type === 'horizontal');
    
    const stats = algorithm.getGameStatistics();
    
    return {
      ...stats,
      verticalCount: verticalGames.length,
      horizontalCount: horizontalGames.length,
      isCorrect: verticalGames.length === 252 && horizontalGames.length === 252,
      expectedTotal: 504,
      actualTotal: allGames.length
    };
  }, [allGames, algorithm]);

  // Análise completa contra resultado da Lotomania
  const performCompleteAnalysis = useCallback(async () => {
    if (!latestResult || allGames.length === 0) {
      console.error('❌ Dados insuficientes para análise');
      return;
    }

    setIsAnalyzing(true);
    console.log('🔄 Iniciando análise completa de todos os jogos...');
    console.log(`📊 Analisando ${allGames.length} jogos contra concurso ${latestResult.concurso}`);
    
    const startTime = performance.now();
    const results: GameAnalysisResult[] = [];
    
    for (let i = 0; i < allGames.length; i++) {
      const game = allGames[i];
      const gameNumbers = algorithm.getNumbersFromGame(game);
      const analysis = analyzeGameResult(gameNumbers, latestResult);
      
      // Calcular valor do prêmio baseado nos dados oficiais
      let prizeValue = 0;
      let prizeDescription = 'Sem prêmio';
      
      if (analysis.points >= 15 || analysis.points === 0) {
        const premio = latestResult.premiacoes?.find(p => p.acertos === analysis.points);
        if (premio) {
          prizeValue = premio.valorPremio;
          prizeDescription = premio.descricao;
        }
      }
      
      results.push({
        game,
        numbers: gameNumbers,
        matches: analysis.matches,
        points: analysis.points,
        isWinning: analysis.isWinning,
        prizeValue,
        prizeDescription
      });
      
      // Log progresso a cada 100 jogos
      if ((i + 1) % 100 === 0) {
        console.log(`📈 Analisados ${i + 1}/${allGames.length} jogos...`);
      }
    }
    
    const endTime = performance.now();
    console.log(`✅ Análise completa em ${(endTime - startTime).toFixed(2)}ms`);
    
    setAnalysisResults(results);
    
    // Calcular resumo financeiro
    const GAME_COST = 3.00; // R$ 3,00 por jogo
    const totalCost = allGames.length * GAME_COST;
    const totalPrizes = results.reduce((sum, r) => sum + r.prizeValue, 0);
    const netResult = totalPrizes - totalCost;
    const profitMargin = totalCost > 0 ? (netResult / totalCost) * 100 : 0;
    const winningGames = results.filter(r => r.isWinning).length;
    
    // Distribuição de pontos
    const pointsDistribution: Record<number, number> = {};
    const prizeDistribution: Record<number, { count: number; totalValue: number }> = {};
    
    results.forEach(r => {
      pointsDistribution[r.points] = (pointsDistribution[r.points] || 0) + 1;
      
      if (r.isWinning) {
        if (!prizeDistribution[r.points]) {
          prizeDistribution[r.points] = { count: 0, totalValue: 0 };
        }
        prizeDistribution[r.points].count++;
        prizeDistribution[r.points].totalValue += r.prizeValue;
      }
    });
    
    const financial: FinancialSummary = {
      totalCost,
      totalPrizes,
      netResult,
      profitMargin,
      winningGames,
      pointsDistribution,
      prizeDistribution
    };
    
    setFinancialSummary(financial);
    
    // Log resumo final
    console.log('💰 ===== ANÁLISE FINANCEIRA COMPLETA =====');
    console.log(`💸 Custo Total: R$ ${totalCost.toLocaleString('pt-BR', { minimumFractionDigits: 2 })}`);
    console.log(`🏆 Prêmios Totais: R$ ${totalPrizes.toLocaleString('pt-BR', { minimumFractionDigits: 2 })}`);
    console.log(`📊 Resultado Líquido: R$ ${netResult.toLocaleString('pt-BR', { minimumFractionDigits: 2 })}`);
    console.log(`📈 Margem: ${profitMargin.toFixed(2)}%`);
    console.log(`🎯 Jogos Premiados: ${winningGames}/${allGames.length} (${((winningGames/allGames.length)*100).toFixed(2)}%)`);
    
    setIsAnalyzing(false);
  }, [latestResult, allGames, algorithm, analyzeGameResult]);

  // Auto-executar análise quando resultado estiver disponível
  useEffect(() => {
    if (latestResult && allGames.length > 0 && analysisResults.length === 0) {
      performCompleteAnalysis();
    }
  }, [latestResult, allGames, analysisResults.length, performCompleteAnalysis]);

  // Buscar resultado mais recente ao carregar
  useEffect(() => {
    fetchLatestResult();
  }, [fetchLatestResult]);

  return (
    <div className="space-y-6">
      {/* Header */}
      <div className="bg-gradient-to-r from-blue-600 to-purple-600 text-white p-6 rounded-lg">
        <h2 className="text-2xl font-bold flex items-center gap-2">
          <Database className="w-6 h-6" />
          Análise Completa do Sistema Lotomania
        </h2>
        <p className="text-blue-100 mt-2">
          Verificação completa da lógica, geração de jogos e análise financeira detalhada
        </p>
      </div>

      {/* Verificação do Algoritmo */}
      <div className="bg-white rounded-lg shadow-lg p-6">
        <h3 className="text-xl font-semibold mb-4 flex items-center gap-2">
          <Target className="w-5 h-5 text-blue-600" />
          Verificação do Algoritmo
        </h3>
        
        {gameStats ? (
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
            <div className={`p-4 rounded-lg border-l-4 ${
              gameStats.isCorrect ? 'border-green-500 bg-green-50' : 'border-red-500 bg-red-50'
            }`}>
              <div className="flex items-center gap-2">
                {gameStats.isCorrect ? (
                  <CheckCircle className="w-5 h-5 text-green-600" />
                ) : (
                  <AlertTriangle className="w-5 h-5 text-red-600" />
                )}
                <span className="font-semibold">Status</span>
              </div>
              <p className={`text-sm ${gameStats.isCorrect ? 'text-green-700' : 'text-red-700'}`}>
                {gameStats.isCorrect ? 'Algoritmo Correto ✅' : 'Algoritmo com Problemas ❌'}
              </p>
            </div>
            
            <div className="p-4 rounded-lg border-l-4 border-blue-500 bg-blue-50">
              <div className="flex items-center gap-2">
                <Calculator className="w-5 h-5 text-blue-600" />
                <span className="font-semibold">Total de Jogos</span>
              </div>
              <p className="text-2xl font-bold text-blue-800">
                {gameStats.actualTotal}
              </p>
              <p className="text-sm text-blue-600">
                Esperado: {gameStats.expectedTotal}
              </p>
            </div>
            
            <div className="p-4 rounded-lg border-l-4 border-purple-500 bg-purple-50">
              <div className="flex items-center gap-2">
                <span className="w-5 h-5 bg-purple-600 rounded"></span>
                <span className="font-semibold">Jogos Verticais</span>
              </div>
              <p className="text-2xl font-bold text-purple-800">
                {gameStats.verticalCount}
              </p>
              <p className="text-sm text-purple-600">
                Esperado: 252
              </p>
            </div>
            
            <div className="p-4 rounded-lg border-l-4 border-orange-500 bg-orange-50">
              <div className="flex items-center gap-2">
                <span className="w-5 h-5 bg-orange-600 rounded"></span>
                <span className="font-semibold">Jogos Horizontais</span>
              </div>
              <p className="text-2xl font-bold text-orange-800">
                {gameStats.horizontalCount}
              </p>
              <p className="text-sm text-orange-600">
                Esperado: 252
              </p>
            </div>
          </div>
        ) : (
          <div className="animate-pulse space-y-4">
            <div className="h-4 bg-gray-200 rounded w-1/4"></div>
            <div className="h-20 bg-gray-200 rounded"></div>
          </div>
        )}
      </div>

      {/* Informações do Resultado da Lotomania */}
      {latestResult && (
        <div className="bg-white rounded-lg shadow-lg p-6">
          <h3 className="text-xl font-semibold mb-4 flex items-center gap-2">
            <Award className="w-5 h-5 text-green-600" />
            Resultado Oficial da Lotomania
          </h3>
          
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
            <div className="p-4 bg-green-50 rounded-lg">
              <span className="text-sm text-green-600">Concurso</span>
              <p className="text-2xl font-bold text-green-800">{latestResult.concurso}</p>
            </div>
            
            <div className="p-4 bg-blue-50 rounded-lg">
              <span className="text-sm text-blue-600">Data</span>
              <p className="text-lg font-semibold text-blue-800">{latestResult.data}</p>
            </div>
            
            <div className="p-4 bg-yellow-50 rounded-lg">
              <span className="text-sm text-yellow-600">Valor Arrecadado</span>
              <p className="text-lg font-semibold text-yellow-800">
                R$ {(latestResult.valorArrecadado || 0).toLocaleString('pt-BR')}
              </p>
            </div>
            
            <div className={`p-4 rounded-lg ${latestResult.acumulado ? 'bg-red-50' : 'bg-green-50'}`}>
              <span className={`text-sm ${latestResult.acumulado ? 'text-red-600' : 'text-green-600'}`}>
                Status
              </span>
              <p className={`text-lg font-semibold ${latestResult.acumulado ? 'text-red-800' : 'text-green-800'}`}>
                {latestResult.acumulado ? 'Acumulou' : 'Teve Ganhador'}
              </p>
            </div>
          </div>
          
          {/* Números Sorteados */}
          <div className="mb-4">
            <h4 className="font-semibold mb-2">Números Sorteados:</h4>
            <div className="grid grid-cols-10 gap-2">
              {latestResult.numeros.map((numero, index) => (
                <div
                  key={index}
                  className="bg-blue-600 text-white rounded-lg p-2 text-center font-bold"
                >
                  {numero === 0 ? '00' : numero.toString().padStart(2, '0')}
                </div>
              ))}
            </div>
          </div>
        </div>
      )}

      {/* Controles de Análise */}
      <div className="bg-white rounded-lg shadow-lg p-6">
        <h3 className="text-xl font-semibold mb-4 flex items-center gap-2">
          <Play className="w-5 h-5 text-blue-600" />
          Análise Completa
        </h3>
        
        <div className="flex flex-col md:flex-row gap-4 items-center">
          <button
            onClick={performCompleteAnalysis}
            disabled={isAnalyzing || !latestResult || allGames.length === 0}
            className={`flex items-center gap-2 px-6 py-3 rounded-lg font-semibold transition-all ${
              isAnalyzing || !latestResult || allGames.length === 0
                ? 'bg-gray-400 cursor-not-allowed'
                : 'bg-blue-600 hover:bg-blue-700 text-white shadow-lg hover:shadow-xl'
            }`}
          >
            {isAnalyzing ? (
              <>
                <div className="animate-spin rounded-full h-5 w-5 border-b-2 border-white"></div>
                Analisando todos os {allGames.length} jogos...
              </>
            ) : (
              <>
                <Play className="w-5 h-5" />
                Analisar Todos os {allGames.length} Jogos
              </>
            )}
          </button>
          
          {analysisResults.length > 0 && (
            <div className="text-sm text-gray-600">
              ✅ Análise completa realizada com {analysisResults.length} jogos
            </div>
          )}
        </div>
      </div>

      {/* Resumo Financeiro */}
      {financialSummary && (
        <div className="bg-white rounded-lg shadow-lg p-6">
          <h3 className="text-xl font-semibold mb-4 flex items-center gap-2">
            <DollarSign className="w-5 h-5 text-green-600" />
            Análise Financeira Completa
          </h3>
          
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
            <div className="p-4 bg-red-50 rounded-lg border-l-4 border-red-500">
              <span className="text-sm text-red-600">Custo Total</span>
              <p className="text-2xl font-bold text-red-800">
                R$ {financialSummary.totalCost.toLocaleString('pt-BR', { minimumFractionDigits: 2 })}
              </p>
              <p className="text-sm text-red-600">
                {allGames.length} jogos × R$ 3,00
              </p>
            </div>
            
            <div className="p-4 bg-green-50 rounded-lg border-l-4 border-green-500">
              <span className="text-sm text-green-600">Prêmios Totais</span>
              <p className="text-2xl font-bold text-green-800">
                R$ {financialSummary.totalPrizes.toLocaleString('pt-BR', { minimumFractionDigits: 2 })}
              </p>
              <p className="text-sm text-green-600">
                {financialSummary.winningGames} jogos premiados
              </p>
            </div>
            
            <div className={`p-4 rounded-lg border-l-4 ${
              financialSummary.netResult >= 0 ? 'border-blue-500 bg-blue-50' : 'border-orange-500 bg-orange-50'
            }`}>
              <span className={`text-sm ${
                financialSummary.netResult >= 0 ? 'text-blue-600' : 'text-orange-600'
              }`}>
                Resultado Líquido
              </span>
              <p className={`text-2xl font-bold ${
                financialSummary.netResult >= 0 ? 'text-blue-800' : 'text-orange-800'
              }`}>
                {financialSummary.netResult >= 0 ? '+' : ''}R$ {financialSummary.netResult.toLocaleString('pt-BR', { minimumFractionDigits: 2 })}
              </p>
              <p className={`text-sm ${
                financialSummary.netResult >= 0 ? 'text-blue-600' : 'text-orange-600'
              }`}>
                {financialSummary.profitMargin >= 0 ? 'Lucro' : 'Prejuízo'}: {Math.abs(financialSummary.profitMargin).toFixed(2)}%
              </p>
            </div>
            
            <div className="p-4 bg-purple-50 rounded-lg border-l-4 border-purple-500">
              <span className="text-sm text-purple-600">Taxa de Acerto</span>
              <p className="text-2xl font-bold text-purple-800">
                {((financialSummary.winningGames / allGames.length) * 100).toFixed(2)}%
              </p>
              <p className="text-sm text-purple-600">
                {financialSummary.winningGames} de {allGames.length} jogos
              </p>
            </div>
          </div>
          
          {/* Distribuição de Pontos */}
          <div className="mt-6">
            <h4 className="font-semibold mb-3">Distribuição de Pontos:</h4>
            <div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-8 gap-3">
              {Object.entries(financialSummary.pointsDistribution)
                .sort(([a], [b]) => parseInt(b) - parseInt(a))
                .map(([points, count]) => {
                  const prize = financialSummary.prizeDistribution[parseInt(points)];
                  const isWinning = prize && prize.count > 0;
                  
                  return (
                    <div
                      key={points}
                      className={`p-3 rounded-lg text-center ${
                        isWinning ? 'bg-green-100 border border-green-300' : 'bg-gray-100'
                      }`}
                    >
                      <div className={`font-bold text-lg ${
                        isWinning ? 'text-green-800' : 'text-gray-700'
                      }`}>
                        {points} pts
                      </div>
                      <div className={`text-sm ${
                        isWinning ? 'text-green-600' : 'text-gray-600'
                      }`}>
                        {count} jogos
                      </div>
                      {isWinning && (
                        <div className="text-xs text-green-600 font-semibold">
                          R$ {prize.totalValue.toLocaleString('pt-BR', { maximumFractionDigits: 0 })}
                        </div>
                      )}
                    </div>
                  );
                })}
            </div>
          </div>
        </div>
      )}

      {/* Confirmação do Sistema */}
      <div className="bg-gradient-to-r from-green-500 to-blue-500 text-white p-6 rounded-lg">
        <h3 className="text-xl font-semibold mb-3 flex items-center gap-2">
          <CheckCircle className="w-6 h-6" />
          Confirmação do Sistema
        </h3>
        
        <div className="space-y-2">
          <p className="flex items-center gap-2">
            <span className="w-2 h-2 bg-white rounded-full"></span>
<strong>Algoritmo Verified:</strong> Gera exatamente {gameStats?.actualTotal} jogos conforme especificado
          </p>
          <p className="flex items-center gap-2">
            <span className="w-2 h-2 bg-white rounded-full"></span>
<strong>Jogos Verticais:</strong> {gameStats?.verticalCount} jogos (colunas marcadas)
          </p>
          <p className="flex items-center gap-2">
            <span className="w-2 h-2 bg-white rounded-full"></span>
<strong>Jogos Horizontais:</strong> {gameStats?.horizontalCount} jogos (linhas marcadas)
          </p>
          <p className="flex items-center gap-2">
            <span className="w-2 h-2 bg-white rounded-full"></span>
<strong>Comparação Real:</strong> Cada jogo é comparado com resultado oficial da Caixa
          </p>
          <p className="flex items-center gap-2">
            <span className="w-2 h-2 bg-white rounded-full"></span>
<strong>Análise Financeira:</strong> Custo total vs. prêmios reais obtidos
          </p>
          <p className="flex items-center gap-2">
            <span className="w-2 h-2 bg-white rounded-full"></span>
<strong>Dados Oficiais:</strong> API da Caixa Econômica Federal integrada
          </p>
        </div>
      </div>
    </div>
  );
};

export default CompleteSystemAnalysis;