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

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

model Apartment {
  id        String   @id @default(cuid())
  name      String
  address   String?
  totalArea Float?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  rooms     Room[]
}

model Room {
  id              String          @id @default(cuid())
  apartmentId     String
  name            String
  shape           String          @default("[]") // JSON array of {x, y} points
  width           Float?
  height          Float?
  wallHeight      Float           @default(2.7)
  plinthHeight    Float           @default(0.06)
  plinthThickness Float           @default(0.01)
  /// Distance (meters) the stretch ceiling hangs *below* the structural
  /// ceiling. 0 = no stretch ceiling. The ceiling plane is therefore at
  /// `wallHeight - stretchCeilingOffset`. Display-only: does not affect
  /// wall geometry, opening positions, or electrical elevations.
  stretchCeilingOffset Float      @default(0)
  order           Int             @default(0)
  posX            Float           @default(0)
  posY            Float           @default(0)
  floorType       String          @default("CONCRETE")
  wallColor       String          @default("#f5f0eb")
  wallFinish      String          @default("PAINT")
  outletWidth     Float           @default(0.07)
  outletHeight    Float           @default(0.07)
  createdAt       DateTime        @default(now())
  updatedAt       DateTime        @updatedAt
  apartment       Apartment       @relation(fields: [apartmentId], references: [id], onDelete: Cascade)
  walls           Wall[]
  openings        WallOpening[]
  electricalItems ElectricalItem[]
  furnitureItems  FurnitureItem[]
  annotations     Annotation[]

  @@index([apartmentId])
}

model Wall {
  id        String        @id @default(cuid())
  roomId    String
  startX    Float
  startY    Float
  endX      Float
  endY      Float
  thickness Float         @default(0.1)
  direction String        @default("OTHER") // NORTH, SOUTH, EAST, WEST, OTHER
  room      Room          @relation(fields: [roomId], references: [id], onDelete: Cascade)
  openings  WallOpening[]

  @@index([roomId])
}

model WallOpening {
  id                 String @id @default(cuid())
  roomId             String
  wallId             String
  type               String // DOOR, WINDOW
  positionAlongWall  Float
  width              Float
  height             Float
  elevationFromFloor Float  @default(0)
  openDirection      String @default("LEFT") // LEFT, RIGHT, INWARD, OUTWARD
  anchorH            String @default("middle") // left, middle, right (positionAlongWall = center by default)
  anchorV            String @default("bottom") // top, middle, bottom (elevationFromFloor = bottom edge by default)
  gridCols           Int    @default(2) // window pane subdivision — columns
  gridRows           Int    @default(2) // window pane subdivision — rows
  slopeDepth         Float  @default(0) // window reveal (откос) depth in meters; 0 = no slope shown
  frameThickness     Float  @default(0.03) // door/window frame member thickness in meters
  room               Room   @relation(fields: [roomId], references: [id], onDelete: Cascade)
  wall               Wall   @relation(fields: [wallId], references: [id], onDelete: Cascade)

  @@index([roomId])
  @@index([wallId])
}

model ElectricalItem {
  id                 String  @id @default(cuid())
  roomId             String
  type               String  // OUTLET, SWITCH, JUNCTION_BOX, LIGHT_CEILING, LIGHT_WALL, CABLE_ROUTE
  x                  Float
  y                  Float
  wallId             String?
  elevationFromFloor Float?
  rotation           Float   @default(0)
  count              Int     @default(1)
  anchorH            String  @default("middle") // left, middle, right
  anchorV            String  @default("middle") // top, middle, bottom
  label              String? // user-supplied display name; falls back to symbol def label when null
  metadata           String? // JSON
  room               Room    @relation(fields: [roomId], references: [id], onDelete: Cascade)

  @@index([roomId])
}

model FurnitureItem {
  id       String  @id @default(cuid())
  roomId   String
  type     String  // BED, DESK, WARDROBE, SOFA, TABLE, CHAIR, SHELF, NIGHTSTAND, DRESSER, BOOKCASE, OTHER
  x        Float
  y        Float
  width    Float
  depth    Float
  height   Float
  rotation           Float   @default(0)
  elevationFromFloor Float   @default(0)
  label              String?
  anchorH            String  @default("middle") // left, middle, right
  anchorV            String  @default("middle") // top, middle, bottom
  showProjection     Boolean @default(false)
  opacity            Float   @default(1)
  metadata           String? // JSON — type-specific extension bag (e.g. curtain open amount + fabric color)
  room               Room    @relation(fields: [roomId], references: [id], onDelete: Cascade)

  @@index([roomId])
}

model Annotation {
  id              String  @id @default(cuid())
  roomId          String
  x               Float   @default(0)
  y               Float   @default(0)
  text            String
  fontSize        Int?
  color           String?
  attachedToId    String?
  projectionOffsetX Float?
  projectionOffsetY Float?
  room            Room    @relation(fields: [roomId], references: [id], onDelete: Cascade)

  @@index([roomId])
}
