// 优化后的架构 - 数据中台 v4.0 (完全通用化)

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "debian-openssl-1.1.x"]
  output        = "../generated/prisma"
}

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

// ============= 数据源层 =============
model DataSource {
  id          String     @id @default(cuid())
  name        String
  code        String     @unique
  type        SourceType
  config      Json // 连接配置
  description String?
  status      Status     @default(ACTIVE)

  // 元数据版本管理
  schemaVersion String?   @default("1.0")
  lastSynced    DateTime?

  // 关联
  pipelines Pipeline[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@map("data_sources")
}

// ============= 统一流水线层 =============
model Pipeline {
  id          String       @id @default(cuid())
  name        String
  type        PipelineType // SYNC | TRANSFORM | STREAM | HYBRID
  schedule    String? // Cron表达式
  config      Json // 流水线配置
  description String?
  status      Status       @default(ACTIVE)

  // 关联数据源（可选）
  dataSource   DataSource? @relation(fields: [dataSourceId], references: [id])
  dataSourceId String?

  // 执行记录
  executions PipelineExecution[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@map("pipelines")
}

// 统一执行记录
model PipelineExecution {
  id          String          @id @default(cuid())
  executionId String          @unique
  status      ExecutionStatus @default(PENDING)

  // 统计信息
  inputRecords  Int @default(0)
  outputRecords Int @default(0)
  errorRecords  Int @default(0)

  // 时间信息
  startedAt   DateTime  @default(now())
  completedAt DateTime?
  duration    Int? // 毫秒

  // 元数据
  metadata Json? // 灵活的元数据存储
  errorMsg String?

  // 关联
  pipeline   Pipeline @relation(fields: [pipelineId], references: [id])
  pipelineId String

  // 产生的数据资产
  dataAssets DataAsset[]

  // 数据血缘
  lineageRecords LineageRecord[]

  @@map("pipeline_executions")
}

// ============= 数据资产层 =============
model DataAsset {
  id      String     @id @default(cuid())
  assetId String     @unique
  name    String
  type    AssetType  @default(BATCH)
  format  DataFormat @default(PARQUET)

  // 存储信息（支持多种存储）
  storageConfig Json // 统一存储配置

  // 元数据管理
  schema      Json?
  partitions  Json? // 分区信息
  size        BigInt? // 数据大小
  recordCount BigInt? // 记录数

  // 数据质量
  qualityScore Float? // 0-1之间的质量分数

  // 关联
  execution   PipelineExecution? @relation(fields: [executionId], references: [id])
  executionId String?

  // 查询记录
  queries Query[]

  status    AssetStatus @default(ACTIVE)
  createdAt DateTime    @default(now())
  updatedAt DateTime    @updatedAt

  @@index([type, status])
  @@index([createdAt])
  @@map("data_assets")
}

// ============= 查询层 =============
model Query {
  id      String      @id @default(cuid())
  queryId String      @unique
  sql     String
  engine  QueryEngine @default(DUCKDB)

  // 执行信息
  status      QueryStatus @default(PENDING)
  resultCount BigInt?
  resultPath  String?
  duration    Int? // 毫秒
  errorMsg    String?

  // 查询标签
  tags Json?

  // 关联资产
  dataAsset DataAsset @relation(fields: [assetId], references: [id])
  assetId   String

  // 审计信息
  userId String? // 执行用户

  createdAt   DateTime  @default(now())
  completedAt DateTime?

  @@index([status, createdAt])
  @@map("queries")
}

// ============= 通用实体层 =============

// 通用实体模型（支持优雅的树形层级）
model Entity {
  id         String     @id @default(cuid())
  code       String     @unique
  name       String
  type       EntityType // PERSON, EQUIPMENT, FACILITY, MATERIAL, ORGANIZATION等
  attributes Json? // 灵活的属性存储
  status     Status     @default(ACTIVE)

  // 树形层级关系（直接支持）
  parentId String?
  parent   Entity?  @relation("EntityTree", fields: [parentId], references: [id])
  children Entity[] @relation("EntityTree")

  // 层级路径（用于快速查询）
  path  String? // 如: "/org1/dept1/team1" 便于层级查询
  level Int? // 层级深度，根节点为0

  // 作为源实体的关系（非树形关系）
  sourceRelations EntityRelation[] @relation("SourceEntity")
  // 作为目标实体的关系  
  targetRelations EntityRelation[] @relation("TargetEntity")

  // 数据血缘
  lineageRecords LineageRecord[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([type, status])
  @@index([code])
  @@index([type, code])
  @@index([parentId])
  @@index([path])
  @@index([type, parentId])
  @@index([level])
  @@map("entities")
}

// ============= 通用关系层 =============

// 通用实体关系表（处理非树形的复杂关系）
model EntityRelation {
  id           String @id @default(cuid())
  sourceId     String // 源实体ID
  targetId     String // 目标实体ID
  relationship String // 关系类型

  // 关系属性
  attributes Json? // 关系属性：权限级别、时间范围等
  startDate  DateTime?
  endDate    DateTime?

  // 关系元数据
  metadata Json? // 其他元数据

  // 关联实体
  sourceEntity Entity @relation("SourceEntity", fields: [sourceId], references: [id])
  targetEntity Entity @relation("TargetEntity", fields: [targetId], references: [id])

  status    Status   @default(ACTIVE)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([sourceId, targetId, relationship])
  @@index([sourceId])
  @@index([targetId])
  @@index([relationship])
  @@index([sourceId, relationship])
  @@index([targetId, relationship])
  @@index([relationship, status])
  @@map("entity_relations")
}

// ============= 优化的血缘层 =============
model LineageRecord {
  id           String @id @default(cuid())
  sourceType   String // 来源类型
  sourceId     String // 来源ID
  targetType   String // 目标类型  
  targetId     String // 目标ID
  relationship String // 关系类型: CREATE, UPDATE, DERIVE

  // 元数据
  metadata Json? // 血缘元数据

  // 关联执行（可选）
  execution   PipelineExecution? @relation(fields: [executionId], references: [id])
  executionId String?

  // 关联实体（可选）
  entity   Entity? @relation(fields: [entityId], references: [id])
  entityId String?

  status    LineageStatus @default(ACTIVE)
  createdAt DateTime      @default(now())

  @@unique([sourceType, sourceId, targetType, targetId])
  @@index([relationship])
  @@map("lineage_records")
}

// ============= 数据治理层 =============
model QualityRule {
  id          String  @id @default(cuid())
  name        String
  description String?
  rule        Json // 质量规则定义
  threshold   Float? // 阈值

  // 应用范围
  entityType String? // 应用的实体类型
  entityId   String? // 特定实体ID

  status Status @default(ACTIVE)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@map("quality_rules")
}

model DataCatalog {
  id          String  @id @default(cuid())
  name        String
  description String?
  tags        Json? // 标签
  owner       String? // 数据负责人

  // 分类
  category    String?
  sensitivity String? // 敏感度级别

  // 关联资产或实体
  assetType String // 资产类型：DataAsset, Entity等
  assetId   String // 资产ID

  status    Status   @default(ACTIVE)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([assetType, assetId])
  @@index([category])
  @@map("data_catalogs")
}

// ============= 精简的权限层 =============
model User {
  id       String @id @default(cuid())
  username String @unique
  email    String @unique
  name     String
  roles    Json? // 简化为JSON存储角色
  status   Status @default(ACTIVE)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@map("users")
}

// ============= 枚举定义 =============
enum Status {
  ACTIVE
  INACTIVE
}

enum SourceType {
  DATABASE
  API
  FILE
  ERP
  MES
  IOT
  STREAM
}

enum PipelineType {
  SYNC
  TRANSFORM
  STREAM
  HYBRID
}

enum ExecutionStatus {
  PENDING
  RUNNING
  SUCCESS
  FAILED
  CANCELLED
}

enum DataFormat {
  PARQUET
  JSON
  CSV
  AVRO
  DELTA
}

enum AssetType {
  BATCH
  STREAM
  TABLE
  VIEW
  MODEL
}

enum AssetStatus {
  ACTIVE
  ARCHIVED
  DEPRECATED
}

enum QueryEngine {
  DUCKDB
  ATHENA
  SPARK
  TRINO
}

enum QueryStatus {
  PENDING
  RUNNING
  SUCCESS
  FAILED
  CANCELLED
}

enum EntityType {
  PERSON
  EQUIPMENT
  FACILITY
  MATERIAL
  ORGANIZATION
  PROJECT
  CUSTOM
}

enum LineageStatus {
  ACTIVE
  ARCHIVED
}
model Resource {
    id          String    @id @default(cuid()) @map("id")
    title       String   @map("title")
    description String?   @map("description")
    type        String?   @map("type")
    fileId      String   @unique
    url         String?
    meta        Json?     @map("meta")
    status      String?
    createdAt   DateTime @default(now()) @map("created_at")
    updatedAt   DateTime @updatedAt @map("updated_at")
    createdBy   String?   @map("created_by")
    updatedBy   String?   @map("updated_by")
    deletedAt   DateTime? @map("deleted_at")
    isPublic Boolean? @default(true) @map("is_public")
    storageType String? @map("storage_type")

    // 索引
    @@index([type])
    @@index([createdAt])
    @@map("resource")
}