File size: 26,195 Bytes
e7427b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// JSON-based Plugin Support for Custom Calculations
// Extensible calculation engine with user-defined formulas and plugins

interface PluginMetadata {
  id: string;
  name: string;
  version: string;
  author: string;
  description: string;
  category: string;
  tags: string[];
  compatibility: string[];
  lastUpdated: Date;
}

interface PluginInput {
  name: string;
  type: 'number' | 'string' | 'boolean' | 'select' | 'file';
  label: string;
  description?: string;
  required: boolean;
  defaultValue?: any;
  validation?: {
    min?: number;
    max?: number;
    pattern?: string;
    options?: string[];
  };
  unit?: string;
}

interface PluginOutput {
  name: string;
  type: 'number' | 'string' | 'object' | 'array';
  label: string;
  description?: string;
  unit?: string;
  format?: string;
}

interface PluginCalculation {
  formula: string;
  conditions?: Array<{
    condition: string;
    formula: string;
  }>;
  constants?: Record<string, number>;
  functions?: Record<string, string>;
}

interface Plugin {
  metadata: PluginMetadata;
  inputs: PluginInput[];
  outputs: PluginOutput[];
  calculations: PluginCalculation[];
  documentation?: {
    overview: string;
    examples: Array<{
      title: string;
      description: string;
      inputs: Record<string, any>;
      expectedOutput: Record<string, any>;
    }>;
    references: string[];
  };
}

interface PluginExecutionResult {
  success: boolean;
  outputs: Record<string, any>;
  errors?: string[];
  warnings?: string[];
  executionTime: number;
  pluginId: string;
}

class PluginSystem {
  private plugins: Map<string, Plugin> = new Map();
  private readonly PLUGIN_STORAGE_KEY = 'prithvi_plugins';
  private readonly EXECUTION_TIMEOUT = 10000; // 10 seconds

  constructor() {
    this.loadPlugins();
    this.initializeBuiltinPlugins();
  }

