File size: 11,155 Bytes
9ce3455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import PDFDocument from 'pdfkit';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

class ReportGenerator {
    async generateReport(analysisResults, documentInfo, executiveSummary) {
        return new Promise((resolve, reject) => {
            try {
                const reportsDir = path.join(__dirname, '../reports');
                if (!fs.existsSync(reportsDir)) {
                    fs.mkdirSync(reportsDir, { recursive: true });
                }

                const filename = `NDMA_Training_Report_${Date.now()}.pdf`;
                const filepath = path.join(reportsDir, filename);

                const doc = new PDFDocument({
                    size: 'A4',
                    margins: { top: 60, bottom: 60, left: 60, right: 60 }
                });

                const stream = fs.createWriteStream(filepath);
                doc.pipe(stream);

                // Generate report
                this.addCoverPage(doc, documentInfo);
                this.addExecutiveSummary(doc, executiveSummary);
                this.addKeyMetrics(doc, analysisResults);
                this.addDetailedAnalysis(doc, analysisResults);
                this.addGapAnalysis(doc, analysisResults);
                this.addInsights(doc, analysisResults);
                this.addRecommendations(doc, analysisResults);

                doc.end();

                stream.on('finish', () => {
                    resolve({ filename, filepath, url: `/reports/${filename}` });
                });

                stream.on('error', (error) => {
                    reject(error);
                });
            } catch (error) {
                reject(error);
            }
        });
    }

    addCoverPage(doc, documentInfo) {
        // Top Border
        doc.rect(0, 0, doc.page.width, 10).fill('#1e40af');

        // Government Header
        doc.fontSize(28).font('Helvetica-Bold').fillColor('#1e40af')
            .text('GOVERNMENT OF INDIA', 0, 100, { align: 'center' }).moveDown(0.3);

        doc.fontSize(24).fillColor('#2563eb')
            .text('National Disaster Management Authority', { align: 'center' }).moveDown(0.2);

        doc.fontSize(12).font('Helvetica').fillColor('#6b7280')
            .text('Ministry of Home Affairs', { align: 'center' }).moveDown(3);

        // Title Box
        const titleY = doc.y;
        doc.roundedRect(80, titleY, 440, 100, 10).fillAndStroke('#dbeafe', '#3b82f6');

        doc.fontSize(22).font('Helvetica-Bold').fillColor('#1e40af')
            .text('DISASTER MANAGEMENT', 80, titleY + 15, { width: 440, align: 'center' }).moveDown(0.3);

        doc.fontSize(20).fillColor('#2563eb')
            .text('TRAINING ANALYSIS REPORT', 80, doc.y, { width: 440, align: 'center' });

        doc.moveDown(4);

        // Report Details
        const currentDate = new Date().toLocaleDateString('en-IN', {
            day: '2-digit', month: 'long', year: 'numeric'
        });

        const detailsY = doc.y;
        doc.roundedRect(120, detailsY, 360, 100, 8).fillAndStroke('#f8fafc', '#cbd5e1');

        doc.fontSize(11).font('Helvetica-Bold').fillColor('#374151')
            .text('πŸ“… Report Date:', 140, detailsY + 15);
        doc.font('Helvetica').text(currentDate, 250, detailsY + 15);

        doc.font('Helvetica-Bold').text('πŸ“„ Document:', 140, detailsY + 35);
        doc.font('Helvetica').text(documentInfo.originalName || 'Training Data', 250, detailsY + 35, { width: 210 });

        doc.font('Helvetica-Bold').text('πŸ” Analysis Type:', 140, detailsY + 55);
        doc.font('Helvetica').text('Comprehensive Analysis', 250, detailsY + 55);

        doc.font('Helvetica-Bold').text('βœ… Status:', 140, detailsY + 75);
        doc.font('Helvetica').fillColor('#059669').text('COMPLETED', 250, detailsY + 75);

        doc.moveDown(4);

        // Confidentiality Notice
        const noticeY = doc.y;
        doc.roundedRect(100, noticeY, 400, 50, 5).fillAndStroke('#fee2e2', '#dc2626');

        doc.fontSize(11).font('Helvetica-Bold').fillColor('#991b1b')
            .text('⚠️ CONFIDENTIAL DOCUMENT', 0, noticeY + 10, { align: 'center' }).moveDown(0.3);

        doc.fontSize(9).font('Helvetica').fillColor('#7f1d1d')
            .text('For official use only. Unauthorized distribution prohibited.', 0, doc.y, { align: 'center' });

        // Bottom Border
        doc.rect(0, doc.page.height - 10, doc.page.width, 10).fill('#1e40af');

        doc.addPage();
    }

