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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
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
from fastapi import APIRouter, HTTPException, UploadFile, File, Form, Request
from fastapi.responses import JSONResponse
import boto3
import json
import os
import time
import logging
import asyncio
from typing import Dict, List, Optional
from pydantic import BaseModel
from datetime import datetime, timedelta
from botocore.exceptions import ClientError
from services.api.api_endpoints import connect_db
from services.api.db.token_utils import decode_token
from services.utils.api_cache import invalidate_cache_pattern

# Configure logging for upload operations
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("upload_endpoints")

# Initialize S3 client
AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
REGION = os.getenv("REGION_NAME", "ap-southeast-1")
BUCKET_NAME = "tlhmaterials"

s3 = boto3.client('s3',
    aws_access_key_id=AWS_ACCESS_KEY,
    aws_secret_access_key=AWS_SECRET_KEY,
    region_name=REGION
)

# Router initialization
router = APIRouter(
    tags=["uploads"],
    responses={404: {"description": "Not found"}},
)

# In-memory storage for active multipart uploads (for quick access)
# Structure: {upload_id: {parts: [], course_id: str, lecture_id: str, upload_key: str, ...}}
active_uploads = {}

# Cache persistence functions for upload state
def save_upload_to_cache(upload_id, upload_info):
    """Save upload information to cache for persistence"""
    try:
        from services.config.valkey_config import get_redis_client, is_connection_available
        
        if not is_connection_available():
            logger.warning("Cache not available, upload data will only be stored in memory")
            return
        
        redis_client = get_redis_client()
        cache_key = f"multipart_upload:{upload_id}"
        
        # Convert datetime objects to ISO strings for JSON serialization
        cache_data = upload_info.copy()
        if isinstance(cache_data.get('created_at'), datetime):
            cache_data['created_at'] = cache_data['created_at'].isoformat()
        if isinstance(cache_data.get('expires_at'), datetime):
            cache_data['expires_at'] = cache_data['expires_at'].isoformat()
        
        # Set expiry based on upload expiry time (24 hours)
        ttl = 24 * 3600  # 24 hours
        redis_client.setex(cache_key, ttl, json.dumps(cache_data))
        logger.info(f"Saved upload {upload_id} to cache with TTL {ttl}s")
        
    except Exception as e:
        logger.error(f"Error saving upload to cache: {e}")

def save_upload_part_to_cache(upload_id, part_number, etag):
    """Save individual part information to cache"""
    try:
        from services.config.valkey_config import get_redis_client, is_connection_available
        
        if not is_connection_available():
            return
        
        redis_client = get_redis_client()
        cache_key = f"multipart_upload_part:{upload_id}:{part_number}"
        
        part_data = {
            'part_number': part_number,
            'etag': etag,
            'uploaded_at': datetime.now().isoformat()
        }
        
        # Set expiry to 24 hours
        ttl = 24 * 3600
        redis_client.setex(cache_key, ttl, json.dumps(part_data))
        logger.debug(f"Saved part {part_number} for upload {upload_id} to cache")
        
    except Exception as e:
        logger.error(f"Error saving upload part to cache: {e}")

def load_upload_from_cache(upload_id):
    """Load upload information from cache"""
    try:
        from services.config.valkey_config import get_redis_client, is_connection_available
        
        if not is_connection_available():
            return None
        
        redis_client = get_redis_client()
        cache_key = f"multipart_upload:{upload_id}"
        
        cached_data = redis_client.get(cache_key)
        if not cached_data:
            return None
        
        upload_info = json.loads(cached_data)
        
        # Convert ISO strings back to datetime objects
        if 'created_at' in upload_info and isinstance(upload_info['created_at'], str):
            upload_info['created_at'] = datetime.fromisoformat(upload_info['created_at'])
        if 'expires_at' in upload_info and isinstance(upload_info['expires_at'], str):
            upload_info['expires_at'] = datetime.fromisoformat(upload_info['expires_at'])
        
        # Load parts data
        parts_pattern = f"multipart_upload_part:{upload_id}:*"
        parts_keys = redis_client.keys(parts_pattern)
        parts = []
        
        for part_key in parts_keys:
            try:
                part_data = json.loads(redis_client.get(part_key))
                parts.append({
                    'PartNumber': part_data['part_number'],
                    'ETag': part_data['etag']
                })
            except Exception as e:
                logger.error(f"Error loading part data from {part_key}: {e}")
        
        # Sort parts by part number
        parts.sort(key=lambda x: x['PartNumber'])
        upload_info['parts'] = parts
        upload_info['parts_received'] = len(parts)
        
        logger.info(f"Loaded upload {upload_id} from cache with {len(parts)} parts")
        return upload_info
        
    except Exception as e:
        logger.error(f"Error loading upload from cache: {e}")
        return None