  // Plugin Management
  loadPlugin(pluginData: string | Plugin): boolean {
    try {
      let plugin: Plugin;
      
      if (typeof pluginData === 'string') {
        plugin = JSON.parse(pluginData);
      } else {
        plugin = pluginData;
      }

      // Validate plugin structure
      this.validatePlugin(plugin);

      // Check for conflicts
      if (this.plugins.has(plugin.metadata.id)) {
        const existingPlugin = this.plugins.get(plugin.metadata.id)!;
        if (existingPlugin.metadata.version >= plugin.metadata.version) {
          throw new Error(`Plugin ${plugin.metadata.id} version ${plugin.metadata.version} is not newer than existing version ${existingPlugin.metadata.version}`);
        }
      }

      // Install plugin
      plugin.metadata.lastUpdated = new Date();
      this.plugins.set(plugin.metadata.id, plugin);
      this.savePlugins();

      return true;
    } catch (error) {
      throw new Error(`Failed to load plugin: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  unloadPlugin(pluginId: string): boolean {
    try {
      if (!this.plugins.has(pluginId)) {
        throw new Error(`Plugin ${pluginId} not found`);
      }

      this.plugins.delete(pluginId);
      this.savePlugins();
      return true;
    } catch (error) {
      throw new Error(`Failed to unload plugin: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  getPlugin(pluginId: string): Plugin | undefined {
    return this.plugins.get(pluginId);
  }

  getAllPlugins(): Plugin[] {
    return Array.from(this.plugins.values()).sort((a, b) => 
      a.metadata.name.localeCompare(b.metadata.name)
    );
  }

  getPluginsByCategory(category: string): Plugin[] {
    return this.getAllPlugins().filter(plugin => 
      plugin.metadata.category.toLowerCase() === category.toLowerCase()
    );
  }

  searchPlugins(query: string): Plugin[] {
    if (!query || query.trim().length === 0) {
      return this.getAllPlugins();
    }

    const searchTerm = query.toLowerCase();
    return this.getAllPlugins().filter(plugin =>
      plugin.metadata.name.toLowerCase().includes(searchTerm) ||
      plugin.metadata.description.toLowerCase().includes(searchTerm) ||
      plugin.metadata.tags.some(tag => tag.toLowerCase().includes(searchTerm))
    );
  }

  // Plugin Execution
  executePlugin(pluginId: string, inputs: Record<string, any>): Promise<PluginExecutionResult> {
    return new Promise((resolve, reject) => {
      const startTime = Date.now();
      
      try {
        const plugin = this.plugins.get(pluginId);
        if (!plugin) {
          reject(new Error(`Plugin ${pluginId} not found`));
          return;
        }

        // Set execution timeout
        const timeoutId = setTimeout(() => {
          reject(new Error(`Plugin execution timed out after ${this.EXECUTION_TIMEOUT}ms`));
        }, this.EXECUTION_TIMEOUT);

        // Validate inputs
        const validationResult = this.validateInputs(plugin, inputs);
        if (!validationResult.valid) {
          clearTimeout(timeoutId);
          resolve({
            success: false,
            outputs: {},
            errors: validationResult.errors,
            executionTime: Date.now() - startTime,
            pluginId
          });
          return;
        }

        // Execute calculations
        const executionResult = this.executeCalculations(plugin, inputs);
        
        clearTimeout(timeoutId);
        resolve({
          success: executionResult.success,
          outputs: executionResult.outputs,
          errors: executionResult.errors,
          warnings: executionResult.warnings,
          executionTime: Date.now() - startTime,
          pluginId
        });

      } catch (error) {
        reject(error);
      }
    });
  }

  // Plugin Creation Helper
  createPlugin(metadata: PluginMetadata, config: {
    inputs: PluginInput[];
    outputs: PluginOutput[];
    calculations: PluginCalculation[];
    documentation?: Plugin['documentation'];
  }): Plugin {
    try {
      const plugin: Plugin = {
        metadata: {
          ...metadata,
          lastUpdated: new Date()
        },
        inputs: config.inputs,
        outputs: config.outputs,
        calculations: config.calculations,
        documentation: config.documentation
      };

      this.validatePlugin(plugin);
      return plugin;
    } catch (error) {
      throw new Error(`Failed to create plugin: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  // Export/Import
  exportPlugin(pluginId: string): string {
    try {
      const plugin = this.plugins.get(pluginId);
      if (!plugin) {
        throw new Error(`Plugin ${pluginId} not found`);
      }

      return JSON.stringify(plugin, null, 2);
    } catch (error) {
      throw new Error(`Failed to export plugin: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  exportAllPlugins(): string {
    try {
      const allPlugins = Array.from(this.plugins.values());
      return JSON.stringify({
        plugins: allPlugins,
        exportDate: new Date().toISOString(),
        version: '1.0'
      }, null, 2);
    } catch (error) {
      throw new Error(`Failed to export plugins: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  importPlugins(importData: string): { successful: number; failed: number; errors: string[] } {
    try {
      const data = JSON.parse(importData);
      const plugins = data.plugins || [data]; // Handle single plugin or multiple
      
      let successful = 0;
      let failed = 0;
      const errors: string[] = [];

      plugins.forEach((plugin: Plugin, index: number) => {
        try {
          this.loadPlugin(plugin);
          successful++;
        } catch (error) {
          failed++;
          errors.push(`Plugin ${index + 1}: ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      });

      return { successful, failed, errors };
    } catch (error) {
      throw new Error(`Failed to import plugins: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  // Private Methods
  private validatePlugin(plugin: Plugin): void {
    // Validate metadata
    if (!plugin.metadata || !plugin.metadata.id || !plugin.metadata.name) {
      throw new Error('Plugin metadata is incomplete');
    }

    if (!plugin.metadata.version || !this.isValidVersion(plugin.metadata.version)) {
      throw new Error('Plugin version is invalid');
    }

    // Validate inputs
    if (!Array.isArray(plugin.inputs)) {
      throw new Error('Plugin inputs must be an array');
    }

    plugin.inputs.forEach((input, index) => {
      if (!input.name || !input.type || !input.label) {
        throw new Error(`Input ${index} is incomplete`);
      }
      if (!['number', 'string', 'boolean', 'select', 'file'].includes(input.type)) {
        throw new Error(`Input ${index} has invalid type: ${input.type}`);
      }
    });

    // Validate outputs
    if (!Array.isArray(plugin.outputs) || plugin.outputs.length === 0) {
      throw new Error('Plugin must have at least one output');
    }

    plugin.outputs.forEach((output, index) => {
      if (!output.name || !output.type || !output.label) {
        throw new Error(`Output ${index} is incomplete`);
      }
    });

    // Validate calculations
    if (!Array.isArray(plugin.calculations) || plugin.calculations.length === 0) {
      throw new Error('Plugin must have at least one calculation');
    }

    plugin.calculations.forEach((calc, index) => {
      if (!calc.formula || typeof calc.formula !== 'string') {
        throw new Error(`Calculation ${index} must have a valid formula`);
      }
    });
  }

  private validateInputs(plugin: Plugin, inputs: Record<string, any>): {
    valid: boolean;
    errors: string[];
  } {
    const errors: string[] = [];

    plugin.inputs.forEach(inputDef => {
      const value = inputs[inputDef.name];

      // Check required fields
      if (inputDef.required && (value === undefined || value === null || value === '')) {
        errors.push(`${inputDef.label} is required`);
        return;
      }

      if (value === undefined || value === null) {
        return; // Skip validation for optional empty fields
      }

      // Type validation
      switch (inputDef.type) {
        case 'number':
          if (typeof value !== 'number' || isNaN(value)) {
            errors.push(`${inputDef.label} must be a valid number`);
          } else {
            if (inputDef.validation?.min !== undefined && value < inputDef.validation.min) {
              errors.push(`${inputDef.label} must be at least ${inputDef.validation.min}`);
            }
            if (inputDef.validation?.max !== undefined && value > inputDef.validation.max) {
              errors.push(`${inputDef.label} must be at most ${inputDef.validation.max}`);
            }
          }
          break;

        case 'string':
          if (typeof value !== 'string') {
            errors.push(`${inputDef.label} must be a string`);
          } else {
            if (inputDef.validation?.pattern) {
              const regex = new RegExp(inputDef.validation.pattern);
              if (!regex.test(value)) {
                errors.push(`${inputDef.label} format is invalid`);
              }
            }
          }
          break;

        case 'select':
          if (inputDef.validation?.options && !inputDef.validation.options.includes(value)) {
            errors.push(`${inputDef.label} must be one of: ${inputDef.validation.options.join(', ')}`);
          }
          break;

        case 'boolean':
          if (typeof value !== 'boolean') {
            errors.push(`${inputDef.label} must be true or false`);
          }
          break;
      }
    });

    return {
      valid: errors.length === 0,
      errors
    };
  }

  private executeCalculations(plugin: Plugin, inputs: Record<string, any>): {
    success: boolean;
    outputs: Record<string, any>;
    errors?: string[];
    warnings?: string[];
  } {
    try {
      const outputs: Record<string, any> = {};
      const warnings: string[] = [];

      for (const calculation of plugin.calculations) {
        try {
          // Create calculation context
          const context = {
            ...inputs,
            ...(calculation.constants || {}),
            Math: Math,
            abs: Math.abs,
            sqrt: Math.sqrt,
            pow: Math.pow,
            exp: Math.exp,
            log: Math.log,
            sin: Math.sin,
            cos: Math.cos,
            tan: Math.tan,
            max: Math.max,
            min: Math.min,
            round: Math.round,
            floor: Math.floor,
            ceil: Math.ceil
          };

          // Execute formula
          let formula = calculation.formula;
          
          // Check conditions
          if (calculation.conditions) {
            for (const condition of calculation.conditions) {
              if (this.evaluateCondition(condition.condition, context)) {
                formula = condition.formula;
                break;
              }
            }
          }

          // Replace variables in formula
          const result = this.evaluateFormula(formula, context);
          
          // Map results to outputs
          plugin.outputs.forEach(outputDef => {
            if (outputDef.name in result) {
              outputs[outputDef.name] = result[outputDef.name];
            } else if (typeof result === 'number' && plugin.outputs.length === 1) {
              outputs[outputDef.name] = result;
            }
          });

        } catch (error) {
          warnings.push(`Calculation error: ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }

      return {
        success: Object.keys(outputs).length > 0,
        outputs,
        warnings: warnings.length > 0 ? warnings : undefined
      };

    } catch (error) {
      return {
        success: false,
        outputs: {},
        errors: [`Execution failed: ${error instanceof Error ? error.message : 'Unknown error'}`]
      };
    }
  }

  private evaluateFormula(formula: string, context: Record<string, any>): any {
    try {
      // Basic safety checks
      if (formula.includes('eval') || formula.includes('Function') || formula.includes('require')) {
        throw new Error('Unsafe formula detected');
      }

      // Create a safe evaluation context
      const safeContext = { ...context };
      delete (safeContext as any).constructor;
      delete (safeContext as any).__proto__;

      // Simple formula evaluation using Function constructor (safer than eval)
      const func = new Function(...Object.keys(safeContext), `return ${formula}`);
      return func(...Object.values(safeContext));
    } catch (error) {
      throw new Error(`Formula evaluation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
    }
  }

  private evaluateCondition(condition: string, context: Record<string, any>): boolean {
    try {
      const result = this.evaluateFormula(condition, context);
      return Boolean(result);
    } catch (error) {
      return false;
    }
  }

  private isValidVersion(version: string): boolean {
    return /^\d+\.\d+\.\d+$/.test(version);
  }

  private loadPlugins(): void {
    try {
      const data = localStorage.getItem(this.PLUGIN_STORAGE_KEY);
      if (data) {
        const plugins = JSON.parse(data);
        plugins.forEach((plugin: Plugin) => {
          plugin.metadata.lastUpdated = new Date(plugin.metadata.lastUpdated);
          this.plugins.set(plugin.metadata.id, plugin);
        });
      }
    } catch (error) {
      console.error('Failed to load plugins from storage:', error);
    }
  }

  private savePlugins(): void {
    try {
      const plugins = Array.from(this.plugins.values());
      localStorage.setItem(this.PLUGIN_STORAGE_KEY, JSON.stringify(plugins));
    } catch (error) {
      throw new Error(`Failed to save plugins: ${error instanceof Error ? error.message : 'Storage quota exceeded'}`);
    }
  }

  private initializeBuiltinPlugins(): void {
    // Water Quality Index Calculator Plugin
    const wqiPlugin: Plugin = {
      metadata: {
        id: 'builtin_wqi_calculator',
        name: 'Water Quality Index Calculator',
        version: '1.0.0',
        author: 'VBharat AI',
        description: 'Comprehensive Water Quality Index calculation based on multiple parameters',
        category: 'Water Quality',
        tags: ['water', 'quality', 'index', 'pollution'],
        compatibility: ['v1.0'],
        lastUpdated: new Date()
      },
      inputs: [
        {
          name: 'ph',
          type: 'number',
          label: 'pH',
          description: 'pH value of water sample',
          required: true,
          validation: { min: 0, max: 14 },
          unit: 'pH units'
        },
        {
          name: 'do',
          type: 'number',
          label: 'Dissolved Oxygen',
          description: 'Dissolved oxygen concentration',
          required: true,
          validation: { min: 0, max: 20 },
          unit: 'mg/L'
        },
        {
          name: 'bod',
          type: 'number',
          label: 'BOD',
          description: 'Biochemical Oxygen Demand',
          required: true,
          validation: { min: 0 },
          unit: 'mg/L'
        },
        {
          name: 'nitrate',
          type: 'number',
          label: 'Nitrate',
          description: 'Nitrate concentration',
          required: true,
          validation: { min: 0 },
          unit: 'mg/L'
        },
        {
          name: 'phosphate',
          type: 'number',
          label: 'Phosphate',
          description: 'Phosphate concentration',
          required: true,
          validation: { min: 0 },
          unit: 'mg/L'
        },
        {
          name: 'temperature',
          type: 'number',
          label: 'Temperature Deviation',
          description: 'Temperature deviation from normal',
          required: true,
          validation: { min: -10, max: 10 },
          unit: '°C'
        }
      ],
      outputs: [
        {
          name: 'wqi',
          type: 'number',
          label: 'Water Quality Index',
          description: 'Overall water quality index value',
          unit: 'WQI'
        },
        {
          name: 'category',
          type: 'string',
          label: 'Quality Category',
          description: 'Water quality classification'
        },
        {
          name: 'recommendation',
          type: 'string',
          label: 'Recommendation',
          description: 'Treatment or action recommendation'
        }
      ],
      calculations: [
        {
          formula: `
            // Individual quality indices
            var qi_ph = ph >= 6.5 && ph <= 8.5 ? 100 : max(0, 100 - abs(7 - ph) * 15);
            var qi_do = min(100, (do / 14.6) * 100);
            var qi_bod = max(0, 100 - (bod * 5));
            var qi_nitrate = max(0, 100 - (nitrate * 2));
            var qi_phosphate = max(0, 100 - (phosphate * 10));
            var qi_temp = max(0, 100 - abs(temperature) * 5);
            
            // Weights (sum = 1.0)
            var w_ph = 0.15;
            var w_do = 0.25;
            var w_bod = 0.25;
            var w_nitrate = 0.15;
            var w_phosphate = 0.10;
            var w_temp = 0.10;
            
            // Weighted WQI calculation
            var wqi_value = (qi_ph * w_ph + qi_do * w_do + qi_bod * w_bod + 
                           qi_nitrate * w_nitrate + qi_phosphate * w_phosphate + qi_temp * w_temp);
            
            // Determine category and recommendation
            var category = wqi_value > 90 ? 'Excellent' : 
                          wqi_value > 70 ? 'Good' : 
                          wqi_value > 50 ? 'Medium' : 
                          wqi_value > 25 ? 'Bad' : 'Very Bad';
            
            var recommendation = wqi_value > 70 ? 'Water is suitable for drinking with standard treatment' :
                               wqi_value > 50 ? 'Requires advanced treatment before use' :
                               'Extensive treatment required, monitor pollution sources';
            
            ({ wqi: round(wqi_value * 100) / 100, category: category, recommendation: recommendation })
          `,
          constants: {
            IDEAL_DO: 14.6,
            NEUTRAL_PH: 7.0
          }
        }
      ],
      documentation: {
        overview: 'Calculates comprehensive Water Quality Index based on six key parameters with appropriate weightings according to Indian standards.',
        examples: [
          {
            title: 'Clean River Water',
            description: 'Typical values for unpolluted river water',
            inputs: {
              ph: 7.2,
              do: 8.5,
              bod: 2.0,
              nitrate: 5.0,
              phosphate: 0.1,
              temperature: 1.0
            },
            expectedOutput: {
              wqi: 85.5,
              category: 'Good',
              recommendation: 'Water is suitable for drinking with standard treatment'
            }
          }
        ],
        references: [
          'CPCB Water Quality Criteria',
          'IS 10500:2012 Drinking Water Standards',
          'WHO Water Quality Guidelines'
        ]
      }
    };

    this.plugins.set(wqiPlugin.metadata.id, wqiPlugin);

    // Structural Load Calculator Plugin
    const loadPlugin: Plugin = {
      metadata: {
        id: 'builtin_structural_load',
        name: 'Structural Load Calculator',
        version: '1.0.0',
        author: 'VBharat AI',
        description: 'Calculate design loads for structures as per IS 875',
        category: 'Structural Engineering',
        tags: ['structural', 'load', 'design', 'IS875'],
        compatibility: ['v1.0'],
        lastUpdated: new Date()
      },
      inputs: [
        {
          name: 'dead_load',
          type: 'number',
          label: 'Dead Load',
          description: 'Permanent structural load',
          required: true,
          validation: { min: 0 },
          unit: 'kN/m²'
        },
        {
          name: 'live_load',
          type: 'number',
          label: 'Live Load',
          description: 'Variable/imposed load',
          required: true,
          validation: { min: 0 },
          unit: 'kN/m²'
        },
        {
          name: 'wind_load',
          type: 'number',
          label: 'Wind Load',
          description: 'Wind pressure load',
          required: false,
          defaultValue: 0,
          validation: { min: 0 },
          unit: 'kN/m²'
        },
        {
          name: 'seismic_load',
          type: 'number',
          label: 'Seismic Load',
          description: 'Earthquake load',
          required: false,
          defaultValue: 0,
          validation: { min: 0 },
          unit: 'kN/m²'
        },
        {
          name: 'load_combination',
          type: 'select',
          label: 'Load Combination',
          description: 'Design load combination type',
          required: true,
          validation: {
            options: ['basic', 'wind', 'seismic', 'all']
          }
        }
      ],
      outputs: [
        {
          name: 'design_load',
          type: 'number',
          label: 'Design Load',
          description: 'Factored design load',
          unit: 'kN/m²'
        },
        {
          name: 'safety_factor',
          type: 'number',
          label: 'Safety Factor',
          description: 'Overall safety factor achieved'
        },
        {
          name: 'governing_combination',
          type: 'string',
          label: 'Governing Combination',
          description: 'Critical load combination'
        }
      ],
      calculations: [
        {
          formula: `
            // Load combinations as per IS 875
            var basic_combo = 1.5 * (dead_load + live_load);
            var wind_combo = 1.2 * (dead_load + live_load + wind_load);
            var seismic_combo = 1.2 * (dead_load + live_load + seismic_load);
            var wind_uplift = 0.9 * dead_load + 1.5 * wind_load;
            
            var combinations = {
              'basic': basic_combo,
              'wind': max(basic_combo, wind_combo, wind_uplift),
              'seismic': max(basic_combo, seismic_combo),
              'all': max(basic_combo, wind_combo, seismic_combo, wind_uplift)
            };
            
            var design_load_value = combinations[load_combination];
            var safety_factor_value = design_load_value / (dead_load + live_load);
            
            var governing = design_load_value === basic_combo ? 'Basic (1.5DL+1.5LL)' :
                           design_load_value === wind_combo ? 'Wind (1.2DL+1.2LL+1.2WL)' :
                           design_load_value === seismic_combo ? 'Seismic (1.2DL+1.2LL+1.2EL)' :
                           'Wind Uplift (0.9DL+1.5WL)';
            
            ({ 
              design_load: round(design_load_value * 100) / 100, 
              safety_factor: round(safety_factor_value * 100) / 100,
              governing_combination: governing 
            })
          `
        }
      ],
      documentation: {
        overview: 'Calculates structural design loads according to IS 875 load combinations for different loading scenarios.',
        examples: [
          {
            title: 'Residential Building',
            description: 'Typical residential structure loads',
            inputs: {
              dead_load: 4.0,
              live_load: 2.0,
              wind_load: 1.0,
              seismic_load: 0.8,
              load_combination: 'all'
            },
            expectedOutput: {
              design_load: 9.6,
              safety_factor: 1.6,
              governing_combination: 'Wind (1.2DL+1.2LL+1.2WL)'
            }
          }
        ],
        references: [
          'IS 875-1987 Design Loads for Buildings',
          'IS 1893-2016 Earthquake Resistant Design',
          'IS 456-2000 Plain and Reinforced Concrete'
        ]
      }
    };

    this.plugins.set(loadPlugin.metadata.id, loadPlugin);
  }
}

export const pluginSystem = new PluginSystem();
export { Plugin, PluginInput, PluginOutput, PluginExecutionResult, PluginMetadata };