File size: 27,522 Bytes
4acc19f
 
 
 
 
 
 
 
d189be0
 
4acc19f
db3d93b
 
 
4acc19f
 
db3d93b
4acc19f
 
 
d189be0
 
 
 
 
 
db3d93b
 
 
 
 
 
 
 
 
 
 
 
d189be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
add3ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f0bbdc
 
2ed6bf1
2f0bbdc
 
d189be0
 
4acc19f
db3d93b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4acc19f
 
d189be0
 
db3d93b
 
d189be0
 
 
 
 
 
 
4acc19f
 
 
 
 
d189be0
4acc19f
d189be0
 
4acc19f
d189be0
4acc19f
405ccd5
 
 
 
d189be0
 
 
 
 
 
 
4acc19f
 
d189be0
4acc19f
 
 
 
 
d189be0
db3d93b
4acc19f
d189be0
 
4acc19f
 
 
d189be0
4acc19f
 
 
f3abb0d
db3d93b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d189be0
f3abb0d
 
add3ead
 
 
 
 
 
 
 
f3abb0d
 
add3ead
 
 
 
 
 
 
 
f3abb0d
 
 
 
 
d189be0
f3abb0d
d189be0
 
f3abb0d
d189be0
f3abb0d
 
 
 
 
d189be0
 
 
 
 
 
 
f3abb0d
 
d189be0
f3abb0d
 
 
 
 
d189be0
f3abb0d
 
d189be0
 
f3abb0d
 
 
d189be0
f3abb0d
 
 
d189be0
add3ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d189be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db3d93b
 
 
d189be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffebaa3
 
 
 
 
 
 
 
 
b876bc7
ffebaa3
 
 
 
 
 
 
 
 
 
 
 
 
 
d189be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f0bbdc
 
 
 
2ed6bf1
 
 
 
 
 
 
 
 
2f0bbdc
 
 
 
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
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
}

// ================ ENUMS =================

enum USER_STATUS {
  PENDING_EMAIL_VERIFICATION
  PENDING_PHONE_VERIFICATION
  PENDING_PROFILE_COMPLETION
  ENABLED
  DISABLED
  SUSPENDED
  INVITED
}

enum SESSION_STATUS {
  CURRENT
  EXPIRED
  REVOKED
}

enum OTP_TYPE {
  EMAIL
  SMS
}

enum OTP_PURPOSE {
  EMAIL_VERIFICATION
  PHONE_VERIFICATION
  PASSWORD_RESET
  LOGIN_2FA
}

enum UPLOAD_STATUS {
  UPLOADING
  PROCESSING
  COMPLETED
  FAILED
}

enum NOTIFICATION_TYPE {
  NEW_MOVIE
  NEW_SERIES
  NEW_EPISODE
  COMMENT_REPLY
  COMMENT_LIKE
  CONTENT_LIKE
  ACCOUNT_UPDATE
  SUBSCRIPTION_REMINDER
  RECOMMENDATION
}

enum CONTENT_TYPE {
  MOVIE
  SERIES
  EPISODE
}

enum TWO_FACTOR_METHOD_TYPE {
  EMAIL
  AUTHENTICATOR // TOTP
  PASSKEY // WebAuthn / FIDO2
  PUSH_APPROVAL // Mobile yes/no
}

enum PUSH_APPROVAL_STATUS {
  PENDING
  APPROVED
  DENIED
  EXPIRED
}

enum PLATFORM_TYPE {
  ANDROID
  IOS
  WEB
}

enum URL_SHORTENER_PROVIDER {
  SHLINK
  TRIM_KUTT
}

// ============ USER MANAGEMENT ============

model User {
  id                String      @id @default(uuid())
  uniqueId          String      @unique
  email             String?     @unique
  phone             String?     @unique
  code              String?
  firstName         String?
  lastName          String?
  firstNameLastName String?
  lastNameFirstName String?
  profileImage      String?
  bio               String?     @db.Text
  dateOfBirth       DateTime?   @db.Timestamp(3)
  country           String?
  city              String?
  status            USER_STATUS @default(PENDING_EMAIL_VERIFICATION)
  emailVerified     Boolean     @default(false)
  phoneVerified     Boolean     @default(false)
  lastLogin         DateTime?   @db.Timestamp(3)
  createdAt         DateTime    @default(now()) @db.Timestamp(3)
  updatedAt         DateTime    @updatedAt @db.Timestamp(3)
  isDeleted         Boolean     @default(false)

  // Relations
  sessions       UserSession[]
  credentials    UserCredential[]
  otps           UserOtp[]
  interests      UserInterest[]
  watchHistories WatchHistory[]
  notifications  Notification[]
  playlists      Playlist[]
  likes          Like[]
  ratings        Rating[]
  comments       Comment[]
  commentLikes   CommentLike[]

  @@map("users")
}

