generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

enum Role {
  USER
  ADMIN
}

enum ImageType {
  COVER
  GALLERY
  OFFER
}

enum OrderStatus {
  PENDING
  COMPLETED
  CANCELLED
}

model User {
  id            String    @id @default(cuid())
  name          String?
  lastName      String?   @map("last_name")
  userName      String    @unique @map("user_name")
  email         String    @unique
  emailVerified DateTime? @map("email_verified")
  image         String?
  password      String?
  role          Role      @default(USER)
  accounts      Account[]
  sessions      Session[]
  orders        Order[]
  reviews       Review[]
  coupons       Coupon[]

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  @@index([email, userName])
  @@map("users")
}

model Account {
  userId            String  @map("user_id")
  type              String
  provider          String
  providerAccountId String  @map("provider_account_id")
  refresh_token     String? @map("refresh_token")
  access_token      String? @map("access_token")
  expires_at        Int?    @map("expires_at")
  token_type        String? @map("token_type")
  scope             String?
  id_token          String? @map("id_token")
  session_state     String? @map("session_state")

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

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

  @@unique([provider, providerAccountId])
  @@map("accounts")
}

model Session {
  sessionToken String   @unique
  userId       String   @map("user_id")
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id])

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  @@map("sessions")
}

model VerificationToken {
  identifier String
  token      String
  expires    DateTime

  @@id([identifier, token])
  @@map("verification_tokens")
}

model Product {
  id            String            @id @default(cuid())
  name          String
  description   String?
  price         Decimal           @db.Decimal(10, 2)
  stock         Int
  coverImageId  String?           @unique @map("cover_image_id")
  categories    ProductCategory[]
  orderItems    OrderItem[]
  reviews       Review[]
  discounts     Discount[]
  offers        Offer[]
  coverImage    Image?            @relation("ProductCoverImage", fields: [coverImageId], references: [id])
  galleryImages Image[]           @relation("ProductGalleryImages")

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  @@index([name])
  @@index([price])
  @@map("products")
}

model Image {
  id        String    @id @default(cuid())
  productId String    @map("product_id")
  url       String
  alt       String?
  type      ImageType

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  productAsCover   Product? @relation("ProductCoverImage")
  productInGallery Product? @relation("ProductGalleryImages", fields: [productId], references: [id])
  offerCover       Offer?

  @@map("images")
}

model Category {
  id          String            @id @default(cuid())
  name        String            @unique
  description String?
  products    ProductCategory[]

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  @@map("categories")
}

model ProductCategory {
  productId  String @map("product_id")
  categoryId String @map("category_id")

  category Category @relation(fields: [categoryId], references: [id])
  product  Product  @relation(fields: [productId], references: [id])

  @@id([productId, categoryId])
  @@map("product_categories")
}

model Order {
  id         String      @id @default(cuid())
  userId     String      @map("user_id")
  totalPrice Decimal     @map("total_price") @db.Decimal(10, 2)
  status     OrderStatus @default(PENDING)
  orderItems OrderItem[]

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  user User @relation(fields: [userId], references: [id])

  @@index([userId, status])
  @@map("orders")
}

model OrderItem {
  orderId   String  @map("order_id")
  productId String  @map("product_id")
  quantity  Int
  price     Decimal @db.Decimal(10, 2)

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  order   Order   @relation(fields: [orderId], references: [id])
  product Product @relation(fields: [productId], references: [id])

  @@id([orderId, productId])
  @@map("order_items")
}

model Discount {
  id              String    @id @default(cuid())
  productId       String    @map("product_id")
  description     String?
  discountPercent Decimal   @map("discount_percent") @db.Decimal(5, 2)
  validFrom       DateTime? @map("valid_from")
  validTo         DateTime? @map("valid_to")

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  product Product @relation(fields: [productId], references: [id])

  @@map("discounts")
}

model Coupon {
  id             String    @id @default(cuid())
  code           String    @unique
  discountAmount Decimal   @map("discount_amount") @db.Decimal(10, 2)
  validFrom      DateTime? @map("valid_from")
  validTo        DateTime? @map("valid_to")
  userId         String    @map("user_id")

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  user User @relation(fields: [userId], references: [id])

  @@map("coupons")
}

model Review {
  id        String  @id @default(cuid())
  productId String  @map("product_id")
  user      User    @relation(fields: [userId], references: [id])
  userId    String  @map("user_id")
  rating    Int
  comment   String?

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  product Product @relation(fields: [productId], references: [id])

  @@map("reviews")
}

model Offer {
  id              String   @id @default(cuid())
  title           String
  description     String?
  coverImageId    String?  @unique @map("cover_image_id")
  coverImage      Image?   @relation(fields: [coverImageId], references: [id])
  productId       String   @map("product_id")
  discountPercent Decimal? @map("discount_percent") @db.Decimal(5, 2)
  validFrom       DateTime @map("valid_from")
  validTo         DateTime @map("valid_to")

  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  product Product @relation(fields: [productId], references: [id])

  @@map("offers")
}