generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "debian-openssl-3.0.x"]
}

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

model Project {
  id                String   @id @default(cuid())
  name              String   @unique
  path              String
  source            String   // "auto-scan" | "manual"
  gitRemote         String?
  branch            String?
  version           String?
  lastCommitHash    String?
  lastCommitMessage String?
  lastCommitTime    DateTime?
  notes             String?
  tags              String?
  priority          Int      @default(0)
  deployTargetId    String?
  deployTarget      DeployTarget? @relation("ProjectDeployTarget", fields: [deployTargetId], references: [id])
  envVarsEncrypted  String?
  composeSource     String   @default("repo")
  composeInline     String?
  deployedVersion   String?
  deployedAt        DateTime?
  deploys           DeployHistory[]
  commits           CommitLog[]
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
}

model Setting {
  key   String @id
  value String
}

model CommitLog {
  id         String   @id @default(cuid())
  projectId  String
  project    Project  @relation(fields: [projectId], references: [id], onDelete: Cascade)
  hash       String
  message    String
  author     String?
  branch     String?
  timestamp  DateTime @default(now())
  @@index([projectId, timestamp])
}

model DeployTarget {
  id          String    @id @default(cuid())
  name        String    @unique
  host        String
  sshPort     Int       @default(22)
  sshUser     String    @default("root")
  authMode    String    // "password" | "key"
  authBlob    String    // AES-256-GCM(JSON{password|privateKey})
  composePath String    @default("/opt/{name}/docker-compose.yml")
  notes       String?
  projects    Project[] @relation("ProjectDeployTarget")
  createdAt   DateTime  @default(now())
}

model DeployHistory {
  id          String    @id @default(cuid())
  projectId   String
  project     Project   @relation(fields: [projectId], references: [id], onDelete: Cascade)
  targetId    String?
  imageTag    String
  commitHash  String?
  status      String    // "queued" | "building" | "pushing" | "deploying" | "success" | "failed"
  startedAt   DateTime  @default(now())
  finishedAt  DateTime?
  errorMsg    String?
  logBlob     String?
  @@index([projectId, startedAt])
}
