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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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DCRM API Test Frontend</title>
    <style>

        * {

            margin: 0;

            padding: 0;

            box-sizing: border-box;

        }

        

        body {

            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

            min-height: 100vh;

            padding: 20px;

        }

        

        .container {

            max-width: 1200px;

            margin: 0 auto;

            background: white;

            border-radius: 12px;

            box-shadow: 0 20px 60px rgba(0,0,0,0.3);

            overflow: hidden;

        }

        

        .header {

            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

            color: white;

            padding: 30px;

            text-align: center;

        }

        

        .header h1 {

            font-size: 2.5rem;

            margin-bottom: 10px;

        }

        

        .header p {

            font-size: 1.1rem;

            opacity: 0.9;

        }

        

        .upload-section {

            padding: 40px;

            text-align: center;

        }

        

        .file-input-wrapper {

            position: relative;

            display: inline-block;

            margin: 20px 0;

        }

        

        .file-input-wrapper input[type="file"] {

            position: absolute;

            left: -9999px;

        }

        

        .file-input-label {

            display: inline-block;

            padding: 15px 30px;

            background: #667eea;

            color: white;

            border-radius: 8px;

            cursor: pointer;

            font-size: 1.1rem;

            transition: all 0.3s;

        }

        

        .file-input-label:hover {

            background: #5568d3;

            transform: translateY(-2px);

            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);

        }

        

        .file-name {

            margin: 15px 0;

            font-size: 1rem;

            color: #666;

        }

        

        .breaker-id-input {

            margin: 20px 0;

        }

        

        .breaker-id-input input {

            padding: 12px 20px;

            font-size: 1rem;

            border: 2px solid #ddd;

            border-radius: 8px;

            width: 400px;

            max-width: 100%;

        }

        

        .breaker-id-input input:focus {

            outline: none;

            border-color: #667eea;

        }

        

        .upload-btn {

            padding: 15px 40px;

            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

            color: white;

            border: none;

            border-radius: 8px;

            font-size: 1.1rem;

            cursor: pointer;

            transition: all 0.3s;

            margin-top: 20px;

        }

        

        .upload-btn:hover:not(:disabled) {

            transform: translateY(-2px);

            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);

        }

        

        .upload-btn:disabled {

            opacity: 0.6;

            cursor: not-allowed;

        }

        

        .status {

            margin: 20px 0;

            padding: 15px;

            border-radius: 8px;

            display: none;

        }

        

        .status.loading {

            display: block;

            background: #fff3cd;

            color: #856404;

            border: 1px solid #ffeaa7;

        }

        

        .status.success {

            display: block;

            background: #d4edda;

            color: #155724;

            border: 1px solid #c3e6cb;

        }

        

        .status.error {

            display: block;

            background: #f8d7da;

            color: #721c24;

            border: 1px solid #f5c6cb;

        }

        

        .spinner {

            display: inline-block;

            width: 20px;

            height: 20px;

            border: 3px solid rgba(0,0,0,0.1);

            border-radius: 50%;

            border-top-color: #667eea;

            animation: spin 1s linear infinite;

            margin-right: 10px;

            vertical-align: middle;

        }

        

        @keyframes spin {

            to { transform: rotate(360deg); }

        }

        

        .results-section {

            padding: 40px;

            background: #f8f9fa;

            display: none;

        }

        

        .results-section.show {

            display: block;

        }

        

        .results-header {

            display: flex;

            justify-content: space-between;

            align-items: center;

            margin-bottom: 20px;

        }

        

        .results-header h2 {

            color: #333;

        }

        

        .copy-btn, .download-btn {

            padding: 10px 20px;

            background: #667eea;

            color: white;

            border: none;

            border-radius: 6px;

            cursor: pointer;

            margin-left: 10px;

            transition: all 0.3s;

        }

        

        .copy-btn:hover, .download-btn:hover {

            background: #5568d3;

        }

        

        .json-display {

            background: #1e1e1e;

            color: #d4d4d4;

            padding: 20px;

            border-radius: 8px;

            overflow-x: auto;

            font-family: 'Courier New', monospace;

            font-size: 0.9rem;

            line-height: 1.5;

            max-height: 600px;

            overflow-y: auto;

        }

        

        .json-display pre {

            margin: 0;

        }

        

        .key-metrics {

            display: grid;

            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

            gap: 20px;

            margin-bottom: 30px;

        }

        

        .metric-card {

            background: white;

            padding: 20px;

            border-radius: 8px;

            box-shadow: 0 2px 8px rgba(0,0,0,0.1);

            text-align: center;

        }

        

        .metric-card .label {

            font-size: 0.9rem;

            color: #666;

            margin-bottom: 10px;

        }

        

        .metric-card .value {

            font-size: 2rem;

            font-weight: bold;

            color: #667eea;

        }

    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>⚑ DCRM API Test Frontend</h1>
            <p>Upload CSV file to test the DCRM analysis API</p>
        </div>
        
        <div class="upload-section">
            <h2>Upload DCRM Test Data</h2>
            
            <div class="breaker-id-input">
                <input 

                    type="text" 

                    id="breakerId" 

                    placeholder="Enter Breaker ID (e.g., 6926e63d4614721a79b7b24e)"

                    value="6926e63d4614721a79b7b24e"

                >
            </div>
            
            <div class="file-input-wrapper">
                <input type="file" id="csvFile" accept=".csv">
                <label for="csvFile" class="file-input-label">
                    πŸ“ Choose CSV File
                </label>
            </div>
            
            <div class="file-name" id="fileName">No file selected</div>
            
            <button class="upload-btn" id="uploadBtn" disabled>
                πŸš€ Upload & Analyze
            </button>
            
            <div class="status" id="status"></div>
        </div>
        
        <div class="results-section" id="resultsSection">
            <div class="results-header">
                <h2>πŸ“Š Analysis Results</h2>
                <div>
                    <button class="copy-btn" id="copyBtn">πŸ“‹ Copy JSON</button>
                    <button class="download-btn" id="downloadBtn">πŸ’Ύ Download JSON</button>
                </div>
            </div>
            
            <div class="key-metrics" id="keyMetrics"></div>
            
            <h3 style="margin-bottom: 15px;">Complete JSON Response:</h3>
            <div class="json-display">
                <pre id="jsonOutput"></pre>
            </div>
        </div>
    </div>
    
    <script>

        const API_URL = 'http://localhost:5000';

        

        const fileInput = document.getElementById('csvFile');

        const fileName = document.getElementById('fileName');

        const uploadBtn = document.getElementById('uploadBtn');

        const status = document.getElementById('status');

        const resultsSection = document.getElementById('resultsSection');

        const jsonOutput = document.getElementById('jsonOutput');

        const keyMetrics = document.getElementById('keyMetrics');

        const copyBtn = document.getElementById('copyBtn');

        const downloadBtn = document.getElementById('downloadBtn');

        const breakerIdInput = document.getElementById('breakerId');

        

        let currentResult = null;

        

        // File selection handler

        fileInput.addEventListener('change', (e) => {

            const file = e.target.files[0];

            if (file) {

                fileName.textContent = `Selected: ${file.name}`;

                uploadBtn.disabled = false;

            } else {

                fileName.textContent = 'No file selected';

                uploadBtn.disabled = true;

            }

        });

        

        // Upload and analyze

        uploadBtn.addEventListener('click', async () => {

            const file = fileInput.files[0];

            const breakerId = breakerIdInput.value.trim();

            

            if (!file) {

                showStatus('Please select a CSV file', 'error');

                return;

            }

            

            if (!breakerId) {

                showStatus('Please enter a Breaker ID', 'error');

                return;

            }

            

            // Prepare form data

            const formData = new FormData();

            formData.append('file', file);

            

            // Show loading

            showStatus('Analyzing... This may take 30-60 seconds', 'loading');

            uploadBtn.disabled = true;

            resultsSection.classList.remove('show');

            

            try {

                const response = await fetch(

                    `${API_URL}/api/circuit-breakers/${breakerId}/tests/upload`,

                    {

                        method: 'POST',

                        body: formData

                    }

                );

                

                if (!response.ok) {

                    const errorData = await response.json();

                    throw new Error(errorData.detail?.message || 'Analysis failed');

                }

                

                const result = await response.json();

                currentResult = result;

                

                // Display results

                displayResults(result);

                showStatus('βœ… Analysis completed successfully!', 'success');

                

            } catch (error) {

                showStatus(`❌ Error: ${error.message}`, 'error');

                console.error('Error:', error);

            } finally {

                uploadBtn.disabled = false;

            }

        });

        

        // Display results

        function displayResults(result) {

            // Show key metrics

            const metrics = [

                { label: 'Health Score', value: `${result.healthScore}/100` },

                { label: 'CBHI Score', value: `${result.cbhi?.score || 'N/A'}/100` },

                { label: 'Findings', value: result.findings || 'N/A' },

                { label: 'Breaker ID', value: result.breakerId || 'N/A' }

            ];

            

            keyMetrics.innerHTML = metrics.map(m => `

                <div class="metric-card">

                    <div class="label">${m.label}</div>

                    <div class="value">${m.value}</div>

                </div>

            `).join('');

            

            // Display JSON

            jsonOutput.textContent = JSON.stringify(result, null, 2);

            

            // Show results section

            resultsSection.classList.add('show');

            resultsSection.scrollIntoView({ behavior: 'smooth' });

        }

        

        // Show status message

        function showStatus(message, type) {

            status.className = `status ${type}`;

            if (type === 'loading') {

                status.innerHTML = `<span class="spinner"></span>${message}`;

            } else {

                status.textContent = message;

            }

        }

        

        // Copy JSON to clipboard

        copyBtn.addEventListener('click', () => {

            if (currentResult) {

                const jsonText = JSON.stringify(currentResult, null, 2);

                navigator.clipboard.writeText(jsonText).then(() => {

                    const originalText = copyBtn.textContent;

                    copyBtn.textContent = 'βœ… Copied!';

                    setTimeout(() => {

                        copyBtn.textContent = originalText;

                    }, 2000);

                });

            }

        });

        

        // Download JSON

        downloadBtn.addEventListener('click', () => {

            if (currentResult) {

                const jsonText = JSON.stringify(currentResult, null, 2);

                const blob = new Blob([jsonText], { type: 'application/json' });

                const url = URL.createObjectURL(blob);

                const a = document.createElement('a');

                a.href = url;

                a.download = `dcrm_analysis_${Date.now()}.json`;

                document.body.appendChild(a);

                a.click();

                document.body.removeChild(a);

                URL.revokeObjectURL(url);

            }

        });

    </script>
</body>
</html>