def remove_upload_from_cache(upload_id):
    """Remove upload and its parts from cache"""
    try:
        from services.config.valkey_config import get_redis_client, is_connection_available
        
        if not is_connection_available():
            return
        
        redis_client = get_redis_client()
        
        # Remove main upload data
        cache_key = f"multipart_upload:{upload_id}"
        redis_client.delete(cache_key)
        
        # Remove all parts
        parts_pattern = f"multipart_upload_part:{upload_id}:*"
        parts_keys = redis_client.keys(parts_pattern)
        if parts_keys:
            redis_client.delete(*parts_keys)
        
        logger.info(f"Removed upload {upload_id} and {len(parts_keys)} parts from cache")
        
    except Exception as e:
        logger.error(f"Error removing upload from cache: {e}")

def get_upload_info(upload_id):
    """Get upload info from memory first, then cache if not found"""
    # Try memory first (fastest)
    if upload_id in active_uploads:
        return active_uploads[upload_id]
    
    # Try cache
    upload_info = load_upload_from_cache(upload_id)
    if upload_info:
        # Cache in memory for faster subsequent access
        active_uploads[upload_id] = upload_info
        return upload_info
    
    return None

# Models
class InitUploadResponse(BaseModel):
    upload_id: str
    presigned_urls: Dict[str, str]
    key: str
    expires_at: datetime

class CompletedPart(BaseModel):
    ETag: str
    PartNumber: int

class CompleteUploadRequest(BaseModel):
    upload_id: str
    parts: List[CompletedPart]

# Helper functions for user info extraction
def get_user_info_from_token(request: Request, auth_token: Optional[str] = None):
    """Extract user information from token without additional verification"""
    if not auth_token:
        auth_header = request.headers.get('Authorization')
        if auth_header and auth_header.startswith('Bearer '):
            auth_token = auth_header.split(' ')[1]
        else:
            raise HTTPException(status_code=401, detail="Authentication token required")
    
    try:
        user_data = decode_token(auth_token)
        return {
            'username': user_data.get('username'),
            'role': user_data.get('role'),
            'user_id': user_data.get('user_id')
        }
    except Exception as e:
        logger.error(f"Token decode error: {str(e)}")
        raise HTTPException(status_code=401, detail="Invalid authentication token")

# Helper to generate upload key for lecture video
def get_lecture_video_key(course_id: int, lecture_id: int):
    return f"videos/cid{course_id}/lid{lecture_id}/vid_lecture.mp4"

