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

datasource db {
  provider = "postgresql"
}

model User {
  id            String    @id
  name          String
  email         String    @unique
  emailVerified Boolean   @default(false)
  image         String?
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt

  sessions Session[]
  accounts Account[]

  @@map("user")
}

model Session {
  id        String   @id
  expiresAt DateTime
  token     String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  ipAddress String?
  userAgent String?
  userId    String
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId])
  @@map("session")
}

model Account {
  id                    String    @id
  accountId             String
  providerId            String
  userId                String
  user                  User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  accessToken           String?
  refreshToken          String?
  idToken               String?
  accessTokenExpiresAt  DateTime?
  refreshTokenExpiresAt DateTime?
  scope                 String?
  password              String?
  createdAt             DateTime  @default(now())
  updatedAt             DateTime  @updatedAt

  @@index([userId])
  @@map("account")
}

model Verification {
  id         String   @id
  identifier String
  value      String
  expiresAt  DateTime
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt

  @@index([identifier])
  @@map("verification")
}

model TelegramChat {
  id          String    @id @default(cuid())
  chatId      BigInt?   @unique
  code        String    @unique
  validatedAt DateTime?
  createdAt   DateTime  @default(now())

  @@map("telegram_chats")
}

model TelegramDailyMaxNotification {
  id        String    @id @default(cuid())
  date      DateTime
  quoteType QuoteType
  maxValue  Decimal   @db.Money
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  @@unique([date, quoteType])
  @@map("telegram_daily_max_notifications")
}

enum QuoteType {
  BLUE
  BNA
  BELO
}

model Quote {
  id                    Int                   @id @default(autoincrement())
  timeStamp             DateTime              @default(now())
  buy                   Decimal               @db.Money
  sell                  Decimal               @db.Money
  type                  QuoteType
  difference            Decimal               @default(0) @db.Decimal(9, 2)
  percentage            Decimal               @default(0) @db.Decimal(9, 2)
  previousDayDifference Decimal               @default(0) @db.Decimal(9, 2)
  previousDayPercentage Decimal               @default(0) @db.Decimal(9, 2)

  @@map("quotes")
}


model QuoteHistory {
  id              Int       @id @default(autoincrement())
  timeStamp       DateTime  @default(now())
  buy             Decimal   @db.Money
  sell            Decimal   @db.Money
  type            QuoteType

  @@map("quotes_history")
}

// Expenses

enum PaymentStatus {
  PENDING
  PAYED
}

model Wallet {
  id        Int       @id @default(autoincrement())
  name      String    @db.VarChar(100)
  currency  String    @db.VarChar(10)
  balance   Decimal   @db.Decimal(14, 4)
  isDefault Boolean   @default(false)
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  movements WalletMovement[]
  expenses  Expense[]

  @@map("wallets")
}

model WalletMovement {
  id                Int       @id @default(autoincrement())
  walletId          Int
  wallet            Wallet    @relation(fields: [walletId], references: [id])
  type              String    @db.VarChar(20)
  amount            Decimal   @db.Decimal(14, 4)
  description       String?   @db.VarChar(255)
  referenceWalletId Int?
  transferGroupId   String?   @db.Uuid
  createdAt         DateTime  @default(now())

  @@index([walletId])
  @@index([transferGroupId])
  @@map("wallet_movements")
}

model PeriodicExpense {
  id            Int       @id @default(autoincrement())
  description   String    @db.VarChar(50)
  defaultDueDay Int
  defaultAmount Decimal   @db.Money
  periods       Int[]
  isDeleted     Boolean   @default(false)
  createdAt     DateTime  @default(now())

  expenses      Expense[]

  @@map("periodic_expenses")
}

model Expense {
  id                Int              @id @default(autoincrement())
  description       String           @db.VarChar(50)
  periodicExpense   PeriodicExpense? @relation(fields: [periodicExpenseId], references: [id])
  periodicExpenseId Int?
  year              Int
  month             Int
  amount            Decimal          @db.Money
  amountPayed       Decimal?         @db.Money
  beloPrice         Decimal?         @db.Money
  usdcEquivalent    Decimal?         @db.Decimal(12, 6)
  status            PaymentStatus    @default(PENDING)
  dueDate           DateTime
  paymentDate       DateTime?
  walletId          Int?
  wallet            Wallet?          @relation(fields: [walletId], references: [id])

  @@index([walletId])
  @@map("expenses")
}