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

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

model User {
  id                    Int       @id @default(autoincrement())
  supabase_uid          String
  email                 String
  display_name          String?

  memberships            Membership[]

  @@map("users")
}

enum ACCOUNT_ACCESS {
  READ_ONLY
  READ_WRITE
  ADMIN
  OWNER
}

model Membership {
  id                      Int         @id @default(autoincrement())
  user_id                 Int
  account_id              Int
  account                 Account     @relation(fields: [account_id], references: [id])
  user                    User        @relation(fields: [user_id], references: [id])
  access                  ACCOUNT_ACCESS    @default(READ_ONLY)
  pending                 Boolean     @default(false)

  @@map("membership")
  @@unique([user_id, account_id])
}

model Account {
  id                      Int         @id @default(autoincrement())
  name                    String
  current_period_ends     DateTime    @default(now()) 
  features                String[]
  plan_id                 Int
  plan                    Plan        @relation(fields: [plan_id], references: [id])
  plan_name               String
  members                 Membership[]
  notes                   Note[]
  max_notes               Int         @default(100)
  stripe_subscription_id  String?
  stripe_customer_id      String?
  max_members             Int        @default(1)
  join_password           String     @unique
  ai_gen_max_pm           Int        @default(7)
  ai_gen_count            Int        @default(0)

  @@map("account")
}

model Plan {
  id                      Int        @id @default(autoincrement())
  name                    String     @unique
  features                String[]
  accounts                Account[]
  max_notes               Int         @default(100)
  stripe_product_id       String?
  max_members             Int        @default(1)
  ai_gen_max_pm           Int        @default(7)

  @@map("plan")
}

model Note {
  id                      Int        @id @default(autoincrement())
  account_id              Int?
  account                 Account?     @relation(fields: [account_id], references: [id])

  note_text               String

  @@map("note")
}
