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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
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
/**
 * API Integration Module
 * Handles communication with the Fair Dispatch backend
 * Real-time data fetching with polling support
 */

const API = {
    BASE_URL: 'http://localhost:8000',
    API_PREFIX: '/api/v1',

    // Polling interval in milliseconds - increased to reduce server load
    POLL_INTERVAL: 10000, // 10 seconds for routine checks
    FAST_POLL_INTERVAL: 3000, // 3 seconds when actively monitoring

    // Current polling timer
    _pollTimer: null,

    // Cache for current allocation run
    _currentAllocationRun: null,
    _lastAgentTimeline: null,
    
    // Track last known allocation run ID to avoid redundant fetches
    _lastKnownRunId: null,
    _lastPollTime: 0,
    _lastRunCount: 0, // Track number of runs to detect new allocations
    
    // Selected date for dashboard (initialized to today on first access)
    _selectedDate: null,
    
    // Get or initialize the selected date
    getSelectedDate() {
        if (!this._selectedDate) {
            this._selectedDate = this.getTodayDate();
        }
        return this._selectedDate;
    },

    // Event callbacks
    _callbacks: {
        onStatusUpdate: null,
        onWorkflowUpdate: null,
        onError: null
    },

    /**
     * Set the base URL for API calls
     */
    setBaseUrl(url) {
        this.BASE_URL = url;
    },

    /**
     * Make an API request
     */
    async request(endpoint, options = {}) {
        const url = `${this.BASE_URL}${this.API_PREFIX}${endpoint}`;

        // Add cache busting for GET requests to ensure real-time data
        const cacheBuster = options.method === 'GET' || !options.method
            ? (endpoint.includes('?') ? '&' : '?') + `_t=${new Date().getTime()}`
            : '';

        try {
            const response = await fetch(url + cacheBuster, {
                cache: 'no-store',
                headers: {
                    'Content-Type': 'application/json',
                    'Pragma': 'no-cache',
                    'Cache-Control': 'no-cache, no-store, must-revalidate',
                    ...options.headers
                },
                ...options
            });

            if (!response.ok) {
                throw new Error(`HTTP ${response.status}: ${response.statusText}`);
            }

            return await response.json();
        } catch (error) {
            console.error(`API Error (${endpoint}):`, error);
            if (this._callbacks.onError) {
                this._callbacks.onError(error);
            }
            throw error;
        }
    },

    /**
     * Check system health
     */
    async checkHealth() {
        try {
            const response = await fetch(`${this.BASE_URL}/health`);
            return await response.json();
        } catch (error) {
            return { status: 'disconnected', error: error.message };
        }
    },

    /**
     * Get admin system health with more details
     */
    async getAdminHealth() {
        try {
            return await this.request('/admin/health');
        } catch (error) {
            return null;
        }
    },

    /**
     * Get allocation runs for a specific date
     */
    async getAllocationRuns(date) {
        return this.request(`/admin/allocation_runs?date=${date}`);
    },

    /**
     * Get agent timeline for an allocation run
     */
    async getAgentTimeline(allocationRunId) {
        return this.request(`/admin/agent_timeline?allocation_run_id=${allocationRunId}`);
    },

    /**
     * Get fairness metrics
     */
    async getFairnessMetrics(startDate, endDate) {
        return this.request(`/admin/metrics/fairness?start_date=${startDate}&end_date=${endDate}`);
    },

    /**
     * Trigger a new allocation (for testing)
     */
    async triggerAllocation(data) {
        return this.request('/allocate', {
            method: 'POST',
            body: JSON.stringify(data)
        });
    },

    /**
     * Get driver details
     */
    async getDriver(driverId) {
        return this.request(`/drivers/${driverId}`);
    },

    /**
     * Get route details
     */
    async getRoute(routeId) {
        return this.request(`/routes/${routeId}`);
    },

    /**
     * Get assignments for a date (needed for map visualization)
     */
    async getAssignments(date) {
        return this.request(`/admin/assignments?date=${date}`);
    },

    /**
     * Register callback for status updates
     */
    onStatusUpdate(callback) {
        this._callbacks.onStatusUpdate = callback;
    },

    /**
     * Register callback for workflow updates
     */
    onWorkflowUpdate(callback) {
        this._callbacks.onWorkflowUpdate = callback;
    },

    /**
     * Register callback for errors
     */
    onError(callback) {
        this._callbacks.onError = callback;
    },

    /**
     * Start polling for updates
     */
    startPolling() {
        if (this._pollTimer) {
            clearInterval(this._pollTimer);
        }

        // Use slower polling interval by default
        this._pollTimer = setInterval(async () => {
            await this._pollStatus();
        }, this.POLL_INTERVAL);

        // Initial poll
        this._pollStatus();
    },

    /**
     * Stop polling
     */
    stopPolling() {
        if (this._pollTimer) {
            clearInterval(this._pollTimer);
            this._pollTimer = null;
        }
    },
    
    /**
     * Set the selected date for dashboard queries
     */
    setSelectedDate(date) {
        this._selectedDate = date;
        // Reset cache when date changes
        this._lastKnownRunId = null;
        this._lastRunCount = 0;
    },

    /**
     * Internal polling function - fetches real data from backend
     * Optimized to minimize redundant API calls
     */
    async _pollStatus() {
        try {
            const health = await this.checkHealth();
            const connected = health.status === 'healthy';

            if (this._callbacks.onStatusUpdate) {
                this._callbacks.onStatusUpdate({
                    connected,
                    health
                });
            }

            // If connected, check for new data (but minimize API calls)
            if (connected && this._callbacks.onWorkflowUpdate) {
                const queryDate = this.getSelectedDate();
                
                // Quick check: just get allocation runs count/latest ID
                const runsResponse = await this.getAllocationRuns(queryDate);
                const runs = runsResponse.runs || [];
                const runCount = runs.length;
                const latestRunId = runs.length > 0 ? runs[runs.length - 1].id : null;
                
                // Only fetch full data if:
                // 1. We have no cached data, OR
                // 2. Run count changed (new allocation added), OR  
                // 3. Latest run ID changed
                const needsFullFetch = !this._currentAllocationRun || 
                    runCount !== this._lastRunCount ||
                    latestRunId !== this._lastKnownRunId;
                
                if (needsFullFetch && runs.length > 0) {
                    console.log(`[Poll] New data detected, fetching full workflow state...`);
                    const workflowState = await this.getRealWorkflowState(queryDate);
                    this._lastRunCount = runCount;
                    this._callbacks.onWorkflowUpdate(workflowState);
                }
                // Otherwise, no API call needed - data hasn't changed
            }
        } catch (error) {
            if (this._callbacks.onStatusUpdate) {
                this._callbacks.onStatusUpdate({
                    connected: false,
                    error: error.message
                });
            }
        }
    },

    /**
     * Get today's date in local timezone (YYYY-MM-DD format)
     */
    getTodayDate() {
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0');
        const day = String(now.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    },

    /**
     * Fetch real workflow state from backend
     * @param {string} date - Optional date to fetch, defaults to selected date or today
     * @param {string} runId - Optional specific run ID to fetch
     */
    async getRealWorkflowState(date = null, runId = null) {
        try {
            // If a specific run_id is provided, fetch that run directly
            if (runId) {
                return await this.getWorkflowStateForRun(runId);
            }

            // Use provided date, or get the selected date
            const queryDate = date || this.getSelectedDate();
            const runsResponse = await this.getAllocationRuns(queryDate);

            if (runsResponse.runs && runsResponse.runs.length > 0) {
                // Get the latest allocation run
                const latestRun = runsResponse.runs[runsResponse.runs.length - 1];
                this._currentAllocationRun = latestRun;
                
                // Track the run ID to avoid redundant fetches
                this._lastKnownRunId = latestRun.id;

                // Get agent timeline for this run
                const timeline = await this.getAgentTimeline(latestRun.id);
                this._lastAgentTimeline = timeline;

                // Transform timeline into workflow state
                return this.transformTimelineToWorkflowState(timeline, latestRun);
            }
        } catch (error) {
            console.log('Using mock data - no allocations found:', error.message);
        }

        // Fallback to mock state
        return this.getMockWorkflowState();
    },

    /**
     * Fetch workflow state for a specific run ID
     * @param {string} runId - The allocation run ID to fetch
     */
    async getWorkflowStateForRun(runId) {
        try {
            // Fetch run summary to get run metadata
            const summaryResponse = await fetch(`${this.BASE_URL}${this.API_PREFIX}/runs/${runId}/summary`);
            if (!summaryResponse.ok) {
                throw new Error(`Failed to fetch run summary: ${summaryResponse.status}`);
            }
            const summary = await summaryResponse.json();

            // Build allocation run object from summary
            const allocationRun = {
                id: summary.allocation_run_id,
                date: summary.date,
                num_drivers: summary.num_drivers,
                num_routes: summary.num_routes,
                num_packages: summary.num_packages,
                gini_index: summary.global_gini_index,
                std_dev: summary.global_std_dev,
                status: summary.status,
            };
            this._currentAllocationRun = allocationRun;
            this._lastKnownRunId = runId;

            // Get agent timeline for this specific run
            const timeline = await this.getAgentTimeline(runId);
            this._lastAgentTimeline = timeline;

            // Transform timeline into workflow state
            return this.transformTimelineToWorkflowState(timeline, allocationRun);
        } catch (error) {
            console.error('Error fetching workflow state for run:', runId, error);
            throw error;
        }
    },

    /**
     * Transform agent timeline data to workflow state format
     */
    transformTimelineToWorkflowState(timeline, allocationRun) {
        // Map backend agent names to frontend node IDs
        // Support both UPPERCASE (from LangGraph) and PascalCase formats
        const agentNameMap = {
            // LangGraph format (UPPERCASE)
            'ML_EFFORT': { id: 'route_planner', name: 'Route Planner Agent', type: 'agent' },
            'ML_EFFORT_AGENT': { id: 'route_planner', name: 'Route Planner Agent', type: 'agent' },
            'ROUTE_PLANNER': { id: 'route_planner', name: 'Route Planner Agent', type: 'agent' },
            'FAIRNESS_MANAGER': { id: 'fairness_manager', name: 'Fairness Manager Agent', type: 'agent' },
            'DRIVER_LIAISON': { id: 'driver_liaison', name: 'Driver Liaison Agent', type: 'agent' },
            'EXPLAINABILITY': { id: 'explainability', name: 'Explainability Agent', type: 'agent' },
            'LEARNING': { id: 'learning', name: 'Learning Agent', type: 'agent' },
            // PascalCase format (legacy)
            'MLEffortAgent': { id: 'route_planner', name: 'Route Planner Agent', type: 'agent' },
            'RoutePlannerAgent': { id: 'route_planner', name: 'Route Planner Agent', type: 'agent' },
            'FairnessManagerAgent': { id: 'fairness_manager', name: 'Fairness Manager Agent', type: 'agent' },
            'DriverLiaisonAgent': { id: 'driver_liaison', name: 'Driver Liaison Agent', type: 'agent' },
            'ExplainabilityAgent': { id: 'explainability', name: 'Explainability Agent', type: 'agent' },
            'LearningAgent': { id: 'learning', name: 'Learning Agent', type: 'agent' },
        };


        // Build agent status map from timeline
        // Handle both new format (timeline.timeline) and legacy format (timeline.steps)
        const agentStatus = {};
        const agentLogs = {};
        let totalSteps = 0;

        // Get the actual steps array from the response
        const steps = timeline.timeline || timeline.steps || [];

        if (steps && steps.length > 0) {
            steps.forEach(step => {
                const mapped = agentNameMap[step.agent_name];
                if (mapped) {
                    agentStatus[mapped.id] = 'active';
                    if (!agentLogs[mapped.id]) {
                        agentLogs[mapped.id] = [];
                    }
                    agentLogs[mapped.id].push(step);
                    totalSteps++;
                }
            });
        }

        // Store logs for terminal display
        this._agentLogs = agentLogs;

        const agents = [
            {
                id: 'central_orchestrator',
                name: 'Central Orchestrator Agent',
                description: 'Central Intelligence Hub',
                status: 'active',
                type: 'orchestrator',
                meta: `${Object.keys(agentStatus).length} Active`
            },
            {
                id: 'route_database',
                name: 'Route Database',
                description: `Run: ${allocationRun.id.substring(0, 8)}...`,
                status: 'active',
                type: 'database',
                meta: 'Connected'
            },
            {
                id: 'route_planner',
                name: 'Route Planner Agent',
                description: agentLogs['route_planner'] ?
                    `${agentLogs['route_planner'].length} decisions` : 'Optimizing routes',
                status: agentStatus['route_planner'] || 'idle',
                type: 'agent',
                meta: null
            },
            {
                id: 'fairness_manager',
                name: 'Fairness Manager Agent',
                description: agentLogs['fairness_manager'] ?
                    `Gini: ${allocationRun.gini_index?.toFixed(3) || 'N/A'}` : 'Ensuring equitable distribution',
                status: agentStatus['fairness_manager'] || 'idle',
                type: 'agent',
                meta: null
            },
            {
                id: 'driver_liaison',
                name: 'Driver Liaison Agent',
                description: agentLogs['driver_liaison'] ?
                    `${agentLogs['driver_liaison'].length} interactions` : 'Driver communication hub',
                status: agentStatus['driver_liaison'] || 'idle',
                type: 'agent',
                meta: null
            },
            {
                id: 'explainability',
                name: 'Explainability Agent',
                description: agentLogs['explainability'] ?
                    `${agentLogs['explainability'].length} explanations` : 'Generating explanations',
                status: agentStatus['explainability'] || 'idle',
                type: 'agent',
                meta: null
            },
            {
                id: 'learning',
                name: 'Learning Agent',
                description: 'Continuous improvement loop',
                status: agentStatus['learning'] || 'idle',
                type: 'agent',
                meta: null
            }
        ];

        // Build connections - active if agent has data
        const connections = [
            { from: 'central_orchestrator', to: 'route_database', active: true },
            { from: 'central_orchestrator', to: 'route_planner', active: !!agentStatus['route_planner'] },
            { from: 'central_orchestrator', to: 'fairness_manager', active: !!agentStatus['fairness_manager'] },
            { from: 'central_orchestrator', to: 'driver_liaison', active: !!agentStatus['driver_liaison'] },
            { from: 'central_orchestrator', to: 'explainability', active: !!agentStatus['explainability'] },
            { from: 'central_orchestrator', to: 'learning', active: !!agentStatus['learning'] },
            { from: 'route_database', to: 'route_planner', active: !!agentStatus['route_planner'] },
            { from: 'route_planner', to: 'fairness_manager', active: !!agentStatus['fairness_manager'] },
            { from: 'fairness_manager', to: 'driver_liaison', active: !!agentStatus['driver_liaison'] },
            { from: 'driver_liaison', to: 'explainability', active: !!agentStatus['explainability'] },
            { from: 'explainability', to: 'learning', active: !!agentStatus['learning'] }
        ];

        const activeConnections = connections.filter(c => c.active).length;

        return {
            agents,
            connections,
            stats: {
                processing: 0,
                dataFlows: activeConnections,
                totalAgents: Object.keys(agentStatus).length
            },
            allocationRun,
            timeline,
            isRealData: true
        };
    },

    /**
     * Get cached agent logs for terminal display
     */
    getAgentLogs(agentId) {
        if (this._agentLogs && this._agentLogs[agentId]) {
            return this._agentLogs[agentId];
        }
        return null;
    },

    /**
     * Get current allocation run
     */
    getCurrentAllocationRun() {
        return this._currentAllocationRun;
    },

    /**
     * Get mock workflow state for demo mode
     */
    getMockWorkflowState() {
        return {
            agents: [
                {
                    id: 'central_orchestrator',
                    name: 'Central Orchestrator Agent',
                    description: 'Central Intelligence Hub',
                    status: 'active',
                    type: 'orchestrator',
                    meta: '6 Agents'
                },
                {
                    id: 'route_database',
                    name: 'Route Database',
                    description: '@ 18.9K records',
                    status: 'active',
                    type: 'database',
                    meta: 'Connected'
                },
                {
                    id: 'route_planner',
                    name: 'Route Planner Agent',
                    description: 'Optimizing delivery routes',
                    status: 'processing',
                    type: 'agent',
                    meta: null
                },
                {
                    id: 'fairness_manager',
                    name: 'Fairness Manager Agent',
                    description: 'Ensuring equitable distribution',
                    status: 'idle',
                    type: 'agent',
                    meta: null
                },
                {
                    id: 'driver_liaison',
                    name: 'Driver Liaison Agent',
                    description: 'Driver communication hub',
                    status: 'idle',
                    type: 'agent',
                    meta: null
                },
                {
                    id: 'explainability',
                    name: 'Explainability Agent',
                    description: 'Generating detailed explanations',
                    status: 'idle',
                    type: 'agent',
                    meta: null
                },
                {
                    id: 'learning',
                    name: 'Learning Agent',
                    description: 'Continuous improvement loop',
                    status: 'idle',
                    type: 'agent',
                    meta: null
                }
            ],
            connections: [
                { from: 'central_orchestrator', to: 'route_database', active: true },
                { from: 'central_orchestrator', to: 'route_planner', active: true },
                { from: 'central_orchestrator', to: 'fairness_manager', active: true },
                { from: 'central_orchestrator', to: 'driver_liaison', active: true },
                { from: 'central_orchestrator', to: 'explainability', active: true },
                { from: 'central_orchestrator', to: 'learning', active: true },
                { from: 'route_database', to: 'route_planner', active: false },
                { from: 'route_planner', to: 'fairness_manager', active: false },
                { from: 'fairness_manager', to: 'driver_liaison', active: false },
                { from: 'driver_liaison', to: 'explainability', active: false },
                { from: 'explainability', to: 'learning', active: false }
            ],
            stats: {
                processing: 1,
                dataFlows: 6,
                totalAgents: 6
            },
            isRealData: false
        };
    }
};

// Export for use in app.js
window.API = API;