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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a4e327
ce4e1e4
 
 
 
 
 
 
 
 
 
 
1a4e327
 
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
<!DOCTYPE html>
<html>
<head>
    <title>AgriChain Backend Documentation</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        pre {
            background-color: #f8f8f8;
            border-radius: 5px;
            padding: 15px;
            overflow-x: auto;
        }
        .endpoint {
            border-left: 4px solid #10b981;
            padding-left: 1rem;
            margin-bottom: 2rem;
        }
    </style>
</head>
<body class="bg-gray-50 p-8">
    <div class="max-w-6xl mx-auto bg-white rounded-lg shadow-lg p-8">
        <div class="flex items-center mb-8">
            <i class="fas fa-leaf text-emerald-500 text-3xl mr-3"></i>
            <h1 class="text-3xl font-bold text-gray-800">AgriChain Backend API</h1>
        </div>
        
        <div class="mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">System Overview</h2>
            <p class="text-gray-600 mb-4">
                The AgriChain backend provides RESTful APIs for the smart farming dashboard, handling IoT device data, 
                blockchain transactions, AI predictions, and user management.
            </p>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
                <div class="bg-emerald-50 p-4 rounded-lg">
                    <h3 class="font-medium text-emerald-800 mb-2">Tech Stack</h3>
                    <ul class="text-sm text-gray-700 space-y-1">
                        <li>Node.js v18+</li>
                        <li>Express.js</li>
                        <li>MongoDB (with Mongoose)</li>
                        <li>JWT Authentication</li>
                        <li>Web3.js for blockchain</li>
                        <li>TensorFlow.js for AI</li>
                    </ul>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg">
                    <h3 class="font-medium text-blue-800 mb-2">Key Features</h3>
                    <ul class="text-sm text-gray-700 space-y-1">
                        <li>Real-time IoT data processing</li>
                        <li>Blockchain data integrity</li>
                        <li>AI prediction models</li>
                        <li>User authentication</li>
                        <li>Cloud integration</li>
                    </ul>
                </div>
                <div class="bg-purple-50 p-4 rounded-lg">
                    <h3 class="font-medium text-purple-800 mb-2">Deployment</h3>
                    <ul class="text-sm text-gray-700 space-y-1">
                        <li>Docker containerization</li>
                        <li>Kubernetes for orchestration</li>
                        <li>AWS ECS/EKS</li>
                        <li>CI/CD with GitHub Actions</li>
                    </ul>
                </div>
            </div>
        </div>
        
        <div class="mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">Installation</h2>
            <pre><code class="language-bash">
# Clone the repository
git clone https://github.com/agrichain/backend.git
cd backend

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your configuration

# Start the development server
npm run dev

# For production
npm run build
npm start
            </code></pre>
        </div>
        
        <div class="mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">API Endpoints</h2>
            
            <div class="endpoint">
                <h3 class="text-lg font-medium text-gray-800 mb-2">Authentication</h3>
                <div class="mb-4">
                    <span class="bg-green-100 text-green-800 text-xs font-medium px-2.5 py-0.5 rounded">POST</span>
                    <span class="ml-2 font-mono">/api/auth/register</span>
                </div>
                <pre><code class="language-javascript">
// Request Body
{
    "name": "John Farmer",
    "email": "john@example.com",
    "password": "securepassword123",
    "farmName": "Green Valley Farms"
}

// Response
{
    "success": true,
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "user": {
        "id": "507f1f77bcf86cd799439011",
        "name": "John Farmer",
        "email": "john@example.com",
        "farmName": "Green Valley Farms"
    }
}
                </code></pre>
                
                <div class="mb-4 mt-6">
                    <span class="bg-green-100 text-green-800 text-xs font-medium px-2.5 py-0.5 rounded">POST</span>
                    <span class="ml-2 font-mono">/api/auth/login</span>
                </div>
                <pre><code class="language-javascript">
// Request Body
{
    "email": "john@example.com",
    "password": "securepassword123"
}

// Response (same structure as register)
                </code></pre>
            </div>
            
            <div class="endpoint">
                <h3 class="text-lg font-medium text-gray-800 mb-2">IoT Device Management</h3>
                <div class="mb-4">
                    <span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded">GET</span>
                    <span class="ml-2 font-mono">/api/iot/devices</span>
                </div>
                <pre><code class="language-javascript">
