-- SQLite-Schema (CRM) — nur Maschinen, Tickets, Events (keine Kunden-Tabelle)
PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS "machines" (
  "id" TEXT NOT NULL PRIMARY KEY,
  "name" TEXT NOT NULL,
  "typ" TEXT NOT NULL,
  "seriennummer" TEXT NOT NULL,
  "standort" TEXT NOT NULL,
  "list_status" TEXT NOT NULL DEFAULT '' CHECK (
    "list_status" IN ('', 'PRUEFEN', 'VERSCHROTTET', 'SN_GEAENDERT', 'IN_BEARBEITUNG', 'UPDATE_RAUS')
  ),
  "extras" TEXT,
  "created_at" TEXT NOT NULL DEFAULT (datetime('now')),
  "updated_at" TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS "users" (
  "id" TEXT NOT NULL PRIMARY KEY,
  "username" TEXT NOT NULL UNIQUE,
  "password_hash" TEXT,
  "role" TEXT NOT NULL DEFAULT 'after_sales' CHECK ("role" IN ('admin', 'viewer', 'after_sales')),
  "source" TEXT NOT NULL DEFAULT 'local' CHECK ("source" IN ('local', 'ldap')),
  "ldap_dn" TEXT,
  "firstname" TEXT,
  "lastname" TEXT,
  "active" INTEGER NOT NULL DEFAULT 1 CHECK ("active" IN (0, 1)),
  "created_at" TEXT NOT NULL DEFAULT (datetime('now')),
  "updated_at" TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS "users_username_idx" ON "users" ("username");

CREATE TABLE IF NOT EXISTS "tickets" (
  "id" TEXT NOT NULL PRIMARY KEY,
  "machine_id" TEXT NOT NULL,
  "title" TEXT NOT NULL,
  "description" TEXT NOT NULL,
  "status" TEXT NOT NULL DEFAULT 'OPEN' CHECK ("status" IN ('OPEN', 'WAITING', 'DONE')),
  "priority" TEXT NOT NULL DEFAULT 'MEDIUM' CHECK ("priority" IN ('LOW', 'MEDIUM', 'HIGH')),
  "sla_days" INTEGER,
  "sla_anchor_at" TEXT,
  "assigned_user_id" TEXT,
  "created_at" TEXT NOT NULL DEFAULT (datetime('now')),
  "updated_at" TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY ("machine_id") REFERENCES "machines" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
  FOREIGN KEY ("assigned_user_id") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS "events" (
  "id" TEXT NOT NULL PRIMARY KEY,
  "ticket_id" TEXT NOT NULL,
  "type" TEXT NOT NULL CHECK ("type" IN ('NOTE', 'CALL', 'REMOTE', 'PART', 'SYSTEM', 'ATTACHMENT')),
  "description" TEXT NOT NULL,
  "callback_number" TEXT,
  "teamviewer_id" TEXT,
  "article_number" TEXT,
  "remote_duration_seconds" INTEGER,
  "teamviewer_notes" TEXT,
  "created_at" TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY ("ticket_id") REFERENCES "tickets" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE INDEX IF NOT EXISTS "tickets_machine_id_idx" ON "tickets" ("machine_id");
CREATE INDEX IF NOT EXISTS "tickets_status_idx" ON "tickets" ("status");
CREATE INDEX IF NOT EXISTS "tickets_priority_idx" ON "tickets" ("priority");
CREATE INDEX IF NOT EXISTS "tickets_assigned_user_id_idx" ON "tickets" ("assigned_user_id");
CREATE INDEX IF NOT EXISTS "events_ticket_id_idx" ON "events" ("ticket_id");
CREATE INDEX IF NOT EXISTS "events_created_at_idx" ON "events" ("created_at");

CREATE TABLE IF NOT EXISTS "ticket_attachments" (
  "id" TEXT NOT NULL PRIMARY KEY,
  "event_id" TEXT NOT NULL,
  "original_name" TEXT NOT NULL,
  "stored_path" TEXT NOT NULL,
  "mime_type" TEXT,
  "size_bytes" INTEGER NOT NULL,
  "created_at" TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY ("event_id") REFERENCES "events" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE INDEX IF NOT EXISTS "ticket_attachments_event_idx" ON "ticket_attachments" ("event_id");

CREATE TABLE IF NOT EXISTS "app_settings" (
  "key" TEXT NOT NULL PRIMARY KEY,
  "value" TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS "ldap_sync_log" (
  "id" TEXT NOT NULL PRIMARY KEY,
  "started_at" TEXT NOT NULL,
  "finished_at" TEXT NOT NULL,
  "trigger_type" TEXT NOT NULL CHECK ("trigger_type" IN ('manual', 'automatic')),
  "status" TEXT NOT NULL CHECK ("status" IN ('success', 'error')),
  "users_synced" INTEGER NOT NULL DEFAULT 0,
  "error_message" TEXT
);

CREATE INDEX IF NOT EXISTS "ldap_sync_log_finished_idx" ON "ldap_sync_log" ("finished_at" DESC);