model UserSession {
  id           String         @id @default(uuid())
  userId       String
  accessToken  String         @db.Text
  refreshToken String         @db.Text
  ipAddress    String?
  userAgent    String?        @db.Text
  geoIpCountry String?
  city         String?
  state        String?
  latitude     Float?
  longitude    Float?
  status       SESSION_STATUS @default(CURRENT)
  loginAt      DateTime?      @db.Timestamp(3)
  logoutAt     DateTime?      @db.Timestamp(3)
  expiredAt    DateTime?      @db.Timestamp(3)
  createdAt    DateTime       @default(now()) @db.Timestamp(3)
  updatedAt    DateTime       @updatedAt @db.Timestamp(3)
  isDeleted    Boolean        @default(false)

  // Relations
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("user_sessions")
}

model UserCredential {
  id        String   @id @default(uuid())
  userId    String   @unique
  password  String
  createdAt DateTime @default(now()) @db.Timestamp(3)
  updatedAt DateTime @updatedAt @db.Timestamp(3)
  isDeleted Boolean  @default(false)

  // Relations
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("user_credentials")
}

model UserOtp {
  id         String      @id @default(uuid())
  userId     String
  otp        String
  type       OTP_TYPE
  purpose    OTP_PURPOSE
  expiresAt  DateTime    @db.Timestamp(3)
  verified   Boolean     @default(false)
  verifiedAt DateTime?   @db.Timestamp(3)
  attempts   Int         @default(0)
  createdAt  DateTime    @default(now()) @db.Timestamp(3)
  updatedAt  DateTime    @updatedAt @db.Timestamp(3)
  isDeleted  Boolean     @default(false)

  // Relations
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId, type, verified])
  @@map("user_otps")
}

model UserInterest {
  id        String   @id @default(uuid())
  userId    String
  genreId   String
  createdAt DateTime @default(now()) @db.Timestamp(3)

  // Relations
  user  User  @relation(fields: [userId], references: [id], onDelete: Cascade)
  genre Genre @relation(fields: [genreId], references: [id], onDelete: Cascade)

  @@unique([userId, genreId])
  @@index([userId])
  @@index([genreId])
  @@map("user_interests")
}

// ============ SUPER ADMIN ============

model SuperAdmin {
  id                 String   @id @default(uuid())
  email              String   @unique
  name               String
  profileImage       String?
  isTwoFactorEnabled Boolean  @default(false)
  createdAt          DateTime @default(now()) @db.Timestamp(3)
  updatedAt          DateTime @updatedAt @db.Timestamp(3)
  isDeleted          Boolean  @default(false)

  // Relations
  sessions               SuperAdminSession[]
  credentials            SuperAdminCredential[]
  twoFactorMethods       SuperAdminTwoFactorMethod[]
  recoveryCodes          SuperAdminRecoveryCode[]
  trustedDevices         SuperAdminTrustedDevice[]
  webAuthnCredentials    SuperAdminWebAuthnCredential[]
  pushDevices            SuperAdminPushDevice[]
  pushApprovalChallenges SuperAdminPushApprovalChallenge[]

  @@map("super_admins")
}

model SuperAdminSession {
  id           String         @id @default(uuid())
  superAdminId String
  accessToken  String         @db.Text
  refreshToken String         @db.Text
  ipAddress    String?
  userAgent    String?        @db.Text
  geoIpCountry String?
  city         String?
  state        String?
  latitude     Float?
  longitude    Float?
  isFromAdmin  Boolean        @default(false)
  status       SESSION_STATUS @default(CURRENT)
  loginAt      DateTime?      @db.Timestamp(3)
  expiredAt    DateTime?      @db.Timestamp(3)
  createdAt    DateTime       @default(now()) @db.Timestamp(3)
  updatedAt    DateTime       @updatedAt @db.Timestamp(3)
  isDeleted    Boolean        @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@map("super_admin_sessions")
}