// Response
{
    "success": true,
    "devices": [
        {
            "id": "60d5ec9cfb9d8b3a48fe9a12",
            "name": "Soil Moisture Sensor - Field A",
            "type": "moisture",
            "status": "online",
            "batteryLevel": 78,
            "signalStrength": 92,
            "lastReading": {
                "value": 72,
                "timestamp": "2023-06-15T08:30:45.000Z"
            },
            "location": {
                "field": "A",
                "coordinates": [34.0522, -118.2437]
            }
        },
        // ... more devices
    ]
}
                </code></pre>
                
                <div class="mb-4 mt-6">
                    <span class="bg-yellow-100 text-yellow-800 text-xs font-medium px-2.5 py-0.5 rounded">POST</span>
                    <span class="ml-2 font-mono">/api/iot/data</span>
                    <span class="text-sm text-gray-500 ml-2">(for IoT devices to send data)</span>
                </div>
                <pre><code class="language-javascript">
// Request Body
{
    "deviceId": "60d5ec9cfb9d8b3a48fe9a12",
    "secretKey": "device-secret-key",
    "readings": [
        {
            "type": "moisture",
            "value": 72,
            "timestamp": "2023-06-15T08:30:45.000Z"
        }
    ]
}

// Response
{
    "success": true,
    "message": "Data received and stored",
    "blockchainHash": "a3f8e2d4b5c71f9e8a0b6d5c4f3e2a1b"
}
                </code></pre>
            </div>
            
            <div class="endpoint">
                <h3 class="text-lg font-medium text-gray-800 mb-2">Blockchain</h3>
                <div class="mb-4">
                    <span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded">GET</span>
                    <span class="ml-2 font-mono">/api/blockchain/blocks</span>
                </div>
                <pre><code class="language-javascript">
// Response
{
    "success": true,
    "blocks": [
        {
            "index": 4892,
            "timestamp": "2023-06-15T08:30:45.000Z",
            "data": {
                "deviceId": "60d5ec9cfb9d8b3a48fe9a12",
                "readingType": "moisture",
                "value": 72,
                "field": "A"
            },
            "previousHash": "b5c71f9e8a0b6d5c4f3e2a1ba3f8e2d",
            "hash": "a3f8e2d4b5c71f9e8a0b6d5c4f3e2a1b"
        },
        // ... more blocks
    ]
}
                </code></pre>
                
                <div class="mb-4 mt-6">
                    <span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded">GET</span>
                    <span class="ml-2 font-mono">/api/blockchain/validate</span>
                </div>
                <pre><code class="language-javascript">
// Response
{
    "success": true,
    "valid": true,
    "invalidBlocks": [],
    "message": "Blockchain integrity verified"
}
                </code></pre>
            </div>
            
            <div class="endpoint">
                <h3 class="text-lg font-medium text-gray-800 mb-2">AI Predictions</h3>
                <div class="mb-4">
                    <span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded">GET</span>
                    <span class="ml-2 font-mono">/api/ai/predictions</span>
                </div>
                <pre><code class="language-javascript">
// Response
{
    "success": true,
    "predictions": [
        {
            "type": "irrigation",
            "field": "A",
            "prediction": "Next irrigation in 8 hours",
            "confidence": 0.92,
            "recommendation": "Delay watering until evening to reduce evaporation"
        },
        {
            "type": "pest",
            "field": "B",
            "prediction": "Low pest risk (15%)",
            "confidence": 0.87,
            "recommendation": "Monitor for corn borer, no treatment needed yet"
        },
        {
            "type": "harvest",
            "field": "C",
            "prediction": "Optimal harvest window: July 15-18",
            "confidence": 0.85,
            "recommendation": "Prepare workforce and storage for peak yield"
        }
    ]
}
                </code></pre>
                
                <div class="mb-4 mt-6">
                    <span class="bg-yellow-100 text-yellow-800 text-xs font-medium px-2.5 py-0.5 rounded">POST</span>
                    <span class="ml-2 font-mono">/api/ai/train</span>
                    <span class="text-sm text-gray-500 ml-2">(Admin only - retrain models)</span>
                </div>
                <pre><code class="language-javascript">
