File size: 5,140 Bytes
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced1993
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced1993
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
ced1993
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced1993
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
 
ced1993
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
ced1993
4b1a31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { DocumentFile, Innovation } from '../types';

export interface ProcessResponse {
    id: string;
    file_name: string;
    answer: string;
    classification: string;
}

export const backendService = {
    /**
     * Sends a document to the backend for processing.
     * The backend handles extraction, storage (SQLite), and mock refinement.
     */
    processDocument: async (file: DocumentFile, workingGroup: string, meeting: string): Promise<Innovation | null> => {
        if (!file.url) {
            console.warn(`Skipping file ${file.filename}: No URL`);
            return null;
        }

        try {
            const payload = {
                file_id: file.id,
                filename: file.filename,
                working_group: workingGroup,
                meeting: meeting,
                type: file.type,
                status: file.status || "Unknown",
                agenda_item: file.agendaItem || "Unknown",
                url: file.url
            };

            const response = await fetch(`/process`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(payload)
            });

            if (!response.ok) {
                console.error(`Backend processing failed for ${file.filename}: ${response.statusText}`);
                return null;
            }

            const data: ProcessResponse = await response.json();

            // Map response to Innovation type
            return {
                id: file.id,
                file_name: file.filename,
                answer: data.answer,
                classification: "UNCLASSIFIED",
            } as Innovation;

        } catch (error) {
            console.error(`Error processing document ${file.filename}:`, error);
            return null;
        }
    },

    getPatterns: async (): Promise<any[]> => {
        try {
            const response = await fetch(`/patterns`);
            if (response.ok) {
                return await response.json();
            }
            return [];
        } catch (error) {
            console.error("Error fetching patterns:", error);
            return [];
        }
    },

    analyzeContent: async (patternId: number, fileId?: string, text?: string): Promise<{ content: string; result_id: number; methodology: string; context: string; problem: string; pattern_name: string } | null> => {
        try {
            const response = await fetch(`/analyze`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    pattern_id: patternId,
                    file_id: fileId,
                    text: text
                })
            });

            if (response.ok) {
                const data = await response.json();
                return { content: data.content, result_id: data.id, methodology: data.methodology, context: data.context, problem: data.problem, pattern_name: data.pattern_name };
            }
            return null;
        } catch (error) {
            console.error("Error analyzing content:", error);
            return null;
        }
    },

    saveClassification: async (resultId: number, classification: string): Promise<boolean> => {
        try {
            const response = await fetch(`/classify`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ result_id: resultId, classification })
            });
            return response.ok;
        } catch (error) {
            console.error("Error saving classification:", error);
            return false;
        }
    },

    fetchClassifiedInnovations: async (): Promise<any[]> => {
        try {
            const response = await fetch(`/results`);
            if (response.ok) {
                return await response.json();
            }
            return [];
        } catch (error) {
            console.error("Error fetching classified innovations:", error);
            return [];
        }
    },

    fetchAllFiles: async (): Promise<DocumentFile[]> => {
        try {
            const response = await fetch(`/get_all`);
            if (response.ok) {
                const rows = await response.json();
                // Map tuple to DocumentFile: 
                // [file_id, working_group, meeting, type, status, agenda_item, content, filename, timestamp]
                return rows.map((row: any) => ({
                    id: row[0],
                    filename: row[7],
                    size: 'Unknown',
                    type: row[3],
                    uploaded: true, // It's from DB, so treated as processed/uploaded
                    status: row[4],
                    agendaItem: row[5],
                    url: '' // Not stored explicitly usually, or maybe we don't need it for display
                }));
            }
            return [];
        } catch (error) {
            console.error("Error fetching all files:", error);
            return [];
        }
    }
};