model SuperAdminCredential {
  id           String   @id @default(uuid())
  superAdminId String
  password     String
  createdAt    DateTime @default(now()) @db.Timestamp(3)
  updatedAt    DateTime @updatedAt @db.Timestamp(3)
  isDeleted    Boolean  @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@map("super_admin_credentials")
}

// ============ SUPER ADMIN 2FA ============

model SuperAdminTwoFactorMethod {
  id           String                 @id @default(uuid())
  superAdminId String
  type         TWO_FACTOR_METHOD_TYPE
  isEnabled    Boolean                @default(false)
  isPrimary    Boolean                @default(false)
  createdAt    DateTime               @default(now()) @db.Timestamp(3)
  updatedAt    DateTime               @updatedAt @db.Timestamp(3)
  isDeleted    Boolean                @default(false)

  // Relations
  superAdmin       SuperAdmin                  @relation(fields: [superAdminId], references: [id], onDelete: Cascade)
  emailTwoFactor   SuperAdminEmailTwoFactor?
  totpTwoFactor    SuperAdminTotpTwoFactor?
  passkeyTwoFactor SuperAdminPasskeyTwoFactor?
  pushTwoFactor    SuperAdminPushTwoFactor?

  @@unique([superAdminId, type])
  @@index([superAdminId])
  @@map("super_admin_two_factor_methods")
}

model SuperAdminEmailTwoFactor {
  id                String    @id @default(uuid())
  methodId          String    @unique
  lastCodeHash      String?
  lastCodeExpiresAt DateTime? @db.Timestamp(3)
  lastSentAt        DateTime? @db.Timestamp(3)
  failedAttempts    Int       @default(0)
  createdAt         DateTime  @default(now()) @db.Timestamp(3)
  updatedAt         DateTime  @updatedAt @db.Timestamp(3)
  isDeleted         Boolean   @default(false)

  // Relations
  method SuperAdminTwoFactorMethod @relation(fields: [methodId], references: [id], onDelete: Cascade)

  @@map("super_admin_email_two_factor")
}

model SuperAdminTotpTwoFactor {
  id              String    @id @default(uuid())
  methodId        String    @unique
  secretEncrypted String
  confirmedAt     DateTime? @db.Timestamp(3)
  createdAt       DateTime  @default(now()) @db.Timestamp(3)
  updatedAt       DateTime  @updatedAt @db.Timestamp(3)
  isDeleted       Boolean   @default(false)

  // Relations
  method SuperAdminTwoFactorMethod @relation(fields: [methodId], references: [id], onDelete: Cascade)

  @@map("super_admin_totp_two_factor")
}

model SuperAdminPasskeyTwoFactor {
  id        String   @id @default(uuid())
  methodId  String   @unique
  createdAt DateTime @default(now()) @db.Timestamp(3)
  updatedAt DateTime @updatedAt @db.Timestamp(3)
  isDeleted Boolean  @default(false)

  // Relations
  method SuperAdminTwoFactorMethod @relation(fields: [methodId], references: [id], onDelete: Cascade)

  @@map("super_admin_passkey_two_factor")
}

model SuperAdminPushTwoFactor {
  id        String   @id @default(uuid())
  methodId  String   @unique
  createdAt DateTime @default(now()) @db.Timestamp(3)
  updatedAt DateTime @updatedAt @db.Timestamp(3)
  isDeleted Boolean  @default(false)

  // Relations
  method SuperAdminTwoFactorMethod @relation(fields: [methodId], references: [id], onDelete: Cascade)

  @@map("super_admin_push_two_factor")
}

model SuperAdminRecoveryCode {
  id           String    @id @default(uuid())
  superAdminId String
  codeHash     String
  usedAt       DateTime? @db.Timestamp(3)
  createdAt    DateTime  @default(now()) @db.Timestamp(3)
  isDeleted    Boolean   @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@index([superAdminId])
  @@map("super_admin_recovery_codes")
}

model SuperAdminWebAuthnCredential {
  id           String    @id @default(uuid())
  superAdminId String
  credentialId String    @unique
  publicKey    String    @db.Text
  counter      Int       @default(0)
  deviceName   String?
  createdAt    DateTime  @default(now()) @db.Timestamp(3)
  lastUsedAt   DateTime? @db.Timestamp(3)
  isDeleted    Boolean   @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@index([superAdminId])
  @@map("super_admin_webauthn_credentials")
}

