// 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   = "client"
}

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

model User {
  id                  String        @id @default(uuid()) @db.Uuid
  email               String        @unique
  name                String
  avatar              String?
  settings            Setting[]
  categories          Category[]
  projects            Project[]
  fields              Field[]
  files               File[]
  currencies          Currency[]
  transactions        Transaction[]
  createdAt           DateTime      @default(now()) @map("created_at")
  updatedAt           DateTime      @updatedAt @map("updated_at")
  stripeCustomerId    String?       @map("stripe_customer_id")
  membershipPlan      String?       @map("membership_plan")
  membershipExpiresAt DateTime?     @map("membership_expires_at")
  emailVerified       Boolean       @default(false) @map("is_email_verified")
  storageUsed         Int           @default(0) @map("storage_used")
  storageLimit        Int           @default(-1) @map("storage_limit")
  aiBalance           Int           @default(0) @map("ai_balance")
  businessName        String?       @map("business_name")
  businessAddress     String?       @map("business_address")
  businessBankDetails String?       @map("business_bank_details")
  businessLogo        String?       @map("business_logo")
  accounts            Account[]
  sessions            Session[]
  appData             AppData[]
  progress            Progress[]

  @@map("users")
}

model Session {
  id        String   @id @default(uuid()) @db.Uuid
  token     String
  expiresAt DateTime @map("expires_at")
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")
  ipAddress String?  @map("ip_address")
  userAgent String?  @map("user_agent")
  userId    String   @map("user_id") @db.Uuid
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([token])
  @@map("sessions")
}

model Account {
  id                    String    @id
  accountId             String    @map("account_id")
  providerId            String    @map("provider_id")
  userId                String    @map("user_id") @db.Uuid
  user                  User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  accessToken           String?   @map("access_token")
  refreshToken          String?   @map("refresh_token")
  idToken               String?   @map("id_token")
  accessTokenExpiresAt  DateTime? @map("access_token_expires_at")
  refreshTokenExpiresAt DateTime? @map("refresh_token_expires_at")
  scope                 String?
  password              String?
  createdAt             DateTime  @default(now()) @map("created_at")
  updatedAt             DateTime  @updatedAt @map("updated_at")

  @@map("account")
}

model Verification {
  id         String   @id @default(uuid()) @db.Uuid
  identifier String
  value      String
  expiresAt  DateTime @map("expires_at")
  createdAt  DateTime @default(now()) @map("created_at")
  updatedAt  DateTime @updatedAt @map("updated_at")

  @@map("verification")
}

model Setting {
  id          String  @id @default(uuid()) @db.Uuid
  userId      String  @map("user_id") @db.Uuid
  user        User    @relation(fields: [userId], references: [id], onDelete: Cascade)
  code        String
  name        String
  description String?
  value       String?

  @@unique([userId, code])
  @@map("settings")
}

model Category {
  id           String        @id @default(uuid()) @db.Uuid
  userId       String        @map("user_id") @db.Uuid
  user         User?         @relation(fields: [userId], references: [id], onDelete: Cascade)
  code         String
  name         String
  color        String        @default("#000000")
  llm_prompt   String?
  transactions Transaction[]
  createdAt    DateTime      @default(now()) @map("created_at")

  @@unique([userId, code])
  @@map("categories")
}

model Project {
  id           String        @id @default(uuid()) @db.Uuid
  userId       String        @map("user_id") @db.Uuid
  user         User          @relation(fields: [userId], references: [id], onDelete: Cascade)
  code         String
  name         String
  color        String        @default("#000000")
  llm_prompt   String?
  transactions Transaction[]
  createdAt    DateTime      @default(now()) @map("created_at")

  @@unique([userId, code])
  @@map("projects")
}

model Field {
  id                  String   @id @default(uuid()) @db.Uuid
  userId              String   @map("user_id") @db.Uuid
  user                User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  code                String
  name                String
  type                String   @default("string")
  llm_prompt          String?
  options             Json?
  createdAt           DateTime @default(now()) @map("created_at")
  isVisibleInList     Boolean  @default(false) @map("is_visible_in_list")
  isVisibleInAnalysis Boolean  @default(false) @map("is_visible_in_analysis")
  isRequired          Boolean  @default(false) @map("is_required")
  isExtra             Boolean  @default(true) @map("is_extra")

  @@unique([userId, code])
  @@map("fields")
}

model File {
  id         String   @id @default(uuid()) @db.Uuid
  userId     String   @map("user_id") @db.Uuid
  user       User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  filename   String
  path       String
  mimetype   String
  metadata   Json?
  isReviewed Boolean  @default(false) @map("is_reviewed")
  createdAt  DateTime @default(now()) @map("created_at")

  @@map("files")
}

model Transaction {
  id                    String    @id @default(uuid()) @db.Uuid
  userId                String    @map("user_id") @db.Uuid
  user                  User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  name                  String?
  description           String?
  merchant              String?
  total                 Int?
  currencyCode          String?   @map("currency_code")
  convertedTotal        Int?      @map("converted_total")
  convertedCurrencyCode String?   @map("converted_currency_code")
  type                  String?   @default("expense")
  note                  String?
  files                 Json      @default("[]")
  extra                 Json?
  category              Category? @relation(fields: [categoryCode, userId], references: [code, userId])
  categoryCode          String?   @map("category_code")
  project               Project?  @relation(fields: [projectCode, userId], references: [code, userId])
  projectCode           String?   @map("project_code")
  issuedAt              DateTime? @map("issued_at")
  createdAt             DateTime  @default(now()) @map("created_at")
  updatedAt             DateTime  @updatedAt @map("updated_at")
  text                  String?

  @@index([userId])
  @@index([projectCode])
  @@index([categoryCode])
  @@index([issuedAt])
  @@index([name])
  @@index([merchant])
  @@index([total])
  @@map("transactions")
}

model Currency {
  id     String  @id @default(uuid()) @db.Uuid
  userId String? @map("user_id") @db.Uuid
  user   User?   @relation(fields: [userId], references: [id], onDelete: Cascade)
  code   String
  name   String

  @@unique([userId, code])
  @@map("currencies")
}

model AppData {
  id     String @id @default(uuid()) @db.Uuid
  app    String
  userId String @map("user_id") @db.Uuid
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  data   Json

  @@unique([userId, app])
  @@map("app_data")
}

model Progress {
  id        String   @id @default(uuid()) @db.Uuid
  userId    String   @map("user_id") @db.Uuid
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  type      String
  data      Json?
  current   Int      @default(0)
  total     Int      @default(0)
  createdAt DateTime @default(now()) @map("created_at")

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