// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

datasource db {
  provider = "mysql"
  // NOTE: When using mysql or sqlserver, uncomment the @db.Text annotations in model Account below
  // Further reading:
  // https://next-auth.js.org/adapters/prisma#create-the-prisma-schema
  // https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#string
  url      = env("DATABASE_URL")
}

// Necessary for Next auth
model Account {
  id                       String  @id @default(cuid())
  userId                   String
  type                     String
  provider                 String
  providerAccountId        String
  refresh_token            String? @db.Text
  access_token             String? // @db.Text
  expires_at               Int?
  token_type               String?
  scope                    String?
  id_token                 String? // @db.Text
  session_state            String?
  user                     User    @relation(fields: [userId], references: [id], onDelete: Cascade)
  refresh_token_expires_in Int?

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  balance       Float     @default(1000)

  accounts Account[]
  sessions Session[]
  shops    Shop[]
  carts    Cart[]
  adresses Adress[]
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

//////////////////////
// SHOP
//////////////////////

model Shop {
  id     Int    @id
  userId String
  label  String

  user      User       @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
  items     Item[]
  sellables Sellable[]
}

model Item {
  item_name String
  shopId    Int
  stock     Int

  shop      Shop       @relation(fields: [shopId], references: [id], onDelete: Cascade, onUpdate: Cascade)
  sellables Sellable[]

  @@id([item_name, shopId])
}

model Sellable {
  id        String  @id @default(cuid())
  item_name String
  shopId    Int
  amount    Int
  price     Float
  enabled   Boolean @default(true)

  shop Shop @relation(fields: [shopId], references: [id], onDelete: Cascade)
  item Item @relation(fields: [item_name, shopId], references: [item_name, shopId], onDelete: Cascade)

  cartItems CartItem[]
}

//////////////////////
// CART
//////////////////////

model Cart {
  userId String @id

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

model CartItem {
  itemId   String @id
  quantity Int
  cartId   String

  cart     Cart     @relation(fields: [cartId], references: [userId], onDelete: Cascade, onUpdate: Cascade)
  sellable Sellable @relation(fields: [itemId], references: [id], onDelete: Cascade, onUpdate: Cascade)
}

//////////////////////
// ADDRESS
//////////////////////

model Adress {
  id     String @id @default(cuid())
  userId String
  adress String

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