model SuperAdminTrustedDevice {
  id           String   @id @default(uuid())
  superAdminId String
  deviceId     String   @unique
  deviceName   String?
  ipAddress    String?
  userAgent    String?  @db.Text
  lastUsedAt   DateTime @default(now()) @db.Timestamp(3)
  expiresAt    DateTime @db.Timestamp(3)
  createdAt    DateTime @default(now()) @db.Timestamp(3)
  isDeleted    Boolean  @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@index([superAdminId])
  @@index([deviceId])
  @@map("super_admin_trusted_devices")
}

model SuperAdminPushDevice {
  id           String        @id @default(uuid())
  superAdminId String
  pushToken    String
  platform     PLATFORM_TYPE
  deviceName   String?
  lastSeenAt   DateTime      @default(now()) @db.Timestamp(3)
  createdAt    DateTime      @default(now()) @db.Timestamp(3)
  updatedAt    DateTime      @updatedAt @db.Timestamp(3)
  isDeleted    Boolean       @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@index([superAdminId])
  @@map("super_admin_push_devices")
}

model SuperAdminPushApprovalChallenge {
  id           String               @id @default(uuid())
  superAdminId String
  status       PUSH_APPROVAL_STATUS @default(PENDING)
  ipAddress    String?
  userAgent    String?              @db.Text
  location     String?
  deviceInfo   String?
  expiresAt    DateTime             @db.Timestamp(3)
  approvedAt   DateTime?            @db.Timestamp(3)
  deniedAt     DateTime?            @db.Timestamp(3)
  createdAt    DateTime             @default(now()) @db.Timestamp(3)
  isDeleted    Boolean              @default(false)

  // Relations
  superAdmin SuperAdmin @relation(fields: [superAdminId], references: [id], onDelete: Cascade)

  @@index([superAdminId])
  @@index([status])
  @@map("super_admin_push_approval_challenges")
}

// ============ MOVIES ============

model Movie {
  id           String        @id @default(uuid())
  title        String
  slug         String        @unique
  description  String?       @db.Text
  thumbnailUrl String
  bannerUrl    String?
  trailerUrl   String?
  rating       String?
  releaseYear  Int?
  language     String?
  duration     Int?
  imdbId       String?
  tmdbId       String?
  isFeatured   Boolean       @default(false)
  uploadStatus UPLOAD_STATUS @default(COMPLETED)
  views        Int           @default(0)
  createdAt    DateTime      @default(now()) @db.Timestamp(3)
  updatedAt    DateTime      @updatedAt @db.Timestamp(3)
  isDeleted    Boolean       @default(false)

  // Relations
  genres        MovieGenre[]
  tags          MovieTag[]
  video         MovieVideo?
  comments      Comment[]
  likes         Like[]
  ratings       Rating[]
  playlistItems PlaylistItem[]
  watchHistory  WatchHistory[]

  @@index([isDeleted])
  @@index([isFeatured])
  @@index([slug])
  @@map("movies")
}

model MovieVideo {
  id           String        @id @default(uuid())
  movieId      String        @unique
  videoUrl     String
  videoKey     String?
  fileSize     BigInt?
  quality      String?
  uploadStatus UPLOAD_STATUS @default(COMPLETED)
  createdAt    DateTime      @default(now()) @db.Timestamp(3)
  updatedAt    DateTime      @updatedAt @db.Timestamp(3)
  isDeleted    Boolean       @default(false)

  // Relations
  movie Movie @relation(fields: [movieId], references: [id], onDelete: Cascade)

  @@index([isDeleted])
  @@map("movie_videos")
}

model MovieGenre {
  id        String   @id @default(uuid())
  movieId   String
  genreId   String
  createdAt DateTime @default(now()) @db.Timestamp(3)

  // Relations
  movie Movie @relation(fields: [movieId], references: [id], onDelete: Cascade)
  genre Genre @relation(fields: [genreId], references: [id], onDelete: Cascade)

  @@unique([movieId, genreId])
  @@index([movieId])
  @@index([genreId])
  @@map("movie_genres")
}

// ============ SERIES ============