// Response
{
    "success": true,
    "message": "AI models retrained successfully",
    "newAccuracy": {
        "irrigation": 0.93,
        "pest": 0.88,
        "harvest": 0.86
    }
}
                </code></pre>
            </div>
            
            <div class="endpoint">
                <h3 class="text-lg font-medium text-gray-800 mb-2">Cloud Integration</h3>
                <div class="mb-4">
                    <span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded">GET</span>
                    <span class="ml-2 font-mono">/api/cloud/status</span>
                </div>
                <pre><code class="language-javascript">
// Response
{
    "success": true,
    "services": [
        {
            "provider": "AWS IoT",
            "status": "connected",
            "metrics": {
                "messages": 1248,
                "storage": 2.8,
                "rulesProcessed": 56
            }
        },
        {
            "provider": "Azure Data Lake",
            "status": "syncing",
            "metrics": {
                "dataStored": 4.2,
                "lastSync": "2023-06-15T08:28:12.000Z",
                "analyticsJobs": 12
            }
        },
        {
            "provider": "Google BigQuery",
            "status": "processing",
            "metrics": {
                "queries": 84,
                "mlModels": 3,
                "storage": 1.5
            }
        }
    ]
}
                </code></pre>
            </div>
        </div>
        
        <div class="mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">Database Schema</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2">User</h3>
                    <pre><code class="language-javascript">
{
    name: String,
    email: { type: String, unique: true },
    password: String,
    farmName: String,
    role: { type: String, enum: ['user', 'admin'], default: 'user' },
    createdAt: { type: Date, default: Date.now }
}
                    </code></pre>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2">Device</h3>
                    <pre><code class="language-javascript">
{
    name: String,
    type: { type: String, enum: ['moisture', 'temperature', 'humidity', 'light', 'ph', 'co2'] },
    status: { type: String, enum: ['online', 'offline', 'maintenance'] },
    batteryLevel: Number,
    signalStrength: Number,
    secretKey: String, // For authentication
    userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    location: {
        field: String,
        coordinates: [Number] // [longitude, latitude]
    }
}
                    </code></pre>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2">Reading</h3>
                    <pre><code class="language-javascript">
{
    deviceId: { type: mongoose.Schema.Types.ObjectId, ref: 'Device' },
    type: String,
    value: Number,
    timestamp: { type: Date, default: Date.now },
    field: String,
    blockchainHash: String
}
                    </code></pre>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2">Block</h3>
                    <pre><code class="language-javascript">
{
    index: Number,
    timestamp: { type: Date, default: Date.now },
    data: {
        deviceId: { type: mongoose.Schema.Types.ObjectId, ref: 'Device' },
        readingType: String,
        value: Number,
        field: String
    },
    previousHash: String,
    hash: String
}
                    </code></pre>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2">Prediction</h3>
                    <pre><code class="language-javascript">
{
    type: { type: String, enum: ['irrigation', 'pest', 'harvest'] },
    field: String,
    prediction: String,
    confidence: Number,
    recommendation: String,
    userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    createdAt: { type: Date, default: Date.now }
}
                    </code></pre>
                </div>
                
                <div class="bg-gray-50 p-4 rounded-lg">
                    <h3 class="font-medium text-gray-800 mb-2">CloudService</h3>
                    <pre><code class="language-javascript">
{
    provider: { type: String, enum: ['aws', 'azure', 'google'] },
    status: String,
    metrics: {
        messages: Number,
        storage: Number,
        rulesProcessed: Number,
        // ... other metrics
    },
    lastSync: Date,
    userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
}
                    </code></pre>
                </div>
            </div>
        </div>
        
        <div class="mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">Example Server Code</h2>
            
            <div class="mb-6">
                <h3 class="font-medium text-gray-800 mb-2">app.js (Main Server File)</h3>
                <pre><code class="language-javascript">
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const { Web3 } = require('web3');
const tf = require('@tensorflow/tfjs-node');

const app = express();

// Database connection
mongoose.connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('MongoDB connection error:', err));

// Middleware
app.use(cors());
app.use(helmet());
app.use(express.json());

// Rate limiting
const limiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // limit each IP to 100 requests per windowMs
});
app.use(limiter);

// Blockchain setup
const web3 = new Web3(process.env.BLOCKCHAIN_NODE_URL);
const contract = new web3.eth.Contract(
    JSON.parse(process.env.CONTRACT_ABI),
    process.env.CONTRACT_ADDRESS
);

