File size: 44,829 Bytes
9ec0175
 
 
 
b2a58f3
 
 
 
 
 
 
 
 
9ec0175
365f157
b2a58f3
 
 
 
ca1825c
b2a58f3
9ec0175
b2a58f3
 
 
0e589b3
9ec0175
b2a58f3
 
0e589b3
9ec0175
b2a58f3
22366a5
 
 
 
 
 
9ec0175
01803b7
9ec0175
 
0e2e301
9ec0175
b2a58f3
9ec0175
b2a58f3
 
 
ca1825c
b2a58f3
9ec0175
0e589b3
9ec0175
b2a58f3
 
 
9ec0175
b2a58f3
9ec0175
 
b2a58f3
 
 
9ec0175
 
b2a58f3
 
 
 
 
 
 
 
 
 
 
9ec0175
b2a58f3
0e589b3
9ec0175
b0f12dd
9ec0175
 
 
 
b0f12dd
b2a58f3
 
9ec0175
b0f12dd
b2a58f3
 
 
0e589b3
9ec0175
b0f12dd
 
9ec0175
b0f12dd
 
 
 
 
 
 
 
9ec0175
b0f12dd
 
 
 
 
 
 
 
 
 
 
9ec0175
 
b2a58f3
 
 
 
 
 
ca1825c
9ec0175
b2a58f3
 
 
 
 
 
 
 
ca1825c
bd5524b
9ec0175
 
 
 
 
 
 
 
 
0e589b3
 
 
 
 
 
 
9ec0175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a58f3
 
 
 
 
9ec0175
b2a58f3
 
9ec0175
 
 
b2a58f3
 
9ec0175
 
 
 
 
 
bd5524b
 
 
 
 
22366a5
bd5524b
 
 
 
22366a5
bd5524b
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
bd5524b
 
 
 
22366a5
bd5524b
 
 
22366a5
bd5524b
 
 
22366a5
bd5524b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
bd5524b
 
22366a5
bd5524b
 
22366a5
bd5524b
 
22366a5
bd5524b
 
0e589b3
bd5524b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
bd5524b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
bd5524b
 
 
 
 
 
22366a5
bd5524b
 
 
 
 
 
 
22366a5
bd5524b
 
 
22366a5
bd5524b
 
 
0e589b3
bd5524b
 
 
a752e59
bd5524b
a752e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
365f157
0e589b3
bd5524b
 
 
 
 
 
0e589b3
b2a58f3
bd5524b
 
 
a752e59
 
 
bd5524b
 
b2a58f3
ca1825c
 
a752e59
0e589b3
b2a58f3
a752e59
b2a58f3
 
 
 
 
 
 
a752e59
 
bd5524b
 
a752e59
bd5524b
 
a752e59
 
bd5524b
 
a752e59
 
bd5524b
a752e59
b2a58f3
 
01803b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
b2a58f3
 
 
 
 
 
 
 
22366a5
b2a58f3
 
 
 
 
 
 
ca1825c
 
b2a58f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
f4cc550
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca1825c
 
f4cc550
 
 
 
 
 
22366a5
f4cc550
 
ca1825c
 
f4cc550
 
 
 
 
 
 
 
22366a5
f4cc550
 
 
 
 
ca1825c
 
f4cc550
 
 
ca1825c
 
f4cc550
 
 
 
 
 
 
 
 
 
 
 
0e589b3
ca1825c
 
 
 
 
 
 
 
22366a5
ca1825c
 
 
 
22366a5
ca1825c
 
22366a5
ca1825c
 
 
 
22366a5
ca1825c
0e589b3
ca1825c
b2a58f3
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
b2a58f3
 
ca1825c
22366a5
 
ca1825c
 
 
b2a58f3
 
 
 
 
 
 
 
 
 
 
 
ca1825c
 
b2a58f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
b2a58f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
b2a58f3
 
 
 
ca1825c
 
b2a58f3
0e589b3
bd5524b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
bd5524b
 
 
 
 
 
 
 
0e589b3
ca1825c
 
 
 
 
 
 
bd5524b
22366a5
0e589b3
ca1825c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd5524b
 
 
 
 
 
ca1825c
 
 
 
 
 
 
 
 
22366a5
ca1825c
 
 
 
 
0e589b3
f6f491d
 
 
 
 
22366a5
f6f491d
 
 
22366a5
f6f491d
 
22366a5
f6f491d
 
22366a5
f6f491d
 
22366a5
f6f491d
 
 
22366a5
f6f491d
22366a5
f6f491d
 
 
 
 
 
 
 
 
 
22366a5
f6f491d
 
 
 
 
0e589b3
f6f491d
 
 
 
 
22366a5
f6f491d
 
 
 
 
22366a5
f6f491d
 
 
22366a5
f6f491d
 
22366a5
f6f491d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
f6f491d
 
 
 
22366a5
f6f491d
22366a5
f6f491d
 
 
 
 
0e589b3
b2a58f3
56e0c09
0e589b3
56e0c09
0e589b3
9ec0175
56e0c09
 
 
 
22366a5
 
0e589b3
56e0c09
 
22366a5
56e0c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22366a5
56e0c09
 
 
 
 
 
 
 
22366a5
b2a58f3
 
 
 
 
 
 
 
bd5524b
22366a5
b2a58f3
 
 
 
 
 
 
 
 
 
 
ca1825c
 
b2a58f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca1825c
 
b2a58f3
 
 
 
 
bd5524b
 
 
b2a58f3
 
 
 
ca1825c
 
b2a58f3
ca1825c
 
 
 
 
 
b2a58f3
 
 
 
 
ca1825c
 
b2a58f3
 
 
 
