PRAGMA foreign_keys = ON;

-- Users table --------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  username TEXT NOT NULL UNIQUE,
  password_hash TEXT NOT NULL,
  full_name TEXT NOT NULL DEFAULT '',
  role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('admin', 'user')),
  active INTEGER NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TRIGGER IF NOT EXISTS trg_users_updated
AFTER UPDATE ON users
FOR EACH ROW
BEGIN
  UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id;
END;

-- Production records -------------------------------------------------------
CREATE TABLE IF NOT EXISTS production_records (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  month INTEGER NOT NULL CHECK (month BETWEEN 1 AND 12),
  year INTEGER NOT NULL,
  client_name TEXT NOT NULL,
  type TEXT,
  offer_number TEXT,
  work_number TEXT,
  quantity INTEGER NOT NULL DEFAULT 0,
  color TEXT,
  notes TEXT,
  notes_date TEXT,
  installer TEXT,
  price REAL NOT NULL DEFAULT 0,
  arve_checked INTEGER NOT NULL DEFAULT 0,
  arve_makstud TEXT,
  price_paid INTEGER NOT NULL DEFAULT 0,
  deleted INTEGER NOT NULL DEFAULT 0,
  created_by INTEGER,
  updated_by INTEGER,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL,
  FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
);

CREATE INDEX IF NOT EXISTS idx_production_records_year_month
  ON production_records (year, month, deleted);

CREATE TRIGGER IF NOT EXISTS trg_production_records_updated
AFTER UPDATE ON production_records
FOR EACH ROW
BEGIN
  UPDATE production_records SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id;
END;

-- Status checkboxes --------------------------------------------------------
CREATE TABLE IF NOT EXISTS status_checkboxes (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  record_id INTEGER NOT NULL UNIQUE,
  material_date TEXT,
  material_confirmed INTEGER NOT NULL DEFAULT 0,
  material2_date TEXT,
  material2_confirmed INTEGER NOT NULL DEFAULT 0,
  package_date TEXT,
  worksheets_date TEXT,
  worksheets_confirmed INTEGER NOT NULL DEFAULT 0,
  worksheets_error INTEGER NOT NULL DEFAULT 0,
  worksheets_cycle_step INTEGER NOT NULL DEFAULT 0,
  cutting_date TEXT,
  cutting_error INTEGER NOT NULL DEFAULT 0,
  glazing_date TEXT,
  glazing_error INTEGER NOT NULL DEFAULT 0,
  ready_date TEXT,
  ready_error INTEGER NOT NULL DEFAULT 0,
  issued_date TEXT,
  issued_error INTEGER NOT NULL DEFAULT 0,
  problems TEXT,
  problems_date TEXT,
  problem_flag INTEGER NOT NULL DEFAULT 0,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (record_id) REFERENCES production_records(id) ON DELETE CASCADE
);

CREATE TRIGGER IF NOT EXISTS trg_status_checkboxes_updated
AFTER UPDATE ON status_checkboxes
FOR EACH ROW
BEGIN
  UPDATE status_checkboxes SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id;
END;

-- Audit log ----------------------------------------------------------------
CREATE TABLE IF NOT EXISTS audit_log (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  record_id INTEGER,
  user_id INTEGER,
  action TEXT NOT NULL,
  field TEXT,
  old_value TEXT,
  new_value TEXT,
  metadata TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (record_id) REFERENCES production_records(id) ON DELETE SET NULL,
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);

CREATE INDEX IF NOT EXISTS idx_audit_log_record_id
  ON audit_log (record_id, created_at DESC);