model Series {
  id           String        @id @default(uuid())
  title        String
  slug         String        @unique
  description  String?       @db.Text
  thumbnailUrl String
  bannerUrl    String?
  trailerUrl   String?
  rating       String?
  releaseYear  Int?
  language     String?
  imdbId       String?
  tmdbId       String?
  isFeatured   Boolean       @default(false)
  uploadStatus UPLOAD_STATUS @default(COMPLETED)
  views        Int           @default(0)
  createdAt    DateTime      @default(now()) @db.Timestamp(3)
  updatedAt    DateTime      @updatedAt @db.Timestamp(3)
  isDeleted    Boolean       @default(false)

  // Relations
  genres        SeriesGenre[]
  tags          SeriesTag[]
  seasons       Season[]
  comments      Comment[]
  likes         Like[]
  ratings       Rating[]
  playlistItems PlaylistItem[]
  watchHistory  WatchHistory[]

  @@index([isDeleted])
  @@index([isFeatured])
  @@index([slug])
  @@map("series")
}

model Season {
  id           String   @id @default(uuid())
  seriesId     String
  seasonNumber Int
  title        String
  description  String?  @db.Text
  thumbnailUrl String?
  releaseYear  Int?
  createdAt    DateTime @default(now()) @db.Timestamp(3)
  updatedAt    DateTime @updatedAt @db.Timestamp(3)
  isDeleted    Boolean  @default(false)

  // Relations
  series   Series    @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  episodes Episode[]

  @@unique([seriesId, seasonNumber])
  @@index([seriesId])
  @@map("seasons")
}

model Episode {
  id            String   @id @default(uuid())
  seasonId      String
  episodeNumber Int
  title         String
  description   String?  @db.Text
  thumbnailUrl  String?
  duration      Int
  createdAt     DateTime @default(now()) @db.Timestamp(3)
  updatedAt     DateTime @updatedAt @db.Timestamp(3)
  isDeleted     Boolean  @default(false)

  // Relations
  season        Season         @relation(fields: [seasonId], references: [id], onDelete: Cascade)
  video         EpisodeVideo?
  comments      Comment[]
  likes         Like[]
  ratings       Rating[]
  playlistItems PlaylistItem[]
  watchHistory  WatchHistory[]

  @@unique([seasonId, episodeNumber])
  @@index([seasonId])
  @@map("episodes")
}

model EpisodeVideo {
  id           String        @id @default(uuid())
  episodeId    String        @unique
  videoUrl     String
  videoKey     String?
  fileSize     BigInt?
  quality      String?
  uploadStatus UPLOAD_STATUS @default(COMPLETED)
  createdAt    DateTime      @default(now()) @db.Timestamp(3)
  updatedAt    DateTime      @updatedAt @db.Timestamp(3)
  isDeleted    Boolean       @default(false)

  // Relations
  episode Episode @relation(fields: [episodeId], references: [id], onDelete: Cascade)

  @@index([isDeleted])
  @@map("episode_videos")
}

model SeriesGenre {
  id        String   @id @default(uuid())
  seriesId  String
  genreId   String
  createdAt DateTime @default(now()) @db.Timestamp(3)

  // Relations
  series Series @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  genre  Genre  @relation(fields: [genreId], references: [id], onDelete: Cascade)

  @@unique([seriesId, genreId])
  @@index([seriesId])
  @@index([genreId])
  @@map("series_genres")
}

// ============ GENRES ============

model Genre {
  id          String   @id @default(uuid())
  name        String   @unique
  slug        String   @unique
  description String?  @db.Text
  icon        String?
  sortOrder   Int      @default(0)
  createdAt   DateTime @default(now()) @db.Timestamp(3)
  updatedAt   DateTime @updatedAt @db.Timestamp(3)
  isDeleted   Boolean  @default(false)

  // Relations
  movieGenres   MovieGenre[]
  seriesGenres  SeriesGenre[]
  userInterests UserInterest[]

  @@index([slug])
  @@map("genres")
}

// ============ UNIFIED COMMENT ============

