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

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

model Company {
  id             String    @id @default(uuid()) @db.Uuid
  cnpj           String    @unique
  corporateName  String    @map("corporate_name")
  category       String    @default("farmacia")
  licenseNumber  String    @map("license_number")
  isVerified     Boolean   @default(false) @map("is_verified")
  status         String    @default("PENDING")
  latitude       Float     @default(0)
  longitude      Float     @default(0)
  city           String    @default("")
  state          String    @default("")
  createdAt      DateTime  @default(now()) @map("created_at") @db.Timestamptz
  updatedAt      DateTime  @updatedAt @map("updated_at") @db.Timestamptz

  users          User[]
  products       Product[]
  ordersAsBuyer  Order[]   @relation("BuyerOrders")
  ordersAsSeller Order[]   @relation("SellerOrders")
  documents      CompanyDocument[]

  @@map("companies")
}

model User {
  id           String    @id @default(uuid()) @db.Uuid
  companyId    String    @map("company_id") @db.Uuid
  company      Company   @relation(fields: [companyId], references: [id])
  role         String
  name         String
  username     String?
  email        String    @unique
  passwordHash String    @map("password_hash")
  refreshToken String?   @map("refresh_token")
  createdAt    DateTime  @default(now()) @map("created_at") @db.Timestamptz
  updatedAt    DateTime  @updatedAt @map("updated_at") @db.Timestamptz

  @@map("users")
}

model Product {
  id           String    @id @default(uuid()) @db.Uuid
  sellerId     String    @map("seller_id") @db.Uuid
  seller       Company   @relation(fields: [sellerId], references: [id])
  name         String
  description  String?
  batch        String
  expiresAt    DateTime  @map("expires_at") @db.Date
  priceCents   BigInt    @map("price_cents")
  stock        BigInt
  createdAt    DateTime  @default(now()) @map("created_at") @db.Timestamptz
  updatedAt    DateTime  @updatedAt @map("updated_at") @db.Timestamptz

  orderItems   OrderItem[]
  cartItems    CartItem[]
  inventoryAdjustments InventoryAdjustment[]

  @@map("products")
}

model InventoryAdjustment {
  id        String   @id @default(uuid()) @db.Uuid
  productId String   @map("product_id") @db.Uuid
  product   Product  @relation(fields: [productId], references: [id])
  delta     BigInt
  reason    String?
  createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz

  @@map("inventory_adjustments")
}

model Order {
  id                    String    @id @default(uuid()) @db.Uuid
  buyerId               String    @map("buyer_id") @db.Uuid
  sellerId              String    @map("seller_id") @db.Uuid
  buyer                 Company   @relation("BuyerOrders", fields: [buyerId], references: [id])
  seller                Company   @relation("SellerOrders", fields: [sellerId], references: [id])
  status                String
  totalCents            BigInt    @map("total_cents")
  paymentMethod         String    @default("pix") @map("payment_method")
  
  shippingRecipientName String?   @map("shipping_recipient_name")
  shippingStreet        String?   @map("shipping_street")
  shippingNumber        String?   @map("shipping_number")
  shippingComplement    String?   @map("shipping_complement")
  shippingDistrict      String?   @map("shipping_district")
  shippingCity          String?   @map("shipping_city")
  shippingState         String?   @map("shipping_state")
  shippingZipCode       String?   @map("shipping_zip_code")
  shippingCountry       String?   @map("shipping_country")

  createdAt             DateTime  @default(now()) @map("created_at") @db.Timestamptz
  updatedAt             DateTime  @updatedAt @map("updated_at") @db.Timestamptz

  items                 OrderItem[]
  shipment              Shipment?
  review                Review?

  @@map("orders")
}

model OrderItem {
  id        String   @id @default(uuid()) @db.Uuid
  orderId   String   @map("order_id") @db.Uuid
  order     Order    @relation(fields: [orderId], references: [id], onDelete: Cascade)
  productId String   @map("product_id") @db.Uuid
  product   Product  @relation(fields: [productId], references: [id])
  quantity  BigInt
  unitCents BigInt   @map("unit_cents")
  batch     String
  expiresAt DateTime @map("expires_at") @db.Date

  @@map("order_items")
}

model CartItem {
  id        String   @id @default(uuid()) @db.Uuid
  buyerId   String   @map("buyer_id") @db.Uuid
  product   Product  @relation(fields: [productId], references: [id], onDelete: Cascade)
  productId String   @map("product_id") @db.Uuid
  quantity  BigInt
  unitCents BigInt   @map("unit_cents")
  batch     String?
  expiresAt DateTime? @map("expires_at") @db.Date
  createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
  updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz

  @@unique([buyerId, productId])
  @@map("cart_items")
}

model Review {
  id        String   @id @default(uuid()) @db.Uuid
  orderId   String   @unique @map("order_id") @db.Uuid
  order     Order    @relation(fields: [orderId], references: [id])
  buyerId   String   @map("buyer_id") @db.Uuid
  sellerId  String   @map("seller_id") @db.Uuid
  rating    Int
  comment   String?
  createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz

  @@index([sellerId])
  @@map("reviews")
}

model Shipment {
  id               String   @id @default(uuid()) @db.Uuid
  orderId          String   @unique @map("order_id") @db.Uuid
  order            Order    @relation(fields: [orderId], references: [id])
  carrier          String
  trackingCode     String?  @map("tracking_code")
  externalTracking String?  @map("external_tracking")
  status           String
  createdAt        DateTime @default(now()) @map("created_at") @db.Timestamptz
  updatedAt        DateTime @updatedAt @map("updated_at") @db.Timestamptz

  @@map("shipments")
}

model SystemSettings {
  id        String   @id @default(uuid()) @db.Uuid
  key       String   @unique
  value     String
  category  String   @default("GENERAL")
  isSecure  Boolean  @default(false) @map("is_secure")
  createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
  updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz

  @@map("system_settings")
}

model CompanyDocument {
  id              String   @id @default(uuid()) @db.Uuid
  companyId       String   @map("company_id") @db.Uuid
  company         Company  @relation(fields: [companyId], references: [id])
  type            String
  fileUrl         String   @map("file_url")
  status          String   @default("PENDING")
  rejectionReason String?  @map("rejection_reason")
  createdAt       DateTime @default(now()) @map("created_at") @db.Timestamptz
  updatedAt       DateTime @updatedAt @map("updated_at") @db.Timestamptz

  @@map("company_documents")
}
