generator client {
  provider = "prisma-client"
  output   = "./generated/prisma"
}

datasource db {
  provider = "postgresql"
}

model User {
  id                 String              @id @default(cuid())
  email              String              @unique
  password           String
  username           String              @unique
  role               Role                @default(USER)
  score              Int                 @default(0)
  level              Int                 @default(1)
  hp                 Int                 @default(1000)
  maxHp              Int                 @default(1000)
  xp                 Int                 @default(0)
  maxXp              Int                 @default(5000)
  avatar             String?
  createdAt          DateTime            @default(now())
  updatedAt          DateTime            @updatedAt
  bio                String?
  characterClass     CharacterClass?
  eventFeedbacks     EventFeedback[]
  eventRegistrations EventRegistration[]
  preferences        UserPreferences?
  challengesAsChallenger Challenge[] @relation("Challenger")
  challengesAsChallenged Challenge[] @relation("Challenged")
  challengesAsAdmin Challenge[] @relation("AdminValidator")
  challengesAsWinner Challenge[] @relation("ChallengeWinner")
  houseMemberships   HouseMembership[]
  houseInvitationsSent HouseInvitation[] @relation("Inviter")
  houseInvitationsReceived HouseInvitation[] @relation("Invitee")
  houseRequestsSent HouseRequest[] @relation("Requester")
  housesCreated      House[]             @relation("HouseCreator")

  @@index([score])
  @@index([email])
}

model UserPreferences {
  id                    String   @id @default(cuid())
  userId                String   @unique
  homeBackground        String?
  eventsBackground      String?
  leaderboardBackground String?
  theme                 String?  @default("default")
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt
  user                  User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Event {
  id            String              @id @default(cuid())
  date          DateTime
  name          String
  description   String
  type          EventType
  room          String?
  time          String?
  maxPlaces     Int?
  createdAt     DateTime            @default(now())
  updatedAt     DateTime            @updatedAt
  feedbacks     EventFeedback[]
  registrations EventRegistration[]

  @@index([date])
}

model EventRegistration {
  id        String   @id @default(cuid())
  userId    String
  eventId   String
  createdAt DateTime @default(now())
  event     Event    @relation(fields: [eventId], references: [id], onDelete: Cascade)
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, eventId])
  @@index([userId])
  @@index([eventId])
}

model EventFeedback {
  id        String   @id @default(cuid())
  userId    String
  eventId   String
  rating    Int
  comment   String?
  isRead    Boolean  @default(false)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  event     Event    @relation(fields: [eventId], references: [id], onDelete: Cascade)
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, eventId])
  @@index([userId])
  @@index([eventId])
  @@index([isRead])
}

model SitePreferences {
  id                    String   @id @default("global")
  homeBackground        String?
  eventsBackground      String?
  leaderboardBackground String?
  challengesBackground  String?
  eventRegistrationPoints Int    @default(100)
  eventFeedbackPoints  Int      @default(100)
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt
}

enum Role {
  USER
  ADMIN
}

enum EventType {
  ATELIER
  KATA
  PRESENTATION
  LEARNING_HOUR
}

enum CharacterClass {
  WARRIOR
  MAGE
  ROGUE
  RANGER
  PALADIN
  ENGINEER
  MERCHANT
  SCHOLAR
  BERSERKER
  NECROMANCER
}

enum ChallengeStatus {
  PENDING
  ACCEPTED
  COMPLETED
  REJECTED
  CANCELLED
}

model Challenge {
  id              String          @id @default(cuid())
  challengerId   String          // Joueur qui lance le défi
  challengedId   String          // Joueur qui reçoit le défi
  challenger     User            @relation("Challenger", fields: [challengerId], references: [id], onDelete: Cascade)
  challenged     User            @relation("Challenged", fields: [challengedId], references: [id], onDelete: Cascade)
  title           String          // Titre du défi
  description     String          // Description détaillée du défi
  pointsReward    Int             @default(100) // Points à gagner pour le gagnant
  status          ChallengeStatus @default(PENDING)
  adminId         String?         // Admin qui valide le défi
  admin           User?           @relation("AdminValidator", fields: [adminId], references: [id], onDelete: SetNull)
  adminComment    String?         // Commentaire de l'admin lors de la validation/rejet
  winnerId        String?         // ID du gagnant (challengerId ou challengedId)
  winner          User?           @relation("ChallengeWinner", fields: [winnerId], references: [id], onDelete: SetNull)
  createdAt       DateTime        @default(now())
  acceptedAt      DateTime?       // Date d'acceptation du défi
  completedAt     DateTime?       // Date de validation par l'admin
  updatedAt       DateTime        @updatedAt
  
  @@index([challengerId])
  @@index([challengedId])
  @@index([status])
  @@index([adminId])
}

model House {
  id          String            @id @default(cuid())
  name        String
  description String?
  creatorId   String
  creator     User              @relation("HouseCreator", fields: [creatorId], references: [id], onDelete: Cascade)
  createdAt   DateTime          @default(now())
  updatedAt   DateTime          @updatedAt
  memberships HouseMembership[]
  invitations HouseInvitation[]
  requests    HouseRequest[]

  @@index([creatorId])
  @@index([name])
}

model HouseMembership {
  id        String   @id @default(cuid())
  houseId   String
  userId    String
  role      HouseRole @default(MEMBER)
  joinedAt  DateTime @default(now())
  house     House    @relation(fields: [houseId], references: [id], onDelete: Cascade)
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([houseId, userId])
  @@index([houseId])
  @@index([userId])
}

model HouseInvitation {
  id        String   @id @default(cuid())
  houseId   String
  inviterId String   // Utilisateur qui envoie l'invitation
  inviteeId String   // Utilisateur invité
  status    InvitationStatus @default(PENDING)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  house     House    @relation(fields: [houseId], references: [id], onDelete: Cascade)
  inviter   User     @relation("Inviter", fields: [inviterId], references: [id], onDelete: Cascade)
  invitee   User     @relation("Invitee", fields: [inviteeId], references: [id], onDelete: Cascade)

  @@unique([houseId, inviteeId])
  @@index([houseId])
  @@index([inviteeId])
  @@index([status])
}

model HouseRequest {
  id        String   @id @default(cuid())
  houseId   String
  requesterId String // Utilisateur qui demande à rejoindre
  status    RequestStatus @default(PENDING)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  house     House    @relation(fields: [houseId], references: [id], onDelete: Cascade)
  requester User     @relation("Requester", fields: [requesterId], references: [id], onDelete: Cascade)

  @@unique([houseId, requesterId])
  @@index([houseId])
  @@index([requesterId])
  @@index([status])
}

enum HouseRole {
  OWNER
  ADMIN
  MEMBER
}

enum InvitationStatus {
  PENDING
  ACCEPTED
  REJECTED
  CANCELLED
}

enum RequestStatus {
  PENDING
  ACCEPTED
  REJECTED
  CANCELLED
}
