-- Base de données SQDC
CREATE TABLE IF NOT EXISTS categories (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  emoji TEXT,
  description TEXT
);

CREATE TABLE IF NOT EXISTS kpis (
  id INTEGER PRIMARY KEY,
  category_id INTEGER NOT NULL,
  name TEXT NOT NULL,
  unit TEXT,
  target REAL,
  formula TEXT,
  description TEXT,
  frequency TEXT, -- 'daily', 'per_10min', 'per_3days', etc.
  FOREIGN KEY(category_id) REFERENCES categories(id)
);

CREATE TABLE IF NOT EXISTS measurements (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  kpi_id INTEGER NOT NULL,
  measurement_date DATETIME NOT NULL,
  value REAL NOT NULL,
  status TEXT, -- 'good', 'warning', 'critical'
  FOREIGN KEY(kpi_id) REFERENCES kpis(id)
);

CREATE TABLE IF NOT EXISTS alerts (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  kpi_id INTEGER NOT NULL,
  alert_type TEXT,
  severity TEXT, -- 'warning', 'critical'
  message TEXT,
  created_at DATETIME,
  FOREIGN KEY(kpi_id) REFERENCES kpis(id)
);

CREATE TABLE IF NOT EXISTS comments (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  kpi_id INTEGER NOT NULL,
  comment TEXT,
  created_at DATETIME,
  FOREIGN KEY(kpi_id) REFERENCES kpis(id)
);

-- Créer les indices pour les performances
CREATE INDEX IF NOT EXISTS idx_measurements_kpi ON measurements(kpi_id);
CREATE INDEX IF NOT EXISTS idx_measurements_date ON measurements(measurement_date);
CREATE INDEX IF NOT EXISTS idx_alerts_kpi ON alerts(kpi_id);
CREATE INDEX IF NOT EXISTS idx_comments_kpi ON comments(kpi_id);