model Comment {
  id          String       @id @default(uuid())
  content     String       @db.Text
  userId      String
  contentType CONTENT_TYPE
  movieId     String?
  seriesId    String?
  episodeId   String?
  parentId    String?
  createdAt   DateTime     @default(now()) @db.Timestamp(3)
  updatedAt   DateTime     @updatedAt @db.Timestamp(3)
  isDeleted   Boolean      @default(false)

  // Relations
  user    User          @relation(fields: [userId], references: [id], onDelete: Cascade)
  movie   Movie?        @relation(fields: [movieId], references: [id], onDelete: Cascade)
  series  Series?       @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  episode Episode?      @relation(fields: [episodeId], references: [id], onDelete: Cascade)
  parent  Comment?      @relation("CommentReplies", fields: [parentId], references: [id], onDelete: Cascade)
  replies Comment[]     @relation("CommentReplies")
  likes   CommentLike[]

  @@index([movieId, isDeleted])
  @@index([seriesId, isDeleted])
  @@index([episodeId, isDeleted])
  @@index([userId, isDeleted])
  @@index([contentType, isDeleted])
  @@map("comments")
}

// ============ UNIFIED LIKE ============

model Like {
  id          String       @id @default(uuid())
  userId      String
  contentType CONTENT_TYPE
  movieId     String?
  seriesId    String?
  episodeId   String?
  isLike      Boolean
  createdAt   DateTime     @default(now()) @db.Timestamp(3)

  // Relations
  user    User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  movie   Movie?   @relation(fields: [movieId], references: [id], onDelete: Cascade)
  series  Series?  @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  episode Episode? @relation(fields: [episodeId], references: [id], onDelete: Cascade)

  // Only one of movieId/seriesId/episodeId should be non-null per row (enforce in code)
  @@unique([userId, movieId])
  @@unique([userId, seriesId])
  @@unique([userId, episodeId])
  @@index([movieId, contentType])
  @@index([seriesId, contentType])
  @@index([episodeId, contentType])
  @@map("likes")
}

// ============ COMMENT LIKE ============

model CommentLike {
  id        String   @id @default(uuid())
  userId    String
  commentId String
  isLike    Boolean
  createdAt DateTime @default(now()) @db.Timestamp(3)

  // Relations
  user    User    @relation(fields: [userId], references: [id], onDelete: Cascade)
  comment Comment @relation(fields: [commentId], references: [id], onDelete: Cascade)

  @@unique([userId, commentId])
  @@index([commentId])
  @@map("comment_likes")
}

// ============ PLAYLISTS ============

model Playlist {
  id          String   @id @default(uuid())
  name        String
  description String?  @db.Text
  userId      String
  createdAt   DateTime @default(now()) @db.Timestamp(3)
  updatedAt   DateTime @updatedAt @db.Timestamp(3)
  isDeleted   Boolean  @default(false)

  // Relations
  user  User           @relation(fields: [userId], references: [id], onDelete: Cascade)
  items PlaylistItem[]

  @@index([userId, isDeleted])
  @@map("playlists")
}

model PlaylistItem {
  id          String       @id @default(uuid())
  playlistId  String
  contentType CONTENT_TYPE
  movieId     String?
  seriesId    String?
  episodeId   String?
  position    Int
  createdAt   DateTime     @default(now()) @db.Timestamp(3)

  // Relations
  playlist Playlist @relation(fields: [playlistId], references: [id], onDelete: Cascade)
  movie    Movie?   @relation(fields: [movieId], references: [id], onDelete: Cascade)
  series   Series?  @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  episode  Episode? @relation(fields: [episodeId], references: [id], onDelete: Cascade)

  // Only one of movieId/seriesId/episodeId should be non-null per row (enforce in code)
  @@unique([playlistId, movieId])
  @@unique([playlistId, seriesId])
  @@unique([playlistId, episodeId])
  @@index([playlistId])
  @@index([movieId, contentType])
  @@index([seriesId, contentType])
  @@index([episodeId, contentType])
  @@map("playlist_items")
}

// ============ WATCH HISTORY ============

model WatchHistory {
  id          String       @id @default(uuid())
  userId      String
  contentType CONTENT_TYPE
  movieId     String?
  seriesId    String?
  episodeId   String?
  watchedAt   DateTime     @default(now()) @db.Timestamp(3)
  watchTime   Int
  duration    Int
  completed   Boolean      @default(false)

  // Relations
  user    User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  movie   Movie?   @relation(fields: [movieId], references: [id], onDelete: Cascade)
  series  Series?  @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  episode Episode? @relation(fields: [episodeId], references: [id], onDelete: Cascade)

  @@index([userId, contentType])
  @@index([movieId])
  @@index([seriesId])
  @@index([episodeId])
  @@map("watch_history")
}