// Routes
app.use('/api/auth', require('./routes/auth'));
app.use('/api/iot', require('./routes/iot'));
app.use('/api/blockchain', require('./routes/blockchain'));
app.use('/api/ai', require('./routes/ai'));
app.use('/api/cloud', require('./routes/cloud'));

// Error handling
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).json({ 
        success: false, 
        message: 'Internal server error' 
    });
});

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});

module.exports = app;
                </code></pre>
            </div>
            
            <div class="mb-6">
                <h3 class="font-medium text-gray-800 mb-2">routes/iot.js (Example Route)</h3>
                <pre><code class="language-javascript">
const express = require('express');
const router = express.Router();
const auth = require('../middleware/auth');
const Device = require('../models/Device');
const Reading = require('../models/Reading');
const { addBlock } = require('../utils/blockchain');

// @route   GET api/iot/devices
// @desc    Get all user's IoT devices
// @access  Private
router.get('/devices', auth, async (req, res) => {
    try {
        const devices = await Device.find({ userId: req.user.id });
        
        // Get latest readings for each device
        const devicesWithReadings = await Promise.all(devices.map(async device => {
            const latestReading = await Reading.findOne({ deviceId: device._id })
                .sort({ timestamp: -1 })
                .limit(1);
                
            return {
                ...device.toObject(),
                lastReading: latestReading || null
            };
        }));
        
        res.json({ success: true, devices: devicesWithReadings });
    } catch (err) {
        console.error(err.message);
        res.status(500).json({ success: false, message: 'Server error' });
    }
});

// @route   POST api/iot/data
// @desc    Receive data from IoT device
// @access  Private (using device secret key)
router.post('/data', async (req, res) => {
    const { deviceId, secretKey, readings } = req.body;
    
    try {
        // Verify device
        const device = await Device.findOne({ _id: deviceId, secretKey });
        if (!device) {
            return res.status(401).json({ 
                success: false, 
                message: 'Invalid device credentials' 
            });
        }
        
        // Save readings
        const savedReadings = await Promise.all(readings.map(async reading => {
            const newReading = new Reading({
                deviceId,
                type: reading.type,
                value: reading.value,
                timestamp: reading.timestamp || Date.now(),
                field: device.location.field
            });
            
            // Add to blockchain
            const blockData = {
                deviceId,
                readingType: reading.type,
                value: reading.value,
                field: device.location.field
            };
            
            const hash = await addBlock(blockData);
            newReading.blockchainHash = hash;
            
            return await newReading.save();
        }));
        
        // Update device status and battery if provided
        if (readings[0].batteryLevel) {
            device.batteryLevel = readings[0].batteryLevel;
            await device.save();
        }
        
        res.json({ 
            success: true, 
            message: 'Data received and stored',
            blockchainHash: savedReadings[0].blockchainHash 
        });
    } catch (err) {
        console.error(err.message);
        res.status(500).json({ success: false, message: 'Server error' });
    }
});

module.exports = router;
                </code></pre>
            </div>
            
            <div class="mb-6">
                <h3 class="font-medium text-gray-800 mb-2">utils/blockchain.js</h3>
                <pre><code class="language-javascript">
const Block = require('../models/Block');
const crypto = require('crypto');

// Simple blockchain implementation (in production would use Ethereum or similar)
class Blockchain {
    constructor() {
        this.chain = [];
        this.initializeChain();
    }
    
    async initializeChain() {
        // Load existing chain from database
        const blocks = await Block.find().sort({ index: 1 });
        this.chain = blocks;
        
        // If no blocks, create genesis block
        if (this.chain.length === 0) {
            await this.createGenesisBlock();
        }
    }
    
    async createGenesisBlock() {
        const genesisBlock = new Block({
            index: 0,
            timestamp: Date.now(),
            data: { message: "Genesis block" },
            previousHash: "0",
            hash: this.calculateHash(0, "0", Date.now(), { message: "Genesis block" })
        });
        
        await genesisBlock.save();
        this.chain.push(genesisBlock);
    }
    
    calculateHash(index, previousHash, timestamp, data) {
        return crypto
            .createHash('sha256')
            .update(index + previousHash + timestamp + JSON.stringify(data))
            .digest('hex');
    }
    
