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 
 
 
 
 
 
 
 
 
1
2
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
503
504
505
506
507
508
509
import { useState } from 'react';
import { Card, CardContent, CardHeader, CardTitle } from './ui/card';
import { Button } from './ui/button';
import { Badge } from './ui/badge';
import { Tabs, TabsContent, TabsList, TabsTrigger } from './ui/tabs';
import { Slider } from './ui/slider';
import { 
  TrendingUp, 
  Brain,
  Target,
  Zap,
  DollarSign,
  Fish,
  Activity
} from 'lucide-react';
import { LineChart, Line, BarChart, Bar, AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Legend } from 'recharts';
import { User, Farm } from '../types';

interface AnalyticsProps {
  user: User;
  selectedFarm: Farm | null;
}

export default function Analytics({ user, selectedFarm }: AnalyticsProps) {
  const [predictionDays, setPredictionDays] = useState([30]);
  const [feedRate, setFeedRate] = useState([3.5]);
  const [temperature, setTemperature] = useState([26]);

  // Growth Prediction Data
  const growthPrediction = [
    { week: 'Week 1', actual: 120, predicted: 122 },
    { week: 'Week 2', actual: 145, predicted: 147 },
    { week: 'Week 3', actual: 175, predicted: 173 },
    { week: 'Week 4', actual: 210, predicted: 208 },
    { week: 'Week 5', actual: 245, predicted: 248 },
    { week: 'Week 6', actual: 285, predicted: 287 },
    { week: 'Week 7', predicted: 328 },
    { week: 'Week 8', predicted: 372 },
  ];

  // Profitability Analysis
  const profitabilityData = [
    { month: 'May', revenue: 38000, costs: 28500, profit: 9500 },
    { month: 'Jun', revenue: 42000, costs: 30200, profit: 11800 },
    { month: 'Jul', revenue: 45000, costs: 31800, profit: 13200 },
    { month: 'Aug', revenue: 48000, costs: 33500, profit: 14500 },
    { month: 'Sep', revenue: 51000, costs: 35100, profit: 15900 },
    { month: 'Oct', revenue: 54000, costs: 36800, profit: 17200 },
  ];

  // FCR Trends
  const fcrTrends = [
    { date: 'Sep 15', fcr: 1.42, target: 1.40 },
    { date: 'Sep 22', fcr: 1.38, target: 1.40 },
    { date: 'Sep 29', fcr: 1.35, target: 1.40 },
    { date: 'Oct 06', fcr: 1.37, target: 1.40 },
    { date: 'Oct 13', fcr: 1.35, target: 1.40 },
    { date: 'Oct 20', fcr: 1.33, target: 1.40 },
  ];

  // KPIs
  const kpis = [
    { label: 'Predicted Weight (30 days)', value: '420g', change: '+47%', trend: 'up' },
    { label: 'Forecasted FCR', value: '1.32', change: '-5%', trend: 'down' },
    { label: 'Survival Rate Prediction', value: '96.8%', change: '+1.2%', trend: 'up' },
    { label: 'Expected ROI', value: '42%', change: '+8%', trend: 'up' },
  ];

  // Scenario simulation results
  const scenarioResults = {
    currentFeed: {
      finalWeight: 420,
      fcr: 1.32,
      cost: 3250,
      profit: 14800
    },
    optimized: {
      finalWeight: 445,
      fcr: 1.28,
      cost: 3180,
      profit: 16200
    }
  };

  return (
    <div className="p-6 space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl">AI Analytics & Predictions</h1>
          <p className="text-gray-600">Data-driven insights for optimal performance</p>
        </div>
        <Badge className="bg-purple-100 text-purple-700">
          <Brain className="w-3 h-3 mr-1" />
          AI-Powered
        </Badge>
      </div>

      {/* Key Performance Indicators */}
      <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
        {kpis.map((kpi, index) => (
          <Card key={index}>
            <CardHeader className="pb-2">
              <CardTitle className="text-sm">{kpi.label}</CardTitle>
            </CardHeader>
            <CardContent>
              <div className="text-2xl">{kpi.value}</div>
              <p className={`text-xs mt-1 flex items-center gap-1 ${
                kpi.trend === 'up' ? 'text-green-600' : 'text-blue-600'
              }`}>
                <TrendingUp className="w-3 h-3" />
                {kpi.change} vs target
              </p>
            </CardContent>
          </Card>
        ))}
      </div>

      <Tabs defaultValue="growth" className="space-y-4">
        <TabsList>
          <TabsTrigger value="growth">Growth Prediction</TabsTrigger>
          <TabsTrigger value="profitability">Profitability</TabsTrigger>
          <TabsTrigger value="scenario">Scenario Simulation</TabsTrigger>
          <TabsTrigger value="performance">Performance Metrics</TabsTrigger>
        </TabsList>

        {/* Growth Prediction */}
        <TabsContent value="growth" className="space-y-4">
          <Card>
            <CardHeader>
              <CardTitle className="text-sm">AI Growth Forecast Model</CardTitle>
              <p className="text-xs text-gray-600">
                Predictions based on historical data, water quality, and feeding patterns
              </p>
            </CardHeader>
            <CardContent>
              <ResponsiveContainer width="100%" height={300}>
                <AreaChart data={growthPrediction}>
                  <defs>
                    <linearGradient id="colorActual" x1="0" y1="0" x2="0" y2="1">
                      <stop offset="5%" stopColor="#3b82f6" stopOpacity={0.8}/>
                      <stop offset="95%" stopColor="#3b82f6" stopOpacity={0}/>
                    </linearGradient>
                    <linearGradient id="colorPredicted" x1="0" y1="0" x2="0" y2="1">
                      <stop offset="5%" stopColor="#10b981" stopOpacity={0.8}/>
                      <stop offset="95%" stopColor="#10b981" stopOpacity={0}/>
                    </linearGradient>
                  </defs>
                  <CartesianGrid strokeDasharray="3 3" />
                  <XAxis dataKey="week" />
                  <YAxis label={{ value: 'Weight (g)', angle: -90, position: 'insideLeft' }} />
                  <Tooltip />
                  <Legend />
                  <Area 
                    type="monotone" 
                    dataKey="actual" 
                    stroke="#3b82f6" 
                    fillOpacity={1} 
                    fill="url(#colorActual)" 
                    name="Actual Weight"
                  />
                  <Area 
                    type="monotone" 
                    dataKey="predicted" 
                    stroke="#10b981" 
                    fillOpacity={1} 
                    fill="url(#colorPredicted)" 
                    strokeDasharray="5 5"
                    name="Predicted Weight"
                  />
                </AreaChart>
              </ResponsiveContainer>
            </CardContent>
          </Card>

          <div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
            <Card className="lg:col-span-2">
              <CardHeader>
                <CardTitle className="text-sm">Model Insights</CardTitle>
              </CardHeader>
              <CardContent className="space-y-3">
                <div className="flex items-start gap-3 p-3 bg-blue-50 rounded-lg">
                  <Brain className="w-5 h-5 text-blue-600 mt-0.5" />
                  <div>
                    <p className="text-sm">Optimal Growth Trajectory</p>
                    <p className="text-xs text-gray-600 mt-1">
                      Current growth rate is 3% above predicted baseline. Fish are responding well to current feeding regime.
                    </p>
                  </div>
                </div>
                <div className="flex items-start gap-3 p-3 bg-green-50 rounded-lg">
                  <Target className="w-5 h-5 text-green-600 mt-0.5" />
                  <div>
                    <p className="text-sm">Harvest Window</p>
                    <p className="text-xs text-gray-600 mt-1">
                      Recommended harvest at 112 days (Week 16) when fish reach optimal market size of 650-700g.
                    </p>
                  </div>
                </div>
                <div className="flex items-start gap-3 p-3 bg-purple-50 rounded-lg">
                  <Zap className="w-5 h-5 text-purple-600 mt-0.5" />
                  <div>
                    <p className="text-sm">Growth Acceleration Opportunity</p>
                    <p className="text-xs text-gray-600 mt-1">
                      Increasing feed protein content by 2% could improve SGR by 0.3% with minimal cost increase.
                    </p>
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card>
              <CardHeader>
                <CardTitle className="text-sm">Prediction Confidence</CardTitle>
              </CardHeader>
              <CardContent className="space-y-4">
                <div>
                  <div className="flex items-center justify-between mb-2">
                    <span className="text-sm">Model Accuracy</span>
                    <span className="text-sm">94.2%</span>
                  </div>
                  <div className="h-2 bg-gray-200 rounded-full overflow-hidden">
                    <div className="h-full bg-green-600" style={{ width: '94.2%' }} />
                  </div>
                </div>
                <div>
                  <div className="flex items-center justify-between mb-2">
                    <span className="text-sm">Data Quality</span>
                    <span className="text-sm">91%</span>
                  </div>
                  <div className="h-2 bg-gray-200 rounded-full overflow-hidden">
                    <div className="h-full bg-blue-600" style={{ width: '91%' }} />
                  </div>
                </div>
                <div>
                  <div className="flex items-center justify-between mb-2">
                    <span className="text-sm">Historical Match</span>
                    <span className="text-sm">88.5%</span>
                  </div>
                  <div className="h-2 bg-gray-200 rounded-full overflow-hidden">
                    <div className="h-full bg-purple-600" style={{ width: '88.5%' }} />
                  </div>
                </div>
                <div className="pt-3 border-t">
                  <p className="text-xs text-gray-600">
                    Predictions are updated every 6 hours based on real-time sensor data and growth sampling.
                  </p>
                </div>
              </CardContent>
            </Card>
          </div>
        </TabsContent>

        {/* Profitability Dashboard */}
        <TabsContent value="profitability" className="space-y-4">
          <div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
            <Card>
              <CardHeader>
                <CardTitle className="text-sm">Revenue vs Cost Trends</CardTitle>
              </CardHeader>
              <CardContent>
                <ResponsiveContainer width="100%" height={300}>
                  <LineChart data={profitabilityData}>
                    <CartesianGrid strokeDasharray="3 3" />
                    <XAxis dataKey="month" />
                    <YAxis />
                    <Tooltip />
                    <Legend />
                    <Line type="monotone" dataKey="revenue" stroke="#10b981" strokeWidth={2} name="Revenue" />
                    <Line type="monotone" dataKey="costs" stroke="#ef4444" strokeWidth={2} name="Costs" />
                    <Line type="monotone" dataKey="profit" stroke="#3b82f6" strokeWidth={2} name="Profit" />
                  </LineChart>
                </ResponsiveContainer>
              </CardContent>
            </Card>

            <Card>
              <CardHeader>
                <CardTitle className="text-sm">Profit Margin Analysis</CardTitle>
              </CardHeader>
              <CardContent>
                <ResponsiveContainer width="100%" height={300}>
                  <BarChart data={profitabilityData}>
                    <CartesianGrid strokeDasharray="3 3" />
                    <XAxis dataKey="month" />
                    <YAxis />
                    <Tooltip />
                    <Bar dataKey="profit" fill="#3b82f6" />
                  </BarChart>
                </ResponsiveContainer>
              </CardContent>
            </Card>
          </div>

          <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm">Projected Annual Revenue</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl">$648,000</div>
                <p className="text-xs text-green-600 mt-1 flex items-center gap-1">
                  <TrendingUp className="w-3 h-3" />
                  +18% vs last year
                </p>
              </CardContent>
            </Card>

            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm">Average Profit Margin</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl">31.8%</div>
                <p className="text-xs text-green-600 mt-1">
                  Above industry average (28%)
                </p>
              </CardContent>
            </Card>

            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm">Break-even Point</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl">Day 98</div>
                <p className="text-xs text-gray-600 mt-1">
                  14 days ahead of schedule
                </p>
              </CardContent>
            </Card>
          </div>
        </TabsContent>

        {/* Scenario Simulation */}
        <TabsContent value="scenario" className="space-y-4">
          <Card>
            <CardHeader>
              <CardTitle className="text-sm">What-If Scenario Planning</CardTitle>
              <p className="text-xs text-gray-600">
                Simulate different conditions to optimize your operations
              </p>
            </CardHeader>
            <CardContent className="space-y-6">
              <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div className="space-y-3">
                  <label className="text-sm">
                    Prediction Timeframe: {predictionDays[0]} days
                  </label>
                  <Slider
                    value={predictionDays}
                    onValueChange={setPredictionDays}
                    min={7}
                    max={90}
                    step={1}
                  />
                </div>

                <div className="space-y-3">
                  <label className="text-sm">
                    Feed Rate: {feedRate[0]}% body weight/day
                  </label>
                  <Slider
                    value={feedRate}
                    onValueChange={setFeedRate}
                    min={2}
                    max={5}
                    step={0.1}
                  />
                </div>

                <div className="space-y-3">
                  <label className="text-sm">
                    Water Temperature: {temperature[0]}°C
                  </label>
                  <Slider
                    value={temperature}
                    onValueChange={setTemperature}
                    min={22}
                    max={30}
                    step={0.5}
                  />
                </div>
              </div>

              <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div className="p-4 border-2 border-gray-200 rounded-lg">
                  <h4 className="text-sm mb-3">Current Settings</h4>
                  <div className="space-y-2">
                    <div className="flex justify-between text-sm">
                      <span className="text-gray-600">Predicted Final Weight</span>
                      <span>{scenarioResults.currentFeed.finalWeight}g</span>
                    </div>
                    <div className="flex justify-between text-sm">
                      <span className="text-gray-600">Expected FCR</span>
                      <span>{scenarioResults.currentFeed.fcr}</span>
                    </div>
                    <div className="flex justify-between text-sm">
                      <span className="text-gray-600">Total Feed Cost</span>
                      <span>${scenarioResults.currentFeed.cost}</span>
                    </div>
                    <div className="flex justify-between text-sm pt-2 border-t">
                      <span>Projected Profit</span>
                      <span className="text-blue-600">${scenarioResults.currentFeed.profit}</span>
                    </div>
                  </div>
                </div>

                <div className="p-4 border-2 border-green-200 bg-green-50 rounded-lg">
                  <h4 className="text-sm mb-3 flex items-center gap-2">
                    <Zap className="w-4 h-4" />
                    Optimized Scenario
                  </h4>
                  <div className="space-y-2">
                    <div className="flex justify-between text-sm">
                      <span className="text-gray-600">Predicted Final Weight</span>
                      <span className="text-green-700">{scenarioResults.optimized.finalWeight}g (+{scenarioResults.optimized.finalWeight - scenarioResults.currentFeed.finalWeight}g)</span>
                    </div>
                    <div className="flex justify-between text-sm">
                      <span className="text-gray-600">Expected FCR</span>
                      <span className="text-green-700">{scenarioResults.optimized.fcr} (-0.04)</span>
                    </div>
                    <div className="flex justify-between text-sm">
                      <span className="text-gray-600">Total Feed Cost</span>
                      <span className="text-green-700">${scenarioResults.optimized.cost} (-$70)</span>
                    </div>
                    <div className="flex justify-between text-sm pt-2 border-t border-green-200">
                      <span>Projected Profit</span>
                      <span className="text-green-700">${scenarioResults.optimized.profit} (+${scenarioResults.optimized.profit - scenarioResults.currentFeed.profit})</span>
                    </div>
                  </div>
                </div>
              </div>

              <Button className="w-full">
                Apply Optimized Settings
              </Button>
            </CardContent>
          </Card>
        </TabsContent>

        {/* Performance Metrics */}
        <TabsContent value="performance" className="space-y-4">
          <Card>
            <CardHeader>
              <CardTitle className="text-sm">Feed Conversion Ratio Trends</CardTitle>
            </CardHeader>
            <CardContent>
              <ResponsiveContainer width="100%" height={300}>
                <LineChart data={fcrTrends}>
                  <CartesianGrid strokeDasharray="3 3" />
                  <XAxis dataKey="date" />
                  <YAxis domain={[1.2, 1.5]} />
                  <Tooltip />
                  <Legend />
                  <Line type="monotone" dataKey="fcr" stroke="#3b82f6" strokeWidth={2} name="Actual FCR" />
                  <Line type="monotone" dataKey="target" stroke="#10b981" strokeWidth={2} strokeDasharray="5 5" name="Target FCR" />
                </LineChart>
              </ResponsiveContainer>
            </CardContent>
          </Card>

          <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm">Performance Score</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl">8.7/10</div>
                <div className="flex gap-1 mt-2">
                  {[...Array(9)].map((_, i) => (
                    <div key={i} className="flex-1 h-2 bg-green-600 rounded-full" />
                  ))}
                  <div className="flex-1 h-2 bg-gray-200 rounded-full" />
                </div>
                <p className="text-xs text-gray-600 mt-2">Excellent performance</p>
              </CardContent>
            </Card>

            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm">Efficiency Rating</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl">92%</div>
                <p className="text-xs text-green-600 mt-1 flex items-center gap-1">
                  <TrendingUp className="w-3 h-3" />
                  Top 10% of farms
                </p>
              </CardContent>
            </Card>

            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm">Sustainability Index</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl">A+</div>
                <p className="text-xs text-gray-600 mt-1">
                  Low environmental impact
                </p>
              </CardContent>
            </Card>
          </div>
        </TabsContent>
      </Tabs>
    </div>
  );
}