    addExecutiveSummary(doc, summary) {
        doc.fontSize(18).font('Helvetica-Bold').fillColor('#1e40af')
            .text('πŸ“‹ EXECUTIVE SUMMARY', { underline: true }).moveDown(1);

        const summaryY = doc.y;
        const summaryHeight = doc.heightOfString(summary, { width: 460, align: 'justify' });

        doc.roundedRect(60, summaryY - 10, 480, summaryHeight + 30, 8)
            .fillAndStroke('#eff6ff', '#93c5fd');

        doc.fontSize(11).font('Helvetica').fillColor('#1f2937')
            .text(summary, 70, summaryY, { width: 460, align: 'justify', lineGap: 5 });

        doc.addPage();
    }

    addKeyMetrics(doc, analysis) {
        doc.fontSize(18).font('Helvetica-Bold').fillColor('#1e40af')
            .text('πŸ“Š KEY PERFORMANCE METRICS', { underline: true }).moveDown(1.5);

        const metrics = [
            { icon: '🎯', label: 'Total Training Sessions', value: analysis.totalTrainings || 0, color: '#3b82f6', bg: '#dbeafe' },
            { icon: 'πŸ‘₯', label: 'Total Participants', value: analysis.totalParticipants?.toLocaleString() || 0, color: '#10b981', bg: '#d1fae5' },
            { icon: 'πŸ“ˆ', label: 'Completion Rate', value: analysis.averageCompletionRate || 'N/A', color: '#f59e0b', bg: '#fef3c7' },
            { icon: 'πŸ“', label: 'States Covered', value: Object.keys(analysis.stateWiseCoverage || {}).length, color: '#8b5cf6', bg: '#ede9fe' },
            { icon: 'πŸŽ“', label: 'Disaster Themes', value: Object.keys(analysis.themeDistribution || {}).length, color: '#ec4899', bg: '#fce7f3' }
        ];

        let yPos = doc.y;
        metrics.forEach((metric) => {
            doc.roundedRect(60, yPos, 480, 50, 8).fillAndStroke(metric.bg, metric.color);
            doc.fontSize(24).text(metric.icon, 75, yPos + 13);
            doc.fontSize(12).font('Helvetica-Bold').fillColor('#374151')
                .text(metric.label, 115, yPos + 12);
            doc.fontSize(20).font('Helvetica-Bold').fillColor(metric.color)
                .text(metric.value.toString(), 380, yPos + 10, { width: 150, align: 'right' });
            yPos += 60;
        });

        doc.addPage();
    }