    async addBlock(data) {
        const previousBlock = this.chain[this.chain.length - 1];
        const newIndex = previousBlock.index + 1;
        const newTimestamp = Date.now();
        const newHash = this.calculateHash(newIndex, previousBlock.hash, newTimestamp, data);
        
        const newBlock = new Block({
            index: newIndex,
            timestamp: newTimestamp,
            data,
            previousHash: previousBlock.hash,
            hash: newHash
        });
        
        await newBlock.save();
        this.chain.push(newBlock);
        
        return newHash;
    }
    
    async validateChain() {
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
            
            // Check hash
            if (currentBlock.hash !== this.calculateHash(
                currentBlock.index,
                currentBlock.previousHash,
                currentBlock.timestamp,
                currentBlock.data
            )) {
                return false;
            }
            
            // Check previous hash
            if (currentBlock.previousHash !== previousBlock.hash) {
                return false;
            }
        }
        
        return true;
    }
}

const blockchain = new Blockchain();

// Public functions
const addBlock = async (data) => {
    return await blockchain.addBlock(data);
};

const validateChain = async () => {
    return await blockchain.validateChain();
};

const getBlocks = async () => {
    return blockchain.chain;
};

module.exports = {
    addBlock,
    validateChain,
    getBlocks
};
                </code></pre>
            </div>
        </div>
        
        <div class="mb-8">
            <h2 class="text-xl font-semibold text-gray-800 mb-4">Deployment</h2>
            
            <div class="mb-6">
                <h3 class="font-medium text-gray-800 mb-2">Dockerfile</h3>
                <pre><code class="language-dockerfile">
# Use official Node.js image
FROM node:18-alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Build the app
RUN npm run build

# Expose the app port
EXPOSE 5000

# Start the app
CMD ["npm", "start"]
                </code></pre>
            </div>
            
            <div class="mb-6">
                <h3 class="font-medium text-gray-800 mb-2">docker-compose.yml</h3>
                <pre><code class="language-yaml">
version: '3.8'

services:
  app:
    build: .
    ports:
      - "5000:5000"
    environment:
      - NODE_ENV=production
      - MONGODB_URI=mongodb://mongo:27017/agrichain
      - JWT_SECRET=your_jwt_secret
    depends_on:
      - mongo
    restart: unless-stopped

  mongo:
    image: mongo:6.0
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=example

volumes:
  mongodb_data:
                </code></pre>
            </div>
            
            <div class="mb-6">
                <h3 class="font-medium text-gray-800 mb-2">Kubernetes Deployment</h3>
                <pre><code class="language-yaml">
apiVersion: apps/v1
kind: Deployment
metadata:
  name: agrichain-backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: agrichain-backend
  template:
    metadata:
      labels:
        app: agrichain-backend
    spec:
      containers:
      - name: app
        image: your-registry/agrichain-backend:latest
        ports:
        - containerPort: 5000
        env:
        - name: NODE_ENV
          value: production
        - name: MONGODB_URI
          value: "mongodb://agrichain-mongo:27017/agrichain"
        - name: JWT_SECRET
          valueFrom:
            secretKeyRef:
              name: agrichain-secrets
              key: jwt-secret
      - name: mongo
        image: mongo:6.0
        ports:
        - containerPort: 27017
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: agrichain-secrets
              key: mongo-root-username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: agrichain-secrets
              key: mongo-root-password
        volumeMounts:
        - name: mongodb-data
          mountPath: /data/db
      volumes:
      - name: mongodb-data
        persistentVolumeClaim:
          claimName: mongodb-pvc

---
apiVersion: v1
kind: Service
metadata:
  name: agrichain-service
spec:
  selector:
    app: agrichain-backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
  type: LoadBalancer
                </code></pre>
            </div>
        </div>
        
        <div class="bg-emerald-50 p-4 rounded-lg">
            <h2 class="text-lg font-medium text-emerald-800 mb-2">Next Steps</h2>
            <ul class="list-disc pl-5 text-gray-700 space-y-1">
                <li>Set up CI/CD pipeline with GitHub Actions</li>
                <li>Configure monitoring with Prometheus and Grafana</li>
                <li>Implement WebSocket for real-time updates to frontend</li>
                <li>Add more AI models for different crop types</li>
                <li>Integrate with weather APIs for enhanced predictions</li>
            </ul>
        </div>
    </div>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Abder004/smartfarming" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>