// ============================================================
// INSIGHT MVP - Tenant Schema (tenant_{slug} Datenbanken)
// ============================================================
// Jeder Mandant hat eine eigene Datenbank mit diesen Tabellen.
// Schema wird per Prisma Migrate auf neue Tenant-DBs angewandt.
//
// HINWEIS: Dieses Schema wird derzeit als Referenz gefuehrt.
// Die tatsaechliche Migration auf Tenant-DBs erfolgt
// in Sprint 2+ wenn das CRM-Modul implementiert wird.
// ============================================================

generator client {
  provider = "prisma-client-js"
  output   = "../node_modules/.prisma/tenant-client"
}

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

// --------------------------------------------------------
// Contact - CRM-Kontakte (Personen & Organisationen)
// --------------------------------------------------------
model Contact {
  id        String      @id @default(uuid()) @db.Uuid
  type      ContactType @default(PERSON)

  // Person
  firstName String? @map("first_name") @db.VarChar(100)
  lastName  String? @map("last_name") @db.VarChar(100)

  // Organisation
  companyName String? @map("company_name") @db.VarChar(200)

  // Kontaktdaten
  email     String?  @db.VarChar(255)
  phone     String?  @db.VarChar(50)
  mobile    String?  @db.VarChar(50)
  website   String?  @db.VarChar(500)

  // Adresse
  street    String?  @db.VarChar(200)
  zip       String?  @db.VarChar(20)
  city      String?  @db.VarChar(100)
  state     String?  @db.VarChar(100)
  country   String?  @default("DE") @db.VarChar(2)

  // Zusaetzlich
  notes     String?  @db.Text
  tags      String[] @default([])

  isActive  Boolean  @default(true) @map("is_active")

  // Wer hat erstellt/bearbeitet (User-IDs aus platform_core)
  createdBy String   @map("created_by") @db.Uuid
  updatedBy String?  @map("updated_by") @db.Uuid

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

  // Relationen
  activities Activity[]

  @@index([email])
  @@index([companyName])
  @@index([lastName, firstName])
  @@map("contacts")
}

enum ContactType {
  PERSON
  ORGANIZATION
}

// --------------------------------------------------------
// Activity - CRM-Aktivitaeten (Notizen, Anrufe, E-Mails)
// --------------------------------------------------------
model Activity {
  id          String       @id @default(uuid()) @db.Uuid
  contactId   String       @map("contact_id") @db.Uuid
  type        ActivityType
  subject     String       @db.VarChar(500)
  description String?      @db.Text

  // Terminierung
  scheduledAt DateTime?    @map("scheduled_at")
  completedAt DateTime?    @map("completed_at")

  // Wer hat erstellt (User-ID aus platform_core)
  createdBy   String       @map("created_by") @db.Uuid

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

  // Relationen
  contact Contact @relation(fields: [contactId], references: [id], onDelete: Cascade)

  @@index([contactId])
  @@index([type])
  @@index([scheduledAt])
  @@map("activities")
}

enum ActivityType {
  NOTE
  CALL
  EMAIL
  MEETING
  TASK
}