    addDetailedAnalysis(doc, analysis) {
        doc.fontSize(18).font('Helvetica-Bold').fillColor('#1e40af')
            .text('πŸ“ˆ DETAILED ANALYSIS', { underline: true }).moveDown(1.5);

        // Theme Distribution
        if (analysis.themeDistribution && Object.keys(analysis.themeDistribution).length > 0) {
            doc.fontSize(14).font('Helvetica-Bold').fillColor('#2563eb')
                .text('Theme Distribution', 60).moveDown(0.8);

            const themes = Object.entries(analysis.themeDistribution).sort((a, b) => b[1] - a[1]);
            themes.forEach(([theme, count], idx) => {
                const percentage = ((count / analysis.totalTrainings) * 100).toFixed(1);
                doc.fontSize(10).font('Helvetica').fillColor('#1f2937')
                    .text(`${idx + 1}. ${theme}: ${count} sessions (${percentage}%)`, { indent: 20 })
                    .moveDown(0.3);
            });
            doc.moveDown(1);
        }

        // State Coverage
        if (analysis.stateWiseCoverage && Object.keys(analysis.stateWiseCoverage).length > 0) {
            doc.fontSize(14).font('Helvetica-Bold').fillColor('#2563eb')
                .text('Top 10 States', 60).moveDown(0.8);

            const states = Object.entries(analysis.stateWiseCoverage)
                .sort((a, b) => b[1] - a[1]).slice(0, 10);

            states.forEach(([state, count], idx) => {
                doc.fontSize(10).font('Helvetica').fillColor('#1f2937')
                    .text(`${idx + 1}. ${state}: ${count} sessions`, { indent: 20 })
                    .moveDown(0.3);
            });
        }

        doc.addPage();
    }

    addGapAnalysis(doc, analysis) {
        if (!analysis.gapAnalysis) return;

        doc.fontSize(18).font('Helvetica-Bold').fillColor('#dc2626')
            .text('⚠️ GAP ANALYSIS', { underline: true }).moveDown(1.5);

        if (analysis.gapAnalysis.underservedStates?.length > 0) {
            doc.fontSize(13).font('Helvetica-Bold').fillColor('#f59e0b')
                .text('Underserved States', 60).moveDown(0.5);

            analysis.gapAnalysis.underservedStates.forEach((state, idx) => {
                doc.fontSize(10).font('Helvetica').fillColor('#92400e')
                    .text(`${idx + 1}. ${state}`, { indent: 20 }).moveDown(0.3);
            });
            doc.moveDown(1);
        }

        if (analysis.gapAnalysis.criticalGaps?.length > 0) {
            doc.fontSize(13).font('Helvetica-Bold').fillColor('#dc2626')
                .text('Critical Gaps', 60).moveDown(0.5);

            analysis.gapAnalysis.criticalGaps.forEach((gap, idx) => {
                doc.fontSize(10).font('Helvetica').fillColor('#7f1d1d')
                    .text(`${idx + 1}. ${gap}`, { indent: 20, align: 'justify' })
                    .moveDown(0.5);
            });
        }

        doc.addPage();
    }

    addInsights(doc, analysis) {
        if (!analysis.keyInsights?.length) return;

        doc.fontSize(18).font('Helvetica-Bold').fillColor('#1e40af')
            .text('πŸ’‘ BUSINESS INSIGHTS', { underline: true }).moveDown(1.5);

        analysis.keyInsights.forEach((insight, idx) => {
            doc.fontSize(10).font('Helvetica').fillColor('#1e40af')
                .text(`${idx + 1}. ${insight}`, { indent: 20, align: 'justify' })
                .moveDown(0.8);
        });

        doc.addPage();
    }

    addRecommendations(doc, analysis) {
        if (!analysis.recommendations?.length) return;

        doc.fontSize(18).font('Helvetica-Bold').fillColor('#059669')
            .text('βœ… STRATEGIC RECOMMENDATIONS', { underline: true }).moveDown(1.5);

        analysis.recommendations.forEach((rec, idx) => {
            doc.fontSize(10).font('Helvetica-Bold').fillColor('#065f46')
                .text(`${idx + 1}. ${rec}`, { indent: 20, align: 'justify' })
                .moveDown(0.8);
        });

        // Footer
        doc.moveDown(2);
        doc.fontSize(9).font('Helvetica').fillColor('#6b7280')
            .text('Β© National Disaster Management Authority, Government of India', { align: 'center' });
    }
}

export default new ReportGenerator();