0e589b3
22366a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
22366a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
22366a5
 
 
0e589b3
8555914
22366a5
 
 
 
 
8555914
22366a5
 
0e589b3
22366a5
 
 
 
 
 
 
 
8555914
0e589b3
8555914
22366a5
0e589b3
22366a5
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
22366a5
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
22366a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e589b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a58f3
22366a5
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
# app.py - Storage Manager (Cleaned v2 - benching_machine reference)
# Structure score: 8.5/10 (modular-ready)
# See BENCHMARK_REFERENCE.md in benching_machine repo for comparison table

import os
import json
import uuid
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
from typing import List, Optional
from datetime import datetime
from enum import Enum

import uvicorn
from fastapi import FastAPI, HTTPException, UploadFile, File, Form, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from aiocache import Cache

# Google Cloud Imports
from google.cloud import storage
from google.oauth2 import service_account

# ========================= CONFIGURATION =========================
BUCKET_NAME = os.environ.get("GCP_BUCKET_NAME")
CREDENTIALS_JSON = os.environ.get("GCP_CREDENTIALS")

# --- STORAGE MAP (extended for Brainfuck WASM benchmarks) ---
STORAGE_MAP = {
    "song": {"folder": "songs/", "index": "songs/_songs.json"},
    "pattern": {"folder": "patterns/", "index": "patterns/_patterns.json"},
    "bank": {"folder": "banks/", "index": "banks/_banks.json"},
    "sample": {"folder": "samples/", "index": "samples/_samples.json"},
    "music": {"folder": "music/", "index": "music/_music.json"},
    "note": {"folder": "notes/", "index": "notes/_notes.json"},
    "shader": {"folder": "shaders/", "index": "shaders/_shaders.json"},
    "brainfuck": {  # ← ADDED (static examples only)
        "folder": "brainfuck/",
        "index": "brainfuck/_brainfuck.json"
    },
    "default": {"folder": "misc/", "index": "misc/_misc.json"},
}

# --- GLOBAL OBJECTS ---
gcs_client = None
bucket = None
io_executor = ThreadPoolExecutor(max_workers=20)
cache = Cache(Cache.MEMORY)
INDEX_LOCK = asyncio.Lock()

# ========================= HELPERS =========================
def get_gcs_client():
    if CREDENTIALS_JSON:
        cred_info = json.loads(CREDENTIALS_JSON)
        creds = service_account.Credentials.from_service_account_info(cred_info)
        return storage.Client(credentials=creds)
    return storage.Client()

async def run_io(func, *args, **kwargs):
    loop = asyncio.get_running_loop()
    return await loop.run_in_executor(io_executor, lambda: func(*args, **kwargs))

# ========================= LIFESPAN =========================
@asynccontextmanager
async def lifespan(app: FastAPI):
    global gcs_client, bucket
    try:
        gcs_client = get_gcs_client()
        bucket = gcs_client.bucket(BUCKET_NAME)
        print(f"--- GCS CONNECTED: {BUCKET_NAME} ---")
    except Exception as e:
        print(f"!!! GCS CONNECTION FAILED: {e}")
    yield
    io_executor.shutdown()

app = FastAPI(lifespan=lifespan)

