generator client {
  provider   = "prisma-client-js"
  output     = "./generated/gateway"
  engineType = "binary"
}

datasource db {
  provider = "postgresql"
}

enum OAuthType {
  NONE
  KAKAO
}

enum GatewayProfileStatus {
  RESERVED
  PREOPEN
  RUNNING
  PAUSED
  COMPLETED
  STOPPED
  DISABLED
}

enum GatewayBuildStatus {
  IDLE
  QUEUED
  RUNNING
  FAILED
  SUCCEEDED
}

enum GatewayOperationType {
    RESET
    START
    STOP
}

enum GatewayOperationStatus {
    QUEUED
    RUNNING
    SUCCEEDED
    FAILED
    CANCELLED
}

enum GatewaySourceMode {
    BRANCH
    COMMIT
}

model AppUser {
  id            String    @id @default(uuid())
  loginId       String    @unique @map("login_id")
  displayName   String    @unique @map("display_name")
  passwordHash  String    @map("password_hash")
  passwordSalt  String    @map("password_salt")
  roles         Json      @default(dbgenerated("'[]'::jsonb"))
  sanctions     Json      @default(dbgenerated("'{}'::jsonb"))
  oauthType     OAuthType @default(NONE) @map("oauth_type")
  oauthId       String?   @unique @map("oauth_id")
  email         String?   @unique
  oauthInfo     Json      @default(dbgenerated("'{}'::jsonb")) @map("oauth_info")
  picture       String    @default("default.jpg")
  imageServer   Int       @default(0) @map("image_server")
  iconUpdatedAt DateTime? @map("icon_updated_at")
  thirdPartyUse Boolean   @default(true) @map("third_party_use")
  termsAcceptedAt DateTime? @map("terms_accepted_at")
  privacyAcceptedAt DateTime? @map("privacy_accepted_at")
  kakaoVerifiedAt DateTime? @map("kakao_verified_at")
  kakaoGraceStartedAt DateTime @default(now()) @map("kakao_grace_started_at")
  deleteAfter   DateTime? @map("delete_after")
  createdAt     DateTime  @default(now()) @map("created_at")
  updatedAt     DateTime  @updatedAt @map("updated_at")
  lastLoginAt   DateTime? @map("last_login_at")

  @@map("app_user")
}

model GatewayProfile {
    profileName      String             @id @map("profile_name")
    profile          String
    scenario         String
    apiPort          Int                @map("api_port")
    status           GatewayProfileStatus
    buildStatus      GatewayBuildStatus @default(IDLE) @map("build_status")
    buildCommitSha   String?            @map("build_commit_sha")
    buildWorkspace   String?            @map("build_workspace")
    buildLastUsedAt  DateTime?          @map("build_last_used_at")
    preopenAt        DateTime?          @map("preopen_at")
    openAt           DateTime?          @map("open_at")
    scheduledStartAt DateTime?          @map("scheduled_start_at")
    buildRequestedAt DateTime?          @map("build_requested_at")
    buildStartedAt   DateTime?          @map("build_started_at")
    buildCompletedAt DateTime?          @map("build_completed_at")
    buildError       String?            @map("build_error")
    lastError        String?            @map("last_error")
    meta             Json               @default(dbgenerated("'{}'::jsonb"))
    createdAt        DateTime           @default(now()) @map("created_at")
    updatedAt        DateTime           @updatedAt @map("updated_at")
    operations       GatewayOperation[]

  @@unique([profile, scenario])
  @@map("gateway_profile")
}

model GatewayOperation {
    id                String                 @id @default(uuid())
    profileName       String                 @map("profile_name")
    profile           GatewayProfile         @relation(fields: [profileName], references: [profileName], onDelete: Cascade)
    type              GatewayOperationType
    status            GatewayOperationStatus @default(QUEUED)
    sourceMode        GatewaySourceMode?      @map("source_mode")
    sourceRef         String?                @map("source_ref")
    resolvedCommitSha String?                @map("resolved_commit_sha")
    payload           Json                   @default(dbgenerated("'{}'::jsonb"))
    reason            String?
    requestedBy       String                 @map("requested_by")
    scheduledAt       DateTime?              @map("scheduled_at")
    startedAt         DateTime?              @map("started_at")
    completedAt       DateTime?              @map("completed_at")
    error             String?
    createdAt         DateTime               @default(now()) @map("created_at")
    updatedAt         DateTime               @updatedAt @map("updated_at")

    @@index([status, scheduledAt, createdAt])
    @@index([profileName, createdAt])
    @@map("gateway_operation")
}

model SystemSetting {
  id     Int    @id @default(1) @map("no")
  notice String @default("") @map("notice")

  @@map("system")
}