@router.post("/upload/init-upload")
async def init_upload(
    request: Request,
    course_id: int = Form(...),
    lecture_id: int = Form(...),
    file_size: int = Form(...),
    file_type: str = Form(...),
    parts: int = Form(...),
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify this instructor owns this course and lecture exists (with retry for newly created lectures)
    max_retries = 3
    retry_delay = 0.5  # 500ms delay between retries
    
    for attempt in range(max_retries):
        conn = connect_db()
        try:
            with conn.cursor() as cursor:
                cursor.execute(
                    "SELECT CourseID FROM Courses WHERE CourseID = %s AND InstructorID = %s",
                    (course_id, instructor_id)
                )
                if not cursor.fetchone():
                    raise HTTPException(status_code=403, detail="Not authorized to modify this course")
                
                # Verify lecture belongs to this course (with retry for newly created lectures)
                cursor.execute(
                    "SELECT LectureID FROM Lectures WHERE LectureID = %s AND CourseID = %s",
                    (lecture_id, course_id)
                )
                lecture_result = cursor.fetchone()
                
                if lecture_result:
                    # Lecture found, break out of retry loop
                    break
                elif attempt < max_retries - 1:
                    # Lecture not found, but we have more retries
                    logger.info(f"Lecture {lecture_id} not found on attempt {attempt + 1}, retrying in {retry_delay}s...")
                    time.sleep(retry_delay)
                    retry_delay *= 1.5  # Exponential backoff
                else:
                    # Final attempt failed
                    raise HTTPException(status_code=404, detail="Lecture not found or doesn't belong to this course")
        finally:
            conn.close()
        
        # If we reached here and haven't broken out, we need to retry
        if attempt < max_retries - 1:
            continue
        else:
            break
    
    # Generate S3 key for the video
    key = get_lecture_video_key(course_id, lecture_id)
    
    # Log upload initialization
    logger.info(f"Initializing upload for course {course_id}, lecture {lecture_id}, size: {file_size}, parts: {parts}")
    logger.info(f"Instructor ID: {instructor_id}, File type: {file_type}")
    
    try:
        # Create multipart upload
        logger.info(f"Creating multipart upload for key: {key}")
        multipart_upload = s3.create_multipart_upload(
            Bucket=BUCKET_NAME,
            Key=key,
            ContentType=file_type,
            ACL='public-read',
            ContentDisposition='inline'
        )
        
        upload_id = multipart_upload['UploadId']
        logger.info(f"Multipart upload created with ID: {upload_id}")
        
        # Generate presigned URLs for each part
        presigned_urls = {}
        logger.info(f"Generating {parts} presigned URLs...")
        for part_number in range(1, parts + 1):
            presigned_url = s3.generate_presigned_url(
                'upload_part',
                Params={
                    'Bucket': BUCKET_NAME,
                    'Key': key,
                    'UploadId': upload_id,
                    'PartNumber': part_number
                },
                ExpiresIn=3600  # 1 hour expiry
            )
            presigned_urls[str(part_number)] = presigned_url
            logger.debug(f"Generated presigned URL for part {part_number}")
        
        logger.info(f"Generated {len(presigned_urls)} presigned URLs")
        
        # Store upload info in both memory and cache
        expires_at = datetime.now() + timedelta(hours=24)
        upload_info = {
            'course_id': course_id,
            'lecture_id': lecture_id,
            'key': key,
            'file_size': file_size,
            'file_type': file_type,
            'parts_expected': parts,
            'parts_received': 0,
            'parts': [],
            'instructor_id': instructor_id,
            'status': 'initialized',
            'created_at': datetime.now(),
            'expires_at': expires_at
        }
        
        # Save to cache for persistence across server restarts
        save_upload_to_cache(upload_id, upload_info)
        
        # Store in memory for faster access
        active_uploads[upload_id] = upload_info
        
        logger.info(f"Upload initialized with ID: {upload_id}")
        
        return JSONResponse({
            'upload_id': upload_id,
            'presigned_urls': presigned_urls,
            'key': key,
            'expires_at': expires_at.isoformat()
        })
        
    except Exception as e:
        logger.error(f"Error initializing upload: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to initialize upload: {str(e)}")

@router.post("/upload/upload-part")
async def upload_part_status(
    request: Request,
    upload_id: str = Form(...),
    part_number: int = Form(...),
    etag: str = Form(...),
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify upload exists and belongs to this instructor
    upload_info = get_upload_info(upload_id)
    if not upload_info:
        raise HTTPException(status_code=404, detail="Upload not found")
    
    if upload_info['instructor_id'] != instructor_id:
        raise HTTPException(status_code=403, detail="Not authorized to access this upload")
     # Check if upload is still valid
    if upload_info['expires_at'] < datetime.now():
        raise HTTPException(status_code=400, detail="Upload expired")
    
    # Validate and clean up ETag format
    def normalize_etag(etag_value):
        """Normalize ETag format to ensure proper S3 compatibility"""
        if not etag_value:
            return None
        
        # Remove any existing quotes first
        clean_etag = etag_value.strip().strip('"')
        
        # Add quotes back for S3 compatibility
        normalized = f'"{clean_etag}"'
        return normalized
    
    etag = normalize_etag(etag)
    if not etag:
        raise HTTPException(status_code=400, detail=f"Invalid ETag provided for part {part_number}")
    
    logger.info(f"Normalized ETag for part {part_number}: {etag}")

    # Add part info
    part_info = {
        'ETag': etag,
        'PartNumber': part_number
    }
    upload_info['parts'].append(part_info)
    
    upload_info['parts_received'] += 1
    
    # Save part to cache for persistence
    save_upload_part_to_cache(upload_id, part_number, etag)
    
    # Update the upload info in cache
    save_upload_to_cache(upload_id, upload_info)
    
    # Log progress with detailed part info
    logger.info(f"Upload {upload_id}: Part {part_number} received with ETag {etag}. Progress: {upload_info['parts_received']}/{upload_info['parts_expected']}")
    logger.info(f"Current parts for upload {upload_id}: {[p['PartNumber'] for p in upload_info['parts']]}")
    
    return JSONResponse({
        'upload_id': upload_id,
        'part_number': part_number,
        'parts_received': upload_info['parts_received'],
        'parts_expected': upload_info['parts_expected'],
        'progress': f"{int((upload_info['parts_received'] / upload_info['parts_expected']) * 100)}%"
    })

@router.post("/upload/complete-upload")
async def complete_upload(
    request: Request,
    upload_id: str = Form(...),
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify upload exists and belongs to this instructor
    upload_info = get_upload_info(upload_id)
    if not upload_info:
        raise HTTPException(status_code=404, detail="Upload not found")
    
    if upload_info['instructor_id'] != instructor_id:
        raise HTTPException(status_code=403, detail="Not authorized to access this upload")
    
    # Check if upload is still valid
    if upload_info['expires_at'] < datetime.now():
        # Try to abort the upload
        try:
            s3.abort_multipart_upload(
                Bucket=BUCKET_NAME,
                Key=upload_info['key'],
                UploadId=upload_id
            )
        except Exception as e:
            logger.error(f"Error aborting expired upload {upload_id}: {str(e)}")
        
        del active_uploads[upload_id]
        raise HTTPException(status_code=400, detail="Upload expired")
    
    # Verify all parts are received
    if upload_info['parts_received'] != upload_info['parts_expected']:
        raise HTTPException(status_code=400, detail=f"Not all parts received. Expected {upload_info['parts_expected']}, got {upload_info['parts_received']}")
    
    try:
        # Sort parts by part number
        parts = sorted(upload_info['parts'], key=lambda x: x['PartNumber'])
        
        # Log the parts being sent to S3
        logger.info(f"Completing upload {upload_id} with {len(parts)} parts:")
        for i, part in enumerate(parts):
            logger.info(f"  Part {i+1}: PartNumber={part['PartNumber']}, ETag={part['ETag']}")
        
        # Complete multipart upload
        try:
            response = s3.complete_multipart_upload(
                Bucket=BUCKET_NAME,
                Key=upload_info['key'],
                UploadId=upload_id,
                MultipartUpload={
                    'Parts': parts
                }
            )
            logger.info(f"S3 complete_multipart_upload response: {response}")
        except ClientError as s3_error:
            logger.error(f"S3 ClientError during completion: {s3_error}")
            raise s3_error
        except Exception as s3_error:
            logger.error(f"S3 Error during completion: {s3_error}")
            raise s3_error
        
        # Get the full video URL
        video_url = f"https://{BUCKET_NAME}.s3-{REGION}.amazonaws.com/{upload_info['key']}"
        
        # Clean up the upload from memory and cache
        course_id = upload_info['course_id']
        lecture_id = upload_info['lecture_id']
        
        # Remove from cache
        remove_upload_from_cache(upload_id)
        
        # Remove from memory
        if upload_id in active_uploads:
            del active_uploads[upload_id]
        
        # Invalidate cache for this course
        invalidate_cache_pattern(f"lectures:id:{lecture_id}:*")
        invalidate_cache_pattern(f"courses:id:{course_id}:*")
        
        logger.info(f"Upload {upload_id} completed successfully for course {course_id}, lecture {lecture_id}")
        
        return JSONResponse({
            'message': 'Upload completed successfully',
            'video_url': video_url,
            'course_id': course_id,
            'lecture_id': lecture_id,
            'key': upload_info['key']
        })
        
    except Exception as e:
        logger.error(f"Error completing upload {upload_id}: {str(e)}")
        
        # Try to abort the upload
        try:
            s3.abort_multipart_upload(
                Bucket=BUCKET_NAME,
                Key=upload_info['key'],
                UploadId=upload_id
            )
        except Exception as abort_error:
            logger.error(f"Error aborting failed upload {upload_id}: {str(abort_error)}")
        
        # Clean up
        if upload_id in active_uploads:
            del active_uploads[upload_id]
            
        raise HTTPException(status_code=500, detail=f"Failed to complete upload: {str(e)}")

@router.post("/upload/abort-upload")
async def abort_upload(
    request: Request,
    upload_id: str = Form(...),
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify upload exists and belongs to this instructor
    upload_info = get_upload_info(upload_id)
    if not upload_info:
        raise HTTPException(status_code=404, detail="Upload not found")
    
    if upload_info['instructor_id'] != instructor_id:
        raise HTTPException(status_code=403, detail="Not authorized to access this upload")
    
    try:
        # Abort multipart upload
        s3.abort_multipart_upload(
            Bucket=BUCKET_NAME,
            Key=upload_info['key'],
            UploadId=upload_id
        )
        
        # Clean up from cache and memory
        course_id = upload_info['course_id']
        lecture_id = upload_info['lecture_id']
        
        remove_upload_from_cache(upload_id)
        if upload_id in active_uploads:
            del active_uploads[upload_id]
        
        logger.info(f"Upload {upload_id} aborted for course {course_id}, lecture {lecture_id}")
        
        return JSONResponse({
            'message': 'Upload aborted successfully',
            'course_id': course_id,
            'lecture_id': lecture_id
        })
        
    except Exception as e:
        logger.error(f"Error aborting upload {upload_id}: {str(e)}")
        
        # Clean up anyway
        if upload_id in active_uploads:
            del active_uploads[upload_id]
            
        raise HTTPException(status_code=500, detail=f"Failed to abort upload: {str(e)}")

@router.get("/upload/status/{upload_id}")
async def get_upload_status(
    request: Request,
    upload_id: str,
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify upload exists and belongs to this instructor
    upload_info = get_upload_info(upload_id)
    if not upload_info:
        raise HTTPException(status_code=404, detail="Upload not found")
    
    if upload_info['instructor_id'] != instructor_id:
        raise HTTPException(status_code=403, detail="Not authorized to access this upload")
    
    # Calculate progress
    progress = int((upload_info['parts_received'] / upload_info['parts_expected']) * 100)
    
    return JSONResponse({
        'upload_id': upload_id,
        'status': upload_info['status'],
        'course_id': upload_info['course_id'],
        'lecture_id': upload_info['lecture_id'],
        'parts_received': upload_info['parts_received'],
        'parts_expected': upload_info['parts_expected'],
        'progress': f"{progress}%",
        'progress_value': progress,
        'created_at': upload_info['created_at'].isoformat(),
        'expires_at': upload_info['expires_at'].isoformat()
    })

@router.get("/upload/active-uploads")
async def list_active_uploads(
    request: Request,
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Filter uploads for this instructor from in-memory storage
    # Note: This only shows uploads that are currently in memory
    # For complete persistence, you'd need to also query cache patterns
    instructor_uploads = {}
    for upload_id, info in active_uploads.items():
        if info['instructor_id'] == instructor_id:
            # Don't include sensitive data
            instructor_uploads[upload_id] = {
                'course_id': info['course_id'],
                'lecture_id': info['lecture_id'],
                'status': info['status'],
                'progress': f"{int((info['parts_received'] / info['parts_expected']) * 100)}%",
                'created_at': info['created_at'].isoformat(),
                'expires_at': info['expires_at'].isoformat()
            }
    
    return JSONResponse({
        'active_uploads': instructor_uploads,
        'count': len(instructor_uploads)
    })

@router.post("/upload/cleanup")
async def cleanup_uploads(
    request: Request,
    auth_token: Optional[str] = None
):
    # Get user info from token (but don't restrict to instructors for cleanup)
    user_info = get_user_info_from_token(request, auth_token)
    
    now = datetime.now()
    expired_uploads = []
    
    # Clean up from memory and also check cache for other expired uploads
    for upload_id in list(active_uploads.keys()):
        upload_info = get_upload_info(upload_id)
        if upload_info and upload_info['expires_at'] < now:
            try:
                # Abort the upload in S3
                s3.abort_multipart_upload(
                    Bucket=BUCKET_NAME,
                    Key=upload_info['key'],
                    UploadId=upload_id
                )
            except Exception as e:
                logger.error(f"Error aborting expired upload {upload_id}: {str(e)}")
            
            # Remove from cache and memory
            remove_upload_from_cache(upload_id)
            if upload_id in active_uploads:
                del active_uploads[upload_id]
            expired_uploads.append(upload_id)
    
    return JSONResponse({
        'cleaned_uploads': expired_uploads,
        'count': len(expired_uploads)
    })

# Periodically clean up expired uploads
async def background_cleanup():
    """Periodically clean up expired uploads"""
    while True:
        now = datetime.now()
        # Create a copy of the keys to avoid dictionary size change during iteration
        upload_ids = list(active_uploads.keys())
        
        for upload_id in upload_ids:
            upload_info = get_upload_info(upload_id)
            if upload_info and upload_info['expires_at'] < now:
                try:
                    # Abort the upload in S3
                    s3.abort_multipart_upload(
                        Bucket=BUCKET_NAME,
                        Key=upload_info['key'],
                        UploadId=upload_id
                    )
                    logger.info(f"Automatically cleaned up expired upload {upload_id}")
                except Exception as e:
                    logger.error(f"Error cleaning up expired upload {upload_id}: {str(e)}")
                
                # Remove from cache and memory
                remove_upload_from_cache(upload_id)
                if upload_id in active_uploads:
                    del active_uploads[upload_id]
        
        # Wait for 1 hour before next cleanup
        await asyncio.sleep(3600)

# Background cleanup task storage
cleanup_task = None

# Function to start background cleanup task
def start_cleanup_task():
    """Start the background cleanup task"""
    global cleanup_task
    if cleanup_task is None or cleanup_task.done():
        cleanup_task = asyncio.create_task(background_cleanup())
        logger.info("Background cleanup task started")
    return cleanup_task

@router.post("/courses/{course_id}/lectures/{lecture_id}/upload-video")
async def upload_video_standard(
    request: Request,
    course_id: int,
    lecture_id: int,
    video: UploadFile = File(...),
    auth_token: Optional[str] = None
):
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify this instructor owns this course and lecture exists (with retry for newly created lectures)
    max_retries = 3
    retry_delay = 0.5  # 500ms delay between retries
    
    for attempt in range(max_retries):
        conn = connect_db()
        try:
            with conn.cursor() as cursor:
                cursor.execute(
                    "SELECT CourseID FROM Courses WHERE CourseID = %s AND InstructorID = %s",
                    (course_id, instructor_id)
                )
                if not cursor.fetchone():
                    raise HTTPException(status_code=403, detail="Not authorized to modify this course")
                
                # Verify lecture belongs to this course (with retry for newly created lectures)
                cursor.execute(
                    "SELECT LectureID FROM Lectures WHERE LectureID = %s AND CourseID = %s",
                    (lecture_id, course_id)
                )
                lecture_result = cursor.fetchone()
                
                if lecture_result:
                    # Lecture found, break out of retry loop
                    break
                elif attempt < max_retries - 1:
                    # Lecture not found, but we have more retries
                    logger.info(f"Lecture {lecture_id} not found on attempt {attempt + 1}, retrying in {retry_delay}s...")
                    time.sleep(retry_delay)
                    retry_delay *= 1.5  # Exponential backoff
                else:
                    # Final attempt failed
                    raise HTTPException(status_code=404, detail="Lecture not found or doesn't belong to this course")
        finally:
            conn.close()
        
        # If we reached here and haven't broken out, we need to retry
        if attempt < max_retries - 1:
            continue
        else:
            break

    try:
        # File validation
        if not video.content_type or not video.content_type.startswith('video/'):
            raise HTTPException(status_code=400, detail="Invalid file type. Please upload a video file")
        
        # Check file size (limit to 100MB for standard upload)
        video.file.seek(0, 2)  # Seek to end
        file_size = video.file.tell()
        video.file.seek(0)  # Reset to beginning
        
        if file_size > 100 * 1024 * 1024:  # 100MB
            raise HTTPException(status_code=400, detail="File too large for standard upload. Use chunked upload for files over 100MB")
        
        # Generate S3 key for the video
        key = get_lecture_video_key(course_id, lecture_id)
        
        logger.info(f"Starting standard upload for course {course_id}, lecture {lecture_id}, size: {file_size}")
        
        # Upload to S3
        s3.upload_fileobj(
            video.file,
            BUCKET_NAME,
            key,
            ExtraArgs={
                'ContentType': video.content_type,
                'ACL': 'public-read',
                'ContentDisposition': 'inline'
            }
        )
        
        # Generate the video URL
        video_url = f"https://{BUCKET_NAME}.s3-{REGION}.amazonaws.com/{key}"
        
        # Invalidate cache for this lecture and course
        invalidate_cache_pattern(f"lectures:id:{lecture_id}:*")
        invalidate_cache_pattern(f"courses:id:{course_id}:*")
        
        logger.info(f"Standard upload completed successfully for course {course_id}, lecture {lecture_id}")
        
        return JSONResponse({
            'message': 'Video uploaded successfully',
            'video_url': video_url,
            'course_id': course_id,
            'lecture_id': lecture_id,
            'key': key,
            'file_size': file_size
        })
        
    except Exception as e:
        logger.error(f"Error in standard upload for course {course_id}, lecture {lecture_id}: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to upload video: {str(e)}")

# Backend-Proxied Chunked Upload Endpoints (to avoid CORS issues)

@router.post("/upload/proxy/init-upload")
async def init_proxy_upload(
    request: Request,
    course_id: int = Form(...),
    lecture_id: int = Form(...),
    file_size: int = Form(...),
    file_type: str = Form(...),
    parts: int = Form(...),
    auth_token: Optional[str] = None
):
    """Initialize a backend-proxied chunked upload"""
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Validate course and lecture ownership (similar to existing init_upload)
    max_retries = 3
    retry_delay = 1.0  # Start with 1 second
    
    for attempt in range(max_retries):
        try:
            conn = connect_db()
            cursor = conn.cursor()
            
            # Check if lecture exists and belongs to instructor
            cursor.execute("""
                SELECT l.LectureID, c.InstructorID 
                FROM Lectures l 
                JOIN Courses c ON l.CourseID = c.CourseID 
                WHERE l.LectureID = %s AND l.CourseID = %s
            """, (lecture_id, course_id))
            
            result = cursor.fetchone()
            if result and result[1] == instructor_id:
                break
            else:
                if attempt == max_retries - 1:
                    conn.close()
                    raise HTTPException(status_code=404, detail="Lecture not found or doesn't belong to this course")
                else:
                    conn.close()
                    await asyncio.sleep(retry_delay)
                    retry_delay *= 1.5
                    continue
                    
        except Exception as e:
            if attempt < max_retries - 1:
                if conn:
                    conn.close()
                await asyncio.sleep(retry_delay)
                retry_delay *= 1.5
                continue
            else:
                if conn:
                    conn.close()
                raise HTTPException(status_code=404, detail="Lecture not found or doesn't belong to this course")
        finally:
            if conn:
                conn.close()
        
        break
    
    # Generate S3 key for the video
    key = get_lecture_video_key(course_id, lecture_id)
    
    logger.info(f"Initializing backend-proxied upload for course {course_id}, lecture {lecture_id}, size: {file_size}, parts: {parts}")
    
    try:
        # Create multipart upload
        multipart_upload = s3.create_multipart_upload(
            Bucket=BUCKET_NAME,
            Key=key,
            ContentType=file_type,
            ACL='public-read',
            ContentDisposition='inline'
        )
        
        upload_id = multipart_upload['UploadId']
        logger.info(f"Backend-proxied multipart upload created with ID: {upload_id}")
        
        # Store upload info in both memory and cache (no presigned URLs needed)
        expires_at = datetime.now() + timedelta(hours=24)
        upload_info = {
            'course_id': course_id,
            'lecture_id': lecture_id,
            'instructor_id': instructor_id,
            'key': key,
            'file_size': file_size,
            'file_type': file_type,
            'parts_expected': parts,
            'parts_received': 0,
            'parts': [],
            'created_at': datetime.now(),
            'expires_at': expires_at,
            'upload_type': 'proxy'  # Mark as backend-proxied
        }
        
        active_uploads[upload_id] = upload_info
        save_upload_to_cache(upload_id, upload_info)
        
        logger.info(f"Backend-proxied upload {upload_id} initialized successfully")
        
        return JSONResponse({
            'upload_id': upload_id,
            'parts_expected': parts,
            'message': 'Backend-proxied upload initialized successfully'
        })
        
    except Exception as e:
        logger.error(f"Error initializing backend-proxied upload: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to initialize upload: {str(e)}")


@router.post("/upload/proxy/upload-part")
async def upload_part_proxy(
    request: Request,
    upload_id: str = Form(...),
    part_number: int = Form(...),
    chunk: UploadFile = File(...),
    auth_token: Optional[str] = None
):
    """Upload a single part through backend proxy"""
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify upload exists and belongs to this instructor
    upload_info = get_upload_info(upload_id)
    if not upload_info:
        raise HTTPException(status_code=404, detail="Upload not found")
    
    if upload_info['instructor_id'] != instructor_id:
        raise HTTPException(status_code=403, detail="Not authorized to access this upload")
    
    # Check if upload is still valid
    if upload_info['expires_at'] < datetime.now():
        raise HTTPException(status_code=400, detail="Upload expired")
    
    # Verify this is a backend-proxied upload
    if upload_info.get('upload_type') != 'proxy':
        raise HTTPException(status_code=400, detail="This upload is not configured for backend proxy")
    
    try:
        # Read the chunk data
        chunk_data = await chunk.read()
        
        logger.info(f"Uploading part {part_number} for upload {upload_id} via backend proxy (size: {len(chunk_data)} bytes)")
        
        # Upload the part directly to S3 via backend
        response = s3.upload_part(
            Bucket=BUCKET_NAME,
            Key=upload_info['key'],
            PartNumber=part_number,
            UploadId=upload_id,
            Body=chunk_data
        )
        
        etag = response['ETag']
        logger.info(f"Part {part_number} uploaded successfully via backend proxy, ETag: {etag}")
        
        # Add part info
        part_info = {
            'ETag': etag,
            'PartNumber': part_number
        }
        upload_info['parts'].append(part_info)
        upload_info['parts_received'] += 1
        
        # Save part to cache for persistence
        save_upload_part_to_cache(upload_id, part_number, etag)
        
        # Update the upload info in cache
        save_upload_to_cache(upload_id, upload_info)
        
        logger.info(f"Backend-proxied upload {upload_id}: Part {part_number} received. Progress: {upload_info['parts_received']}/{upload_info['parts_expected']}")
        
        return JSONResponse({
            'upload_id': upload_id,
            'part_number': part_number,
            'etag': etag,
            'parts_received': upload_info['parts_received'],
            'parts_expected': upload_info['parts_expected'],
            'progress': f"{int((upload_info['parts_received'] / upload_info['parts_expected']) * 100)}%"
        })
        
    except Exception as e:
        logger.error(f"Error uploading part {part_number} via backend proxy for upload {upload_id}: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Failed to upload part: {str(e)}")


@router.post("/upload/proxy/complete-upload")
async def complete_proxy_upload(
    request: Request,
    upload_id: str = Form(...),
    auth_token: Optional[str] = None
):
    """Complete a backend-proxied chunked upload"""
    # Get user info from token
    user_info = get_user_info_from_token(request, auth_token)
    instructor_id = user_info['user_id']
    
    # Verify upload exists and belongs to this instructor
    upload_info = get_upload_info(upload_id)
    if not upload_info:
        raise HTTPException(status_code=404, detail="Upload not found")
    
    if upload_info['instructor_id'] != instructor_id:
        raise HTTPException(status_code=403, detail="Not authorized to access this upload")
    
    # Check if upload is still valid
    if upload_info['expires_at'] < datetime.now():
        # Try to abort the upload
        try:
            s3.abort_multipart_upload(
                Bucket=BUCKET_NAME,
                Key=upload_info['key'],
                UploadId=upload_id
            )
        except Exception as e:
            logger.error(f"Error aborting expired backend-proxied upload {upload_id}: {str(e)}")
        
        if upload_id in active_uploads:
            del active_uploads[upload_id]
        raise HTTPException(status_code=400, detail="Upload expired")
    
    # Verify this is a backend-proxied upload
    if upload_info.get('upload_type') != 'proxy':
        raise HTTPException(status_code=400, detail="This upload is not configured for backend proxy")
    
    # Verify all parts are received
    if upload_info['parts_received'] != upload_info['parts_expected']:
        raise HTTPException(status_code=400, detail=f"Not all parts received. Expected {upload_info['parts_expected']}, got {upload_info['parts_received']}")
    
    try:
        # Sort parts by part number
        parts = sorted(upload_info['parts'], key=lambda x: x['PartNumber'])
        
        logger.info(f"Completing backend-proxied upload {upload_id} with {len(parts)} parts")
        
        # Complete multipart upload
        response = s3.complete_multipart_upload(
            Bucket=BUCKET_NAME,
            Key=upload_info['key'],
            UploadId=upload_id,
            MultipartUpload={
                'Parts': parts
            }
        )
        
        # Get the full video URL
        video_url = f"https://{BUCKET_NAME}.s3-{REGION}.amazonaws.com/{upload_info['key']}"
        
        # Clean up the upload from memory and cache
        course_id = upload_info['course_id']
        lecture_id = upload_info['lecture_id']
        
        # Remove from cache
        remove_upload_from_cache(upload_id)
        
        # Remove from memory
        if upload_id in active_uploads:
            del active_uploads[upload_id]
        
        # Invalidate cache for this course
        invalidate_cache_pattern(f"lectures:id:{lecture_id}:*")
        invalidate_cache_pattern(f"courses:id:{course_id}:*")
        
        logger.info(f"Backend-proxied upload {upload_id} completed successfully for course {course_id}, lecture {lecture_id}")
        
        return JSONResponse({
            'message': 'Backend-proxied upload completed successfully',
            'video_url': video_url,
            'course_id': course_id,
            'lecture_id': lecture_id,
            'key': upload_info['key']
        })
        
    except Exception as e:
        logger.error(f"Error completing backend-proxied upload {upload_id}: {str(e)}")
        
        # Try to abort the upload
        try:
            s3.abort_multipart_upload(
                Bucket=BUCKET_NAME,
                Key=upload_info['key'],
                UploadId=upload_id
            )
        except Exception as abort_error:
            logger.error(f"Error aborting failed backend-proxied upload {upload_id}: {str(abort_error)}")
        
        # Clean up
        if upload_id in active_uploads:
            del active_uploads[upload_id]
            
        raise HTTPException(status_code=500, detail=f"Failed to complete upload: {str(e)}")