// ============ NOTIFICATIONS ============

model Notification {
  id        String            @id @default(uuid())
  userId    String
  type      NOTIFICATION_TYPE
  title     String
  message   String            @db.Text
  read      Boolean           @default(false)
  data      Json?
  createdAt DateTime          @default(now()) @db.Timestamp(3)
  isDeleted Boolean           @default(false)

  // Relations
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId, isDeleted])
  @@index([userId, read, isDeleted])
  @@map("notifications")
}

// ============ RATINGS (Star Ratings 1-5) ============

model Rating {
  id          String       @id @default(uuid())
  userId      String
  contentType CONTENT_TYPE
  movieId     String?
  seriesId    String?
  episodeId   String?
  value       Int
  createdAt   DateTime     @default(now()) @db.Timestamp(3)
  updatedAt   DateTime     @updatedAt @db.Timestamp(3)

  // Relations
  user    User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  movie   Movie?   @relation(fields: [movieId], references: [id], onDelete: Cascade)
  series  Series?  @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  episode Episode? @relation(fields: [episodeId], references: [id], onDelete: Cascade)

  // Only one of movieId/seriesId/episodeId should be non-null per row (enforce in code)
  @@unique([userId, movieId])
  @@unique([userId, seriesId])
  @@unique([userId, episodeId])
  @@index([movieId, contentType])
  @@index([seriesId, contentType])
  @@index([episodeId, contentType])
  @@map("ratings")
}

// ============ UPLOADS ============

model Upload {
  id            String    @id @default(uuid())
  fileName      String
  fileType      String // 'image', 'video', 'document'
  originalSize  Int?
  // For videos (HLS)
  hlsUrl        String? // URL to playlist.m3u8
  hlsFolderId   String? // folder containing HLS files
  hlsSegments   Json? // Array of segment URLs
  videoDuration Float? // Duration in seconds
  status        String    @default("pending") // pending, processing, completed, failed
  errorMessage  String?
  uploadedAt    DateTime  @default(now()) @db.Timestamp(3)
  processedAt   DateTime?
  metadata      Json?

  @@index([status])
  @@index([fileType])
  @@index([uploadedAt])
  @@map("uploads")
}

// ============ TAGS (Flexible Content Tags) ============

model Tag {
  id        String   @id @default(uuid())
  name      String   @unique
  slug      String   @unique
  createdAt DateTime @default(now()) @db.Timestamp(3)
  updatedAt DateTime @updatedAt @db.Timestamp(3)
  isDeleted Boolean  @default(false)

  // Relations
  movieTags  MovieTag[]
  seriesTags SeriesTag[]

  @@index([slug])
  @@map("tags")
}

model MovieTag {
  id        String   @id @default(uuid())
  movieId   String
  tagId     String
  createdAt DateTime @default(now()) @db.Timestamp(3)

  // Relations
  movie Movie @relation(fields: [movieId], references: [id], onDelete: Cascade)
  tag   Tag   @relation(fields: [tagId], references: [id], onDelete: Cascade)

  @@unique([movieId, tagId])
  @@index([movieId])
  @@index([tagId])
  @@map("movie_tags")
}

model SeriesTag {
  id        String   @id @default(uuid())
  seriesId  String
  tagId     String
  createdAt DateTime @default(now()) @db.Timestamp(3)

  // Relations
  series Series @relation(fields: [seriesId], references: [id], onDelete: Cascade)
  tag    Tag    @relation(fields: [tagId], references: [id], onDelete: Cascade)

  @@unique([seriesId, tagId])
  @@index([seriesId])
  @@index([tagId])
  @@map("series_tags")
}

// ============ SHORT URL ============

model ShortUrl {
  id         String                 @id @default(uuid())
  longUrl    String                 @db.Text
  shortUrl   String
  shortCode  String?
  externalId String? // Provider's internal ID (e.g., Kutt's ID)
  provider   URL_SHORTENER_PROVIDER
  createdAt  DateTime               @default(now()) @db.Timestamp(3)
  updatedAt  DateTime               @updatedAt @db.Timestamp(3)
  isDeleted  Boolean                @default(false)

  @@index([provider])
  @@map("short_urls")
}