File size: 25,727 Bytes
66a0674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Script d'initialisation de la base de données SQLite pour HuggingFace Spaces."""

import os
import sqlite3
import sys

# Ajouter le répertoire src au path pour les imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))

# Connexion globale en mémoire partagée
_db_connection = None


def get_db_connection():
    """Retourne la connexion SQLite partagée en mémoire."""
    global _db_connection
    if _db_connection is None:
        _db_connection = sqlite3.connect(":memory:", check_same_thread=False)
    return _db_connection


def init_sqlite_data():
    """Initialise la base SQLite avec les données essentielles en utilisant SQLite direct."""
    try:
        conn = get_db_connection()
        cursor = conn.cursor()

        # Vérifier les tables existantes
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
        tables = cursor.fetchall()
        print(f"📋 Tables existantes: {[table[0] for table in tables]}")

        print("📊 Insertion des données de référence...")

        # Données Neighborhoods
        neighborhoods = [
            (1, "UNKNOWN", -1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (2, "BALLARD", 0, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (3, "CENTRAL", 1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (4, "DELRIDGE", 2, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (5, "DOWNTOWN", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (6, "EAST", 4, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (7, "GREATER DUWAMISH", 5, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (8, "LAKE UNION", 6, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                9,
                "MAGNOLIA / QUEEN ANNE",
                7,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (10, "NORTH", 8, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (11, "NORTHEAST", 9, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (12, "NORTHWEST", 10, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (13, "SOUTHEAST", 11, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (14, "SOUTHWEST", 12, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (15, "WEST", 13, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
        ]

        cursor.executemany(
            "INSERT OR REPLACE INTO neighborhood (id, neighborhood_name, model_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
            neighborhoods,
        )

        # Données Building Types
        building_types = [
            (
                1,
                -1,
                "UNKNOWN",
                "Type de bâtiment inconnu ou non spécifié",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                2,
                0,
                "CAMPUS",
                "Campus building complex",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                3,
                1,
                "NONRESIDENTIAL",
                "Non-residential building",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                4,
                2,
                "NONRESIDENTIAL COS",
                "Non-residential COS type",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                5,
                3,
                "NONRESIDENTIAL WA",
                "Non-residential WA type",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                6,
                4,
                "SPS-DISTRICT K-12",
                "Seattle Public Schools District K-12",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                7,
                5,
                "Multifamily MR (5-9)",
                "Multifamily Mid-Rise 5-9 units",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                8,
                6,
                "Multifamily HR (10+)",
                "Multifamily High-Rise 10+ units",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                9,
                7,
                "Multifamily LR (2-4)",
                "Multifamily Low-Rise 2-4 units",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
        ]

        cursor.executemany(
            "INSERT OR REPLACE INTO building_type (id, model_id, building_type_name, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
            building_types,
        )

        # Données Categories
        categories = [
            (
                1,
                "UNKNOWN",
                "UNKNOWN",
                "Catégorie inconnue ou non spécifiée",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                2,
                "CAMPUS",
                "Campus",
                "Complexes de campus et installations multiples",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                3,
                "EDUCATION",
                "Éducation",
                "Établissements denseignement et de formation",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                4,
                "ENTERTAINMENT",
                "Divertissement",
                "Théâtres, cinémas et espaces de divertissement",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                5,
                "FINANCIAL",
                "Services financiers",
                "Banques, bureaux financiers et services monétaires",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                6,
                "HEALTHCARE",
                "Santé",
                "Hôpitaux, cliniques et établissements de soins médicaux",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                7,
                "INDUSTRIAL",
                "Industrie",
                "Usines et installations industrielles",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                8,
                "LODGING",
                "Hébergement",
                "Hôtels et logements temporaires",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                9,
                "MIXED",
                "Usage mixte",
                "Propriétés à usage multiple",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                10,
                "NONE",
                "Aucun",
                "Aucune utilisation spécifique",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                11,
                "OFFICE",
                "Bureaux",
                "Espaces de bureaux et administratifs",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                12,
                "PARKING",
                "Stationnement",
                "Structures et espaces de stationnement",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                13,
                "PUBLIC",
                "Services publics",
                "Services gouvernementaux et publics",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                14,
                "RECREATION",
                "Loisirs",
                "Installations sportives et récréatives",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                15,
                "RELIGIOUS",
                "Religieux",
                "Églises et lieux de culte",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                16,
                "RESIDENTIAL",
                "Résidentiel",
                "Logements et habitations",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                17,
                "RESTAURANT",
                "Restauration",
                "Restaurants et services alimentaires",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                18,
                "RETAIL",
                "Commerce de détail",
                "Magasins et commerces",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                19,
                "SOCIAL",
                "Social",
                "Clubs et espaces sociaux",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                20,
                "STORE",
                "Magasins",
                "Commerces et magasins divers",
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
        ]

        cursor.executemany(
            "INSERT OR REPLACE INTO categories (id, category_code, category_name, description, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
            categories,
        )

        # Données Properties (sélection des principales)
        properties = [
            (1, -1, "UNKNOWN", 1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (2, 0, "ADULT EDUCATION", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                3,
                1,
                "AUTOMOBILE DEALERSHIP",
                20,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (4, 2, "BANK BRANCH", 5, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (5, 3, "BAR/NIGHTCLUB", 19, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                6,
                4,
                "COLLEGE/UNIVERSITY",
                3,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                7,
                5,
                "CONVENIENCE STORE WITHOUT GAS STATION",
                20,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (8, 6, "COURTHOUSE", 15, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (9, 7, "DATA CENTER", 11, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                10,
                8,
                "DISTRIBUTION CENTER",
                7,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                11,
                9,
                "FINANCIAL OFFICE",
                5,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (12, 10, "FOOD SALES", 17, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                13,
                11,
                "HOSPITAL (GENERAL MEDICAL & SURGICAL)",
                6,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (14, 12, "HOTEL", 8, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (15, 13, "K-12 SCHOOL", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (16, 14, "LIBRARY", 3, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (17, 15, "MEDICAL OFFICE", 6, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                18,
                16,
                "MULTIFAMILY HOUSING",
                16,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                19,
                17,
                "MUNICIPAL WASTEWATER TREATMENT PLANT",
                7,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (20, 18, "OFFICE", 11, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (21, 19, "OTHER", 1, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (22, 20, "PARKING", 12, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (23, 21, "RESTAURANT", 17, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (24, 22, "RETAIL STORE", 18, "2025-09-09 09:56:21", "2025-09-09 09:56:21"),
            (
                25,
                23,
                "SELF-STORAGE FACILITY",
                7,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                26,
                24,
                "SENIOR LIVING COMMUNITY",
                8,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                27,
                25,
                "SUPERMARKET/GROCERY STORE",
                17,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                28,
                26,
                "WAREHOUSE (UNREFRIGERATED)",
                7,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                29,
                27,
                "WORSHIP FACILITY",
                15,
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
        ]

        cursor.executemany(
            "INSERT OR REPLACE INTO property (id, model_id, property_name, category_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
            properties,
        )

        # Données Building Models (exemples pour tester l'API)
        building_models = [
            (
                1,
                "SEATTLE001",
                "123 Main Street",
                "Seattle",
                "WA",
                "98101",
                "TAX001",
                "DISTRICT1",
                47.6062,
                -122.3321,
                1995,
                1,
                15,
                50000.0,
                5000.0,
                2000.0,
                500.0,
                0,  # multiusage
                0,  # steam
                1,  # electricity
                1,  # natural_gas
                2,  # neighborhood_id (BALLARD)
                3,  # building_type_id (NONRESIDENTIAL)
                20,  # largest_property_use_type_id (OFFICE)
                20,  # primary_property_type_id (OFFICE)
                1,  # second_largest_property_use_type_id (UNKNOWN)
                1,  # third_largest_property_use_type_id (UNKNOWN)
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                2,
                "SEATTLE002",
                "456 Pine Avenue",
                "Seattle",
                "WA",
                "98102",
                "TAX002",
                "DISTRICT2",
                47.6205,
                -122.3493,
                2010,
                1,
                8,
                25000.0,
                2000.0,
                0.0,
                0.0,
                0,  # multiusage
                0,  # steam
                1,  # electricity
                1,  # natural_gas
                5,  # neighborhood_id (DOWNTOWN)
                7,  # building_type_id (Multifamily MR 5-9)
                18,  # largest_property_use_type_id (MULTIFAMILY HOUSING)
                18,  # primary_property_type_id (MULTIFAMILY HOUSING)
                1,  # second_largest_property_use_type_id (UNKNOWN)
                1,  # third_largest_property_use_type_id (UNKNOWN)
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
            (
                3,
                "SEATTLE003",
                "789 University Way",
                "Seattle",
                "WA",
                "98105",
                "TAX003",
                "DISTRICT3",
                47.6587,
                -122.3128,
                1980,
                1,
                3,
                15000.0,
                1000.0,
                500.0,
                200.0,
                1,  # multiusage
                0,  # steam
                1,  # electricity
                1,  # natural_gas
                11,  # neighborhood_id (NORTHEAST)
                6,  # building_type_id (SPS-DISTRICT K-12)
                15,  # largest_property_use_type_id (K-12 SCHOOL)
                15,  # primary_property_type_id (K-12 SCHOOL)
                16,  # second_largest_property_use_type_id (LIBRARY)
                1,  # third_largest_property_use_type_id (UNKNOWN)
                "2025-09-09 09:56:21",
                "2025-09-09 09:56:21",
            ),
        ]

        cursor.executemany(
            """INSERT OR REPLACE INTO building_models (
                id, ose_building_id, address, city, state, zip_code,
                tax_parcel_identification_number, council_district_code,
                latitude, longitude, year_built, number_of_buildings, number_of_floors,
                property_gfa_total, property_gfa_parking,
                second_largest_property_use_type_gfa, third_largest_property_use_type_gfa,
                multiusage, steam, electricity, natural_gas,
                neighborhood_id, building_type_id,
                largest_property_use_type_id, primary_property_type_id,
                second_largest_property_use_type_id, third_largest_property_use_type_id,
                created_at, updated_at
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
            building_models,
        )

        # Données Building Energy Predictions (exemples de prédictions)
        energy_predictions = [
            (
                1,
                1,  # building_id (correspond à SEATTLE001)
                45678.5,  # site_energy_use_wn_kbtu
                1,  # predicted (True)
                "2025-09-09 10:30:00",
            ),
            (
                2,
                2,  # building_id (correspond à SEATTLE002)
                23456.7,  # site_energy_use_wn_kbtu
                1,  # predicted (True)
                "2025-09-09 11:15:00",
            ),
            (
                3,
                3,  # building_id (correspond à SEATTLE003)
                12345.3,  # site_energy_use_wn_kbtu
                1,  # predicted (True)
                "2025-09-09 12:00:00",
            ),
        ]

        cursor.executemany(
            """INSERT OR REPLACE INTO building_energy_predictions (
                id, building_id, site_energy_use_wn_kbtu, predicted, updated_at
            ) VALUES (?, ?, ?, ?, ?)""",
            energy_predictions,
        )

        conn.commit()
        # Ne pas fermer la connexion - elle reste en mémoire

        print(f"✅ {len(neighborhoods)} quartiers insérés")
        print(f"✅ {len(building_types)} types de bâtiments insérés")
        print(f"✅ {len(categories)} catégories insérées")
        print(f"✅ {len(properties)} propriétés insérées")
        print(f"✅ {len(building_models)} bâtiments d'exemple insérés")
        print(f"✅ {len(energy_predictions)} prédictions d'exemple insérées")

    except Exception as e:
        print(f"❌ Erreur lors de l'insertion des données: {e}")
        raise


def create_sqlite_tables():
    """Crée les tables SQLite nécessaires avec raw SQL."""

    try:
        conn = get_db_connection()
        cursor = conn.cursor()

        # Créer les tables de référence principales
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS neighborhood (
                id INTEGER PRIMARY KEY,
                neighborhood_name VARCHAR(50) NOT NULL UNIQUE,
                model_id INTEGER NOT NULL UNIQUE,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
        """
        )

        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS building_type (
                id INTEGER PRIMARY KEY,
                model_id INTEGER NOT NULL UNIQUE,
                building_type_name VARCHAR(100) NOT NULL UNIQUE,
                description TEXT,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
        """
        )

        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS categories (
                id INTEGER PRIMARY KEY,
                category_code VARCHAR(50) NOT NULL UNIQUE,
                category_name VARCHAR(100) NOT NULL,
                description TEXT,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
        """
        )

        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS property (
                id INTEGER PRIMARY KEY,
                model_id INTEGER NOT NULL UNIQUE,
                property_name VARCHAR(150) NOT NULL UNIQUE,
                category_id INTEGER NOT NULL,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (category_id) REFERENCES categories (id)
            );
        """
        )

        # Créer les tables principales
        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS building_energy_predictions (
                id INTEGER PRIMARY KEY,
                building_id INTEGER NOT NULL,
                site_energy_use_wn_kbtu FLOAT NOT NULL,
                predicted BOOLEAN DEFAULT 0,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
        """
        )

        cursor.execute(
            """
            CREATE TABLE IF NOT EXISTS building_models (
                id INTEGER PRIMARY KEY,
                ose_building_id INTEGER NOT NULL UNIQUE,
                address VARCHAR(255) NOT NULL,
                city VARCHAR(100) NULL,
                state VARCHAR(50)  NULL,
                zip_code VARCHAR(20) NULL,
                tax_parcel_identification_number VARCHAR(100) NULL,
                council_district_code varchar(20) NULL,
                latitude FLOAT NULL,
                longitude FLOAT NULL,
                year_built INTEGER NULL,
                number_of_buildings INTEGER NULL,
                number_of_floors INTEGER NULL,
                property_gfa_total FLOAT NULL,
                property_gfa_parking FLOAT NULL,
                second_largest_property_use_type_gfa FLOAT NULL,
                third_largest_property_use_type_gfa FLOAT NULL,
                multiusage BOOLEAN DEFAULT 0,
                steam BOOLEAN DEFAULT 0,
                electricity BOOLEAN DEFAULT 0,
                natural_gas BOOLEAN DEFAULT 0,
                neighborhood_id INTEGER NULL,
                building_type_id INTEGER NULL,
                largest_property_use_type_id INTEGER NULL,
                primary_property_type_id INTEGER NULL,
                second_largest_property_use_type_id INTEGER NULL,
                third_largest_property_use_type_id INTEGER NULL,
                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (id) REFERENCES building_energy_predictions (building_id),
                FOREIGN KEY (largest_property_use_type_id) REFERENCES property (id),
                FOREIGN KEY (primary_property_type_id) REFERENCES property (id),
                FOREIGN KEY (second_largest_property_use_type_id) REFERENCES property (id),
                FOREIGN KEY (third_largest_property_use_type_id) REFERENCES property (id),
                FOREIGN KEY (neighborhood_id) REFERENCES neighborhood (id),
                FOREIGN KEY (building_type_id) REFERENCES building_type (id)
            );
        """
        )

        conn.commit()
        # Ne pas fermer la connexion - elle reste en mémoire
        print("🗄️ Structure des tables créée avec succès!")

    except Exception as e:
        print(f"❌ Erreur lors de la création des tables: {e}")
        raise


if __name__ == "__main__":
    try:
        print("🚀 Initialisation de la base de données SQLite...")

        # Créer les tables avec raw SQL
        create_sqlite_tables()

        print("📊 Insertion des données de référence...")
        init_sqlite_data()
        print("✅ Base de données SQLite initialisée avec succès!")

    except Exception as e:
        print(f"❌ Erreur lors de l'initialisation: {e}")
        import traceback

        traceback.print_exc()
        sys.exit(1)