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

datasource db {
  provider     = "mysql"
  url          = env("DATABASE_URL")
  relationMode = "prisma"
}

// 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)

  @@unique([provider, providerAccountId])
  @@index([userId])
}

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

  @@index([userId])
}

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  superAdmin    Boolean   @default(false) @map("super_admin")

  createDate DateTime @default(now())

  accounts      Account[]
  sessions      Session[]
  runs          Run[]
  Agent         Agent[]
  organizations OrganizationUser[]

  @@index([email])
  @@index([createDate])
}

model Organization {
  id   String @id @default(cuid())
  name String @unique

  created_by  String
  create_date DateTime  @default(now())
  update_date DateTime? @updatedAt
  delete_date DateTime?

  users OrganizationUser[]

  @@index([name])
  @@map("organization")
}

model OrganizationUser {
  id              String    @id @default(cuid())
  user_id         String
  organization_id String
  role            String    @default("member")
  create_date     DateTime  @default(now())
  update_date     DateTime? @updatedAt
  delete_date     DateTime?

  user         User         @relation(fields: [user_id], references: [id])
  organization Organization @relation(fields: [organization_id], references: [id])

  @@unique([user_id, organization_id])
  @@index([user_id])
  @@index([organization_id])
  @@map("organization_user")
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

model OAuthCredentials {
  id              String  @id @default(cuid())
  user_id         String
  organization_id String?
  provider        String
  state           String
  redirect_uri    String

  // Post install
  token_type              String?
  access_token_enc        String?   @db.Text
  access_token_expiration DateTime?
  refresh_token_enc       String?   @db.Text
  scope                   String?

  create_date DateTime  @default(now())
  update_date DateTime? @updatedAt
  delete_date DateTime?

  @@index([state])
  @@index([user_id])
  @@map("oauth_credentials")
}

model Agent {
  id         String      @id @default(cuid())
  userId     String
  name       String      @db.Text
  goal       String      @db.Text
  deleteDate DateTime?
  createDate DateTime    @default(now())
  user       User        @relation(fields: [userId], references: [id], onDelete: Cascade)
  tasks      AgentTask[]

  @@index([createDate])
  @@index([userId, deleteDate, createDate])
}

model AgentTask {
  id         String    @id @default(cuid())
  agentId    String
  type       String
  status     String?
  value      String    @db.Text
  info       String?   @db.Text
  sort       Int
  deleteDate DateTime?
  createDate DateTime  @default(now())
  agent      Agent     @relation(fields: [agentId], references: [id], onDelete: Cascade)

  @@index([agentId])
  @@index([type])
  @@index([createDate])
}

model Run {
  id         String   @id @default(cuid())
  userId     String
  createDate DateTime @default(now())

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

  @@index([userId, createDate])
  @@index([userId])
}

model NewRun {
  id          String   @id @default(cuid())
  user_id     String
  create_date DateTime @default(now())
  goal        String   @db.Text

  tasks Task[]

  @@index([user_id, create_date])
  @@index([user_id])
  @@map("agent_run")
}

model Task {
  id          String   @id @default(cuid())
  run_id      String
  type        String
  create_date DateTime @default(now())

  run NewRun @relation(fields: [run_id], references: [id])

  @@index([run_id])
  @@map("agent_task")
}
