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

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

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

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

model User {
  id           Int        @id @default(autoincrement())
  username     String     @unique
  email        String     @unique
  passwordHash String
  avatarUrl String? @db.VarChar(512)
  createdAt    DateTime   @default(now())
  updatedAt    DateTime   @default(now()) @updatedAt
  Deal         Deal[]
  votes        DealVote[]
  comments Comment[]
  companies    Seller[]      
  domains      SellerDomain[] 
  dealVoteHistory  DealVoteHistory[]
}

enum DealStatus {
  PENDING
  ACTIVE
  EXPIRED
  REJECTED
}

enum SaleType{
  ONLINE
  OFFLINE
  CODE
}

enum AffiliateType{
  AFFILIATE
  NON_AFFILIATE
  USER_AFFILIATE
}

  model SellerDomain {
    id        Int      @id @default(autoincrement())
    domain    String   @unique  
    sellerId Int
    seller   Seller  @relation(fields: [sellerId], references: [id])

    createdAt DateTime @default(now())
    createdById Int
    createdBy User     @relation(fields: [createdById], references: [id])
  }

  model Seller {
    id        Int      @id @default(autoincrement())
    name      String   @unique
    url       String   @default("")
    isActive  Boolean  @default(true)
    createdAt DateTime @default(now())
    createdById Int

    deals     Deal[]
    createdBy User     @relation(fields: [createdById], references: [id])
    domains     SellerDomain[]
  }

model Deal {
  id          Int         @id @default(autoincrement())
  title       String
  description String?
  url         String?
  price       Float?

  userId      Int
  score       Int         @default(0)
  commentCount Int       @default(0)
  status      DealStatus  @default(PENDING)
  saletype SaleType @default(ONLINE)
  affiliateType AffiliateType @default(NON_AFFILIATE)

  sellerId     Int?
  customSeller String?
  
  seller       Seller?   @relation(fields: [sellerId], references: [id])
  createdAt   DateTime    @default(now())
  updatedAt   DateTime    @updatedAt

  user      User        @relation(fields: [userId], references: [id])
  votes     DealVote[]
  voteHistory  DealVoteHistory[]
  comments  Comment[]
  images    DealImage[]  // ← yeni ilişki
}
model DealImage {
  id        Int      @id @default(autoincrement())
  imageUrl  String   @db.VarChar(512)
  order     Int      @default(0)
  createdAt DateTime @default(now())
  dealId    Int

  deal Deal @relation(fields: [dealId], references: [id], onDelete: Cascade)
}

model DealVote {
  id         Int      @id @default(autoincrement())
  dealId     Int
  userId     Int
  voteType   Int      @default(0) // -1,0,1
  createdAt  DateTime @default(now())
  lastVotedAt DateTime @default(now()) // her vote değişiminde set edeceğiz

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

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

model DealVoteHistory {
  id        Int      @id @default(autoincrement())
  dealId    Int
  userId    Int
  voteType  Int
  createdAt DateTime @default(now())

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

  @@index([dealId])
  @@index([userId])
  @@index([createdAt])
}


model Comment {
  id        Int      @id @default(autoincrement())
  text      String
  createdAt DateTime @default(now())
  userId    Int
  dealId    Int

  user User @relation(fields: [userId], references: [id])
  deal Deal @relation(fields: [dealId], references: [id])
}