# ========================= CORS =========================
ALLOWED_ORIGINS = [
    "http://localhost:3000",
    "https://test.1ink.us",
    "https://go.1ink.us",
    "https://noahcohn.com",
]
app.add_middleware(
    CORSMiddleware,
    allow_origins=ALLOWED_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ========================= DIRECT STORAGE LISTING =========================
@app.get("/api/storage/files")
async def list_gcs_folder(folder: str = Query(..., description="Folder name, e.g., 'songs' or 'samples'")):
    # (your exact original code - unchanged)
    config = STORAGE_MAP.get(folder)
    prefix = config["folder"] if config else f"{folder}/"
    try:
        def _fetch_blobs():
            blobs = bucket.list_blobs(prefix=prefix, delimiter="/")
            file_list = []
            for blob in blobs:
                name = blob.name.replace(prefix, "")
                if name:
                    file_list.append({
                        "filename": name,
                        "size": blob.size,
                        "updated": blob.updated.isoformat() if blob.updated else None,
                        "url": blob.public_url if blob.public_url else None
                    })
            return file_list
        files = await run_io(_fetch_blobs)
        return {"folder": prefix, "count": len(files), "files": files}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# ========================= MODELS =========================
class ItemPayload(BaseModel):
    name: str
    author: str
    description: Optional[str] = ""
    type: str = "song"
    data: dict
    rating: Optional[int] = None

class MetaData(BaseModel):
    id: str
    name: str
    author: str
    date: str
    type: str
    description: Optional[str] = ""
    filename: str
    rating: Optional[int] = None
    genre: Optional[str] = None
    last_played: Optional[str] = None
    tags: List[str] = Field(default_factory=list)

class SortBy(str, Enum):
    date = "date"
    rating = "rating"
    name = "name"
    last_played = "last_played"
    genre = "genre"

class ShaderCategory(str, Enum):
    generative = "generative"
    reactive = "reactive"
    transition = "transition"
    filter = "filter"
    distortion = "distortion"

class SampleMetaUpdatePayload(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    rating: Optional[int] = Field(None, ge=1, le=10)
    genre: Optional[str] = None
    last_played: Optional[str] = None

class MetaPatch(BaseModel):
    name: Optional[str] = None
    rating: Optional[int] = Field(None, ge=0, le=10)
    genre: Optional[str] = None
    tags: Optional[List[str]] = None
    last_played: Optional[str] = None

# ========================= GCS I/O HELPERS =========================
def _read_json_sync(blob_path):
    blob = bucket.blob(blob_path)
    if blob.exists():
        return json.loads(blob.download_as_text())
    return []

def _write_json_sync(blob_path, data):
    blob = bucket.blob(blob_path)
    blob.upload_from_string(json.dumps(data), content_type='application/json')

# ========================= ENDPOINTS =========================
@app.get("/")
def home():
    return {
        "status": "online",
        "provider": "Google Cloud Storage",
        "benchmark_ready": True,
        "reference": "benching_machine_ideal"
    }
@app.post("/api/admin/sync-music")
async def sync_music_folder():
    """Scans the music/ folder and rebuilds the music index."""
    config = STORAGE_MAP["music"]
    report = {"added": 0, "removed": 0}
   
    async with INDEX_LOCK:
        try:
            # 1. List all files in music/
            blobs = await run_io(lambda: list(bucket.list_blobs(prefix=config["folder"])))
           
            # Filter for audio files (FLAC, WAV, MP3)
            audio_files = []
            for b in blobs:
                fname = b.name.replace(config["folder"], "")
                if fname and not b.name.endswith(config["index"]):
                    lower = fname.lower()
                    if lower.endswith(('.flac', '.wav', '.mp3', '.ogg')):
                        audio_files.append({
                            "filename": fname,
                            "name": fname,
                            "size": b.size,
                            "url": b.public_url
                        })
           
            # 2. Get current index
            index_data = await run_io(_read_json_sync, config["index"])
            if not isinstance(index_data, list):
                index_data = []
           
            # 3. Compare and update
            index_map = {item["filename"]: item for item in index_data}
            disk_set = set(f["filename"] for f in audio_files)
           
            # Remove missing files
            new_index = [item for item in index_data if item["filename"] in disk_set]
            report["removed"] = len(index_data) - len(new_index)
           
            # Add new files
            for file_info in audio_files:
                if file_info["filename"] not in index_map:
                    new_entry = {
                        "id": str(uuid.uuid4()),
                        "filename": file_info["filename"],
                        "name": file_info["name"],
                        "type": "music",
                        "date": datetime.now().strftime("%Y-%m-%d"),
                        "author": "Unknown",
                        "description": "",
                        "rating": None,
                        "genre": None,
                        "last_played": None,
                        "url": file_info["url"],
                        "size": file_info["size"]
                    }
                    new_index.insert(0, new_entry)
                    report["added"] += 1
           
            if report["added"] > 0 or report["removed"] > 0:
                await run_io(_write_json_sync, config["index"], new_index)
           
            await cache.delete("library:music")
            await cache.delete("library:all")
           
            report["total"] = len(new_index)
            return report
           
        except Exception as e:
            raise HTTPException(500, f"Failed to sync music: {str(e)}")
            
@app.post("/api/admin/seed-test-samples")
async def seed_test_samples():
    """Creates test sample entries for development."""
    config = STORAGE_MAP["sample"]
    test_samples = [
        {
            "id": "test-flac-001",
            "name": "Test Ambient Track.flac",
            "filename": "test-flac-001.flac",
            "type": "sample",
            "author": "Test Artist",
            "date": "2024-02-09",
            "description": "Test ambient track",
            "rating": 8,
            "genre": "ambient"
        },
        {
            "id": "test-wav-002",
            "name": "Test Bass Line.wav",
            "filename": "test-wav-002.wav",
            "type": "sample",
            "author": "Test Artist",
            "date": "2024-02-09",
            "description": "Test bass line",
            "rating": 7,
            "genre": "bass"
        },
        {
            "id": "test-flac-003",
            "name": "Unrated Demo.flac",
            "filename": "test-flac-003.flac",
            "type": "sample",
            "author": "Unknown",
            "date": "2024-02-09",
            "description": "Demo without rating",
            "rating": None,
            "genre": None
        }
    ]
   
    async with INDEX_LOCK:
        try:
            # Read existing
            index_data = await run_io(_read_json_sync, config["index"])
            if not isinstance(index_data, list):
                index_data = []
           
            # Add test samples (avoid duplicates)
            existing_ids = {item.get("id") for item in index_data}
            added = 0
            for sample in test_samples:
                if sample["id"] not in existing_ids:
                    index_data.insert(0, sample)
                    added += 1
           
            await run_io(_write_json_sync, config["index"], index_data)
            await cache.delete("library:sample")
            await cache.delete("library:all")
           
            return {"success": True, "added": added, "total": len(index_data)}
        except Exception as e:
            raise HTTPException(500, f"Failed to seed: {str(e)}")
            
@app.get("/api/health")
async def health_check():
    """Returns storage manager status and index counts."""
    status_report = {}
    for item_type, config in STORAGE_MAP.items():
        try:
            # Attempt to read the index to verify connectivity
            index_data = await run_io(_read_json_sync, config["index"])
            status_report[item_type] = {
                "count": len(index_data) if isinstance(index_data, list) else 0,
                "status": "connected"
            }
        except Exception as e:
            status_report[item_type] = {"count": 0, "status": "error", "error": str(e)}
            
    return {
        "status": "online", 
        "gcs_connected": bucket is not None,
        "storage": status_report
    }
    


class SortBy(str, Enum):
    date = "date"
    rating = "rating"
    name = "name"
    last_played = "last_played"
    genre = "genre"
    
@app.get("/api/songs", response_model=List[MetaData])
async def list_library(
    type: Optional[str] = Query(None),
    genre: Optional[str] = Query(None),
    min_rating: Optional[int] = Query(None, ge=1, le=10),
    sort_by: SortBy = Query(SortBy.date), # Added missing param
    sort_desc: bool = Query(True)         # Added missing param
):
    cache_key = f"library:{type or 'all'}:{sort_by}:{sort_desc}:{genre}:{min_rating}"
    cached = await cache.get(cache_key)
    if cached:
        return cached

    search_types = [type] if type else ["song", "pattern", "bank", "sample", "music", "shader"]
    results = []
    
    for t in search_types:
        config = STORAGE_MAP.get(t, STORAGE_MAP["default"])
        try:
            items = await run_io(_read_json_sync, config["index"])
            if isinstance(items, list):
                results.extend(items)
        except Exception as e:
            logging.error(f"Error listing {t}: {e}")

    if genre:
        results = [r for r in results if r.get("genre") == genre]
    
    if min_rating is not None:
        results = [r for r in results if (r.get("rating") or 0) >= min_rating]

    # Sorting Logic
    def sort_key(item):
        val = item.get(sort_by.value)
        return (0, val) if val is not None else (1, "")

    results.sort(key=sort_key, reverse=sort_desc)
    
    await cache.set(cache_key, results, ttl=30)
    return results

@app.post("/api/admin/seed-brainfuck-examples")
async def seed_brainfuck_examples():
    """Seeds static benchmark examples for estimation charts."""
    config = STORAGE_MAP["brainfuck"]
    examples = [
        {
            "id": "bf-mandelbrot",
            "name": "Mandelbrot Set",
            "type": "brainfuck",
            "author": "Classic BF",
            "date": "2026-03-07",
            "description": "bf2wasm compiler + -O3 flags",
            "filename": "mandelbrot.bf",
            "execution_time_ms": 1247,
            "cells": 30000,
            "relative_to_cpp": 0.26,      # 26% of native C++ speed
            "relative_to_js": 1.48,       # 1.48× faster than JS
            "compiler": "bf2wasm",
            "optimizer": "-O3 + loop unrolling"
        },
        {
            "id": "bf-fib",
            "name": "Fibonacci n=40",
            "type": "brainfuck",
            "author": "Classic BF",
            "date": "2026-03-07",
            "description": "fuck compiler (default 30k cells)",
            "filename": "fib.bf",
            "execution_time_ms": 47,
            "cells": 30000,
            "relative_to_cpp": 0.17,
            "relative_to_js": 2.1,
            "compiler": "fuck",
            "optimizer": "default"
        },
        {
            "id": "bf-sieve",
            "name": "Prime Sieve n=1M",
            "type": "brainfuck",
            "author": "Classic BF",
            "date": "2026-03-07",
            "description": "brainfuck2wasm + fixed 16k cells",
            "filename": "sieve.bf",
            "execution_time_ms": 184,
            "cells": 16384,
            "relative_to_cpp": 0.22,
            "relative_to_js": 1.9,
            "compiler": "brainfuck2wasm",
            "optimizer": "16k cells"
        }
    ]
    
    async with INDEX_LOCK:
        idx = await run_io(_read_json_sync, config["index"]) or []
        existing_ids = {item.get("id") for item in idx}
        added = 0
        for ex in examples:
            if ex["id"] not in existing_ids:
                idx.insert(0, ex)
                added += 1
        await run_io(_write_json_sync, config["index"], idx)
        await cache.delete("library:brainfuck")
        await cache.delete("library:all")
    
    return {"success": True, "added": added, "total": len(idx)}
    
# --- 2. UPLOAD JSON ---
@app.post("/api/songs")
async def upload_item(payload: ItemPayload):
    item_id = str(uuid.uuid4())
    date_str = datetime.now().strftime("%Y-%m-%d")
    item_type = payload.type if payload.type in STORAGE_MAP else "song"
    config = STORAGE_MAP[item_type]
    filename = f"{item_id}.json"
    full_path = f"{config['folder']}{filename}" # e.g., songs/uuid.json
    meta = {
        "id": item_id,
        "name": payload.name,
        "author": payload.author,
        "date": date_str,
        "type": item_type,
        "description": payload.description,
        "filename": filename,
        "rating": payload.rating
    }
    # Add meta to the actual data file too
    payload.data["_cloud_meta"] = meta
    async with INDEX_LOCK:
        try:
            # 1. Write the Data File
            await run_io(_write_json_sync, full_path, payload.data)
            # 2. Update the Index
            def _update_index():
                current = _read_json_sync(config["index"])
                current.insert(0, meta)
                _write_json_sync(config["index"], current)
            await run_io(_update_index)
            # Clear cache
            await cache.clear()
            return {"success": True, "id": item_id}
        except Exception as e:
            raise HTTPException(500, f"Upload failed: {str(e)}")
            
# --- 2.5 UPDATE JSON (PUT) ---
@app.put("/api/songs/{item_id}")
async def update_item(item_id: str, payload: ItemPayload):
    # Verify type configuration
    item_type = payload.type if payload.type in STORAGE_MAP else "song"
    config = STORAGE_MAP[item_type]
    # Assume filename is {id}.json (standard convention for this app)
    filename = f"{item_id}.json"
    full_path = f"{config['folder']}{filename}"
    # We preserve the original ID but update other metadata
    # Note: 'date' field in metadata is typically 'created_at'.
    # We could add an 'updated_at' field if needed, but for now we keep 'date' as is or update it?
    # Usually you don't change the creation date.
    # We will fetch the original creation date if possible, or just use current if not found.
    # However, to avoid complexity, we'll just update the metadata entry in the index.
    # Use current date as 'last updated' effectively?
    date_str = datetime.now().strftime("%Y-%m-%d")
    # Or should we try to preserve original date?
    # Let's try to preserve it by reading the index first.
    new_meta = {
        "id": item_id,
        "name": payload.name,
        "author": payload.author,
        "date": date_str, # Defaulting to now if not found
        "type": item_type,
        "description": payload.description,
        "filename": filename,
        "rating": payload.rating
    }
    # Add meta to data
    payload.data["_cloud_meta"] = new_meta
    async with INDEX_LOCK:
        try:
            # 1. Check if file exists (optional, but good for 404)
            # blob = bucket.blob(full_path)
            # if not await run_io(blob.exists):
            # raise HTTPException(404, "Item not found")
            # 2. Write the Data File (Overwrite)
            await run_io(_write_json_sync, full_path, payload.data)
            # 3. Update the Index
            def _update_index_logic():
                current = _read_json_sync(config["index"])
                if not isinstance(current, list):
                    current = []
                # Find and remove existing entry for this ID
                # Also, capture the original date if possible to preserve "Created Date" behavior
                # But user might want "Updated Date". Let's stick to updating it to "now" so it bubbles to top.
                existing_index = next((i for i, item in enumerate(
                    current) if item.get("id") == item_id), -1)
                if existing_index != -1:
                    # Preserve original creation date if desired, but user wants 'latest' usually.
                    # Let's keep it simple: Remove old, insert new at top (Fresh Update)
                    current.pop(existing_index)
                current.insert(0, new_meta)
                _write_json_sync(config["index"], current)
            await run_io(_update_index_logic)
            # Clear cache
            await cache.clear()
            return {"success": True, "id": item_id, "action": "updated"}
        except Exception as e:
            raise HTTPException(500, f"Update failed: {str(e)}")
            
# --- 3. FETCH METADATA (without full data) ---
@app.get("/api/songs/{item_id}/meta")
async def get_item_metadata(item_id: str, type: Optional[str] = Query(None)):
    """
    Returns only the metadata for an item without the full data payload.
    Much faster for listing/details views.
    """
    search_types = [type] if type else ["song", "pattern", "bank"]
   
    for t in search_types:
        config = STORAGE_MAP.get(t)
        if not config:
            continue
           
        # Read from index (much faster than fetching full file)
        index_data = await run_io(_read_json_sync, config["index"])
       
        if isinstance(index_data, list):
            entry = next((item for item in index_data if item.get("id") == item_id), None)
            if entry:
                return entry
   
    raise HTTPException(404, "Item not found")
    
# --- 3.5 FETCH JSON ITEM ---
@app.get("/api/songs/{item_id}")
async def get_item(item_id: str, type: Optional[str] = Query(None)):
    # Try to find the file
    search_types = [type] if type else ["song", "pattern", "bank"]
    for t in search_types:
        config = STORAGE_MAP.get(t)
        filepath = f"{config['folder']}{item_id}.json"
        # Check existence efficiently
        blob = bucket.blob(filepath)
        exists = await run_io(blob.exists)
        if exists:
            data = await run_io(blob.download_as_text)
            return json.loads(data)
    raise HTTPException(404, "Item not found")
    
# --- 4. STREAMING SAMPLES (Upload & Download) ---
@app.post("/api/samples")
async def upload_sample(
    file: UploadFile = File(...),
    author: str = Form(...),
    description: str = Form(""),
    rating: Optional[int] = Form(None)
):
    sample_id = str(uuid.uuid4())
    ext = os.path.splitext(file.filename)[1]
    storage_filename = f"{sample_id}{ext}"
    config = STORAGE_MAP["sample"]
    full_path = f"{config['folder']}{storage_filename}"
    meta = {
        "id": sample_id,
        "name": file.filename,
        "author": author,
        "date": datetime.now().strftime("%Y-%m-%d"),
        "type": "sample",
        "description": description,
        "filename": storage_filename,
        "rating": rating
    }
    async with INDEX_LOCK:
        try:
            # 1. Stream Upload to GCS
            blob = bucket.blob(full_path)
            # GCS Python client doesn't support async streaming upload easily out of the box,
            # but upload_from_file is efficient.
            # We wrap the spooled temp file from FastAPI
            await run_io(blob.upload_from_file, file.file, content_type=file.content_type)
            # 2. Update Index
            def _update_idx():
                idx = _read_json_sync(config["index"])
                idx.insert(0, meta)
                _write_json_sync(config["index"], idx)
            await run_io(_update_idx)
            await cache.delete("library:sample")
            return {"success": True, "id": sample_id}
        except Exception as e:
            raise HTTPException(500, str(e))
            
@app.get("/api/samples/{sample_id}")
async def get_sample(sample_id: str):
    config = STORAGE_MAP["sample"]
    # 1. Lookup in Index (to get original filename/extension)
    idx = await run_io(_read_json_sync, config["index"])
    entry = next((i for i in idx if i["id"] == sample_id), None)
    if not entry:
        raise HTTPException(404, "Sample not found in index")
    blob_path = f"{config['folder']}{entry['filename']}"
    blob = bucket.blob(blob_path)
    if not await run_io(blob.exists):
        raise HTTPException(404, "File missing from storage")
    # 2. Stream Download
    # GCS blob.open() returns a file-like object we can stream
    def iterfile():
        with blob.open("rb") as f:
            while chunk := f.read(1024 * 1024): # 1MB chunks
                yield chunk
    return StreamingResponse(
        iterfile(),
        media_type="application/octet-stream",
        headers={
            "Content-Disposition": f"attachment; filename={entry['name']}"}
    )
    
# --- 4.4 RECORD PLAY ---
@app.post("/api/samples/{sample_id}/play")
async def record_play(sample_id: str):
    """Records that a sample was played by updating last_played timestamp."""
    config = STORAGE_MAP["sample"]
    index_path = config["index"]
    now = datetime.now().isoformat()
    async with INDEX_LOCK:
        try:
            index_data = await run_io(_read_json_sync, index_path)
            if not isinstance(index_data, list):
                raise HTTPException(status_code=500, detail="Sample index is corrupted.")
            entry = next((item for item in index_data if item.get("id") == sample_id), None)
            if not entry:
                raise HTTPException(status_code=404, detail="Sample not found.")
            entry["last_played"] = now
            await run_io(_write_json_sync, index_path, index_data)
           
            await cache.delete("library:sample")
            await cache.delete("library:all")
            return {"success": True, "id": sample_id, "last_played": now}
        except HTTPException:
            raise
        except Exception as e:
            logging.error(f"Failed to record play for {sample_id}: {e}")
            raise HTTPException(status_code=500, detail=f"Failed to record play: {str(e)}")
            
# --- 4.5 UPDATE SAMPLE METADATA ---
class SampleMetaUpdatePayload(BaseModel):
    """Payload for updating a sample's metadata."""
    name: Optional[str] = None
    description: Optional[str] = None
    rating: Optional[int] = Field(
        None, ge=1, le=10, description="A rating from 1 to 10.")
    genre: Optional[str] = None
    last_played: Optional[str] = None # ISO timestamp
    
@app.put("/api/samples/{sample_id}")
async def update_sample_metadata(sample_id: str, payload: SampleMetaUpdatePayload):
    """
    Updates the metadata (name, description, rating) for a sample (e.g., a FLAC file).
    This only modifies the JSON index, not the audio file itself.
    """
    config = STORAGE_MAP["sample"]
    index_path = config["index"]
    async with INDEX_LOCK:
        try:
            # 1. Read the index
            index_data = await run_io(_read_json_sync, index_path)
            if not isinstance(index_data, list):
                raise HTTPException(
                    status_code=500, detail="Sample index is corrupted.")
            # 2. Find the item and its index
            entry_index = next((i for i, item in enumerate(
                index_data) if item.get("id") == sample_id), -1)
            if entry_index == -1:
                raise HTTPException(
                    status_code=404, detail="Sample not found in index.")
            entry = index_data[entry_index]
            update_happened = False
            # 3. Update metadata fields if provided in the payload
            if payload.name is not None and payload.name != entry.get("name"):
                entry["name"] = payload.name
                update_happened = True
            if payload.description is not None and payload.description != entry.get("description"):
                entry["description"] = payload.description
                update_happened = True
            if payload.rating is not None and payload.rating != entry.get("rating"):
                entry["rating"] = payload.rating
                update_happened = True
            if payload.genre is not None and payload.genre != entry.get("genre"):
                entry["genre"] = payload.genre
                update_happened = True
            if payload.last_played is not None and payload.last_played != entry.get("last_played"):
                entry["last_played"] = payload.last_played
                update_happened = True
            if not update_happened:
                return {"success": True, "id": sample_id, "action": "no_change", "message": "No new data provided to update."}
            # 4. Write the updated index back to GCS
            await run_io(_write_json_sync, index_path, index_data)
            # 5. Clear relevant cache
            await cache.delete("library:sample")
            await cache.delete("library:all")
            return {"success": True, "id": sample_id, "action": "metadata_updated"}
        except HTTPException:
            raise # Re-raise FastAPI exceptions
        except Exception as e:
            logging.error(
                f"Failed to update sample metadata for {sample_id}: {e}")
            raise HTTPException(
                status_code=500, detail=f"Failed to update sample metadata: {str(e)}")
            
# --- 4.6 MUSIC ENDPOINTS ---
@app.get("/api/music/{music_id}")
async def get_music_file(music_id: str):
    """Streams a music file from the music folder."""
    config = STORAGE_MAP["music"]
   
    # 1. Lookup in Index
    idx = await run_io(_read_json_sync, config["index"])
    entry = next((i for i in idx if i["id"] == music_id), None)
   
    if not entry:
        raise HTTPException(404, "Music track not found in index")
   
    blob_path = f"{config['folder']}{entry['filename']}"
    blob = bucket.blob(blob_path)
   
    if not await run_io(blob.exists):
        raise HTTPException(404, "File missing from storage")
   
    # 2. Stream Download
    def iterfile():
        with blob.open("rb") as f:
            while chunk := f.read(1024 * 1024): # 1MB chunks
                yield chunk
   
    # Determine content type
    lower_name = entry['filename'].lower()
    if lower_name.endswith('.flac'):
        media_type = 'audio/flac'
    elif lower_name.endswith('.wav'):
        media_type = 'audio/wav'
    elif lower_name.endswith('.mp3'):
        media_type = 'audio/mpeg'
    else:
        media_type = 'audio/mpeg'
   
    return StreamingResponse(
        iterfile(),
        media_type=media_type,
        headers={"Content-Disposition": f'inline; filename="{entry["name"]}"'}
    )
    
@app.put("/api/music/{music_id}")
async def update_music_metadata(music_id: str, payload: SampleMetaUpdatePayload):
    """Updates metadata for a music track."""
    config = STORAGE_MAP["music"]
    index_path = config["index"]
   
    async with INDEX_LOCK:
        try:
            index_data = await run_io(_read_json_sync, index_path)
            if not isinstance(index_data, list):
                raise HTTPException(status_code=500, detail="Music index is corrupted.")
           
            entry_index = next((i for i, item in enumerate(index_data) if item.get("id") == music_id), -1)
            if entry_index == -1:
                raise HTTPException(status_code=404, detail="Music track not found.")
           
            entry = index_data[entry_index]
            update_happened = False
           
            if payload.name is not None and payload.name != entry.get("name"):
                entry["name"] = payload.name
                update_happened = True
            if payload.rating is not None and payload.rating != entry.get("rating"):
                entry["rating"] = payload.rating
                update_happened = True
            if payload.genre is not None and payload.genre != entry.get("genre"):
                entry["genre"] = payload.genre
                update_happened = True
            if payload.last_played is not None and payload.last_played != entry.get("last_played"):
                entry["last_played"] = payload.last_played
                update_happened = True
            if payload.description is not None and payload.description != entry.get("description"):
                entry["description"] = payload.description
                update_happened = True
           
            if update_happened:
                await run_io(_write_json_sync, index_path, index_data)
                await cache.delete("library:music")
                await cache.delete("library:all")
           
            return {"success": True, "id": music_id, "action": "metadata_updated" if update_happened else "no_change"}
           
        except HTTPException:
            raise
        except Exception as e:
            logging.error(f"Failed to update music metadata: {e}")
            raise HTTPException(status_code=500, detail=f"Failed to update: {str(e)}")
            
# --- 5. SMART SYNC (The "Magic" Button) ---
from pydantic import Field

from typing import Optional, List


class MetaPatch(BaseModel):
    name: Optional[str] = None
    rating: Optional[int] = Field(None, ge=0, le=10)
    genre: Optional[str] = None
    tags: Optional[List[str]] = None # full list (replace)
    last_played: Optional[str] = None # ISO string
    
@app.patch("/api/songs/{item_id}")
async def patch_song(item_id: str, patch: MetaPatch):
    config = STORAGE_MAP["song"] # ← FIXED HERE
    index_path = config["index"]
    async with INDEX_LOCK:
        try:
            index: list = await run_io(_read_json_sync, index_path)
            if not isinstance(index, list):
                index = []
            # Find by id
            entry = next((e for e in index if e.get("id") == item_id), None)
            if not entry:
                raise HTTPException(status_code=404, detail="Song not found")
            changes = patch.model_dump(exclude_unset=True)
            if not changes:
                return {"status": "no-op", "message": "Nothing to update"}
            updated = {}
            for field, value in changes.items():
                if field == "tags":
                    entry["tags"] = value if value is not None else []
                else:
                    entry[field] = value
                updated[field] = entry[field]
            await run_io(_write_json_sync, index_path, index)
            await cache.clear() # or cache.delete("library:songs") for precision
            return {
                "status": "success",
                "item_id": item_id,
                "updated": updated
            }
        except Exception as e:
            logging.error(f"PATCH /songs/{item_id} failed: {e}")
            raise HTTPException(status_code=500, detail=str(e))
           
@app.post("/api/admin/sync")
async def sync_gcs_storage():
    """
    Scans Google Cloud Storage to rebuild JSON indexes based on actual files.
    """
    report = {}
    async with INDEX_LOCK:
        for item_type, config in STORAGE_MAP.items():
            if item_type == "default" or item_type == "music":
                continue # Skip music in default sync, handle separately
            added = 0
            removed = 0
            try:
                # 1. List ALL objects in this folder prefix
                # prefix="songs/" returns "songs/123.json", "songs/456.json", etc.
                blobs = await run_io(lambda: list(bucket.list_blobs(prefix=config["folder"])))
                # Filter out the index file itself
                actual_files = []
                for b in blobs:
                    # Remove the folder prefix to get just filename (e.g., "123.json")
                    fname = b.name.replace(config["folder"], "")
                    # Ensure it's not the index file
                    if fname and not b.name.endswith(config["index"]):
                        actual_files.append(fname)
                # 2. Get Current Index
                index_data = await run_io(_read_json_sync, config["index"])
                # 3. Compare
                index_map = {item["filename"]: item for item in index_data}
                disk_set = set(actual_files)
                # Find Ghosts (In Index, Not on Disk)
                new_index = []
                for item in index_data:
                    if item["filename"] in disk_set:
                        new_index.append(item)
                    else:
                        removed += 1
                # Find Orphans (On Disk, Not in Index)
                for filename in actual_files:
                    if filename not in index_map:
                        # Create new entry
                        new_entry = {
                            # Generate new ID or parse from filename if possible
                            "id": str(uuid.uuid4()),
                            "filename": filename,
                            "type": item_type,
                            "date": datetime.now().strftime("%Y-%m-%d"),
                            "name": filename,
                            "author": "Unknown",
                            "description": "Auto-discovered via Sync",
                            "genre": None,
                            "last_played": None
                        }
                        # If JSON, peek inside for metadata
                        if filename.endswith(".json") and item_type in ["song", "pattern", "bank"]:
                            try:
                                b = bucket.blob(
                                    f"{config['folder']}{filename}")
                                content = json.loads(b.download_as_text())
                                if "name" in content:
                                    new_entry["name"] = content["name"]
                                if "author" in content:
                                    new_entry["author"] = content["author"]
                            except:
                                pass
                        new_index.insert(0, new_entry)
                        added += 1
                # 4. Save if changed
                if added > 0 or removed > 0:
                    await run_io(_write_json_sync, config["index"], new_index)
                report[item_type] = {"added": added,
                                     "removed": removed, "status": "synced"}
            except Exception as e:
                report[item_type] = {"error": str(e)}
        await cache.clear()
        return report
        
async def load_metadata(shader_id: str):
    config = STORAGE_MAP["shader"]
    meta_path = f"{config['folder']}{shader_id}/metadata.json"
    try:
        meta = await run_io(_read_json_sync, meta_path)
        if not meta:
            # Fallback: Search in main index
            index = await run_io(_read_json_sync, config["index"])
            meta = next((item for item in index if item["id"] == shader_id), {})
        # Defaults
        meta.setdefault("stars", 0.0)
        meta.setdefault("rating_count", 0)
        meta.setdefault("thumbnail", "")  # For future previews
        return meta
    except Exception as e:
        raise HTTPException(404, f"Shader {shader_id} not found: {str(e)}")
        
async def save_metadata(shader_id: str, meta: dict):
    config = STORAGE_MAP["shader"]
    meta_path = f"{config['folder']}{shader_id}/metadata.json"
    try:
        await run_io(_write_json_sync, meta_path, meta)
        # Also update main index (optional, for consistency)
        async with INDEX_LOCK:
            index = await run_io(_read_json_sync, config["index"])
            for i, item in enumerate(index):
                if item["id"] == shader_id:
                    index[i].update({
                        "stars": meta["stars"],
                        "rating_count": meta["rating_count"],
                        "description": meta.get("description", ""),
                        # Add more fields as needed
                    })
                    break
            else:  # New entry
                index.insert(0, {
                    "id": shader_id,
                    "name": meta.get("name", shader_id),
                    "stars": meta["stars"],
                    "rating_count": meta["rating_count"],
                    "description": meta.get("description", ""),
                    # Defaults...
                })
            await run_io(_write_json_sync, config["index"], index)
        await cache.delete(f"shader:{shader_id}")
        await cache.delete("shaders:list")
    except Exception as e:
        raise HTTPException(500, f"Failed to save metadata: {str(e)}")
# --- SHADER ENDPOINTS ---

@app.get("/api/shaders/{shader_id}")
async def get_shader_meta(shader_id: str):
    return await load_metadata(shader_id)
    
@app.post("/api/shaders/{shader_id}/rate")
async def rate_shader(shader_id: str, stars: float = Form(...)):  # 1.0–5.0
    if not 1 <= stars <= 5:
        raise HTTPException(400, "Stars must be between 1 and 5")
    meta = await load_metadata(shader_id)
    meta["stars"] = (meta["stars"] * meta["rating_count"] + stars) / (meta["rating_count"] + 1)
    meta["rating_count"] += 1
    await save_metadata(shader_id, meta)
    return meta
    
@app.post("/api/shaders/{shader_id}/update")
async def update_shader_meta(shader_id: str, description: str = Form(None), tags: str = Form(None)):
    meta = await load_metadata(shader_id)
    if description:
        meta["description"] = description
    if tags:
        meta["tags"] = [t.strip() for t in tags.split(",")]
    await save_metadata(shader_id, meta)
    return meta
    
@app.get("/api/shaders")
async def list_shaders(
    category: Optional[ShaderCategory] = Query(None),  # ← Use Enum here
    min_stars: float = Query(0.0, ge=0, le=5),
    sort_by: SortBy = Query(SortBy.rating)
):
    cache_key = f"shaders:list:{category}:{min_stars}:{sort_by}"
    cached = await cache.get(cache_key)
    if cached:
        return cached
    config = STORAGE_MAP["shader"]
    try:
        index = await run_io(_read_json_sync, config["index"])
        if not isinstance(index, list):
            index = []
        # Filters
        if category:
            index = [s for s in index if category.value in s.get("tags", []) or category.value.lower() in s.get("description", "").lower()]
        if min_stars > 0:
            index = [s for s in index if s.get("stars", 0) >= min_stars]
        # Sort
        reverse = sort_by in ["rating", "date"]  # Desc for rating/date
        if sort_by == "rating":
            index.sort(key=lambda s: s.get("stars", 0), reverse=reverse)
        elif sort_by == "date":
            index.sort(key=lambda s: s.get("date", ""), reverse=reverse)
        elif sort_by == "name":
            index.sort(key=lambda s: s.get("name", "").lower())
        await cache.set(cache_key, index, ttl=300)  # 5 min cache
        return index
    except Exception as e:
        raise HTTPException(500, f"Failed to list shaders: {str(e)}")
        
@app.post("/api/shaders/upload")
async def upload_shader(
    file: UploadFile = File(...),  # The .wgsl file
    name: str = Form(...),
    description: str = Form(""),
    tags: str = Form(""),  # Comma-separated
    author: str = Form("ford442")
):
    if not file.filename.endswith(".wgsl"):
        raise HTTPException(400, "Only .wgsl files allowed")
    
    shader_id = str(uuid.uuid4())
    storage_filename = f"{shader_id}.wgsl"
    config = STORAGE_MAP["shader"]
    full_path = f"{config['folder']}{storage_filename}"
    
    meta = {
        "id": shader_id,
        "name": name,
        "author": author,
        "date": datetime.now().strftime("%Y-%m-%d"),
        "description": description,
        "tags": [t.strip() for t in tags.split(",")] if tags else [],
        "filename": storage_filename,
        "stars": 0.0,
        "rating_count": 0
    }
    
    async with INDEX_LOCK:
        try:
            # 1. Upload the .wgsl file
            blob = bucket.blob(full_path)
            await run_io(blob.upload_from_file, file.file, content_type="text/plain")
            
            # 2. Save metadata.json
            await save_metadata(shader_id, meta)
            
            # Clear list cache
            await cache.delete("shaders:list")
            
            return {"success": True, "id": shader_id, "meta": meta}
        except Exception as e:
            raise HTTPException(500, f"Shader upload failed: {str(e)}")
            
@app.get("/api/shaders/{shader_id}/code")
async def get_shader_code(shader_id: str):
    """Returns the actual .wgsl shader code."""
    config = STORAGE_MAP["shader"]
    meta = await load_metadata(shader_id)
    blob_path = f"{config['folder']}{meta['filename']}"
    blob = bucket.blob(blob_path)
    
    if not await run_io(blob.exists):
        raise HTTPException(404, "Shader file not found")
    
    code = await run_io(blob.download_as_text)
    return {"id": shader_id, "code": code, "name": meta.get("name")}
    
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)