CREATE TABLE IF NOT EXISTS employees (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    name        TEXT    NOT NULL,
    department  TEXT,
    supervisor  TEXT,
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS violations (
    id                   INTEGER PRIMARY KEY AUTOINCREMENT,
    employee_id          INTEGER NOT NULL REFERENCES employees(id),
    violation_type       TEXT    NOT NULL,
    violation_name       TEXT    NOT NULL,
    category             TEXT    NOT NULL DEFAULT 'General',
    points               INTEGER NOT NULL,
    incident_date        TEXT    NOT NULL,
    incident_time        TEXT,
    location             TEXT,
    details              TEXT,
    submitted_by         TEXT,
    witness_name         TEXT,
    negated              INTEGER NOT NULL DEFAULT 0,
    negated_at           DATETIME,
    prior_active_points  INTEGER, -- snapshot at time of logging
    prior_tier_label     TEXT,    -- optional human-readable tier
    acknowledged_by      TEXT,    -- employee name who acknowledged receipt
    acknowledged_date    TEXT,    -- date of acknowledgment (YYYY-MM-DD)
    amount               TEXT,    -- dollar amount in question for financial violations (record-keeping / repayment)
    created_at           DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS violation_resolutions (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    violation_id    INTEGER NOT NULL REFERENCES violations(id) ON DELETE CASCADE,
    resolution_type TEXT    NOT NULL,
    details         TEXT,
    resolved_by     TEXT,
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- CPAS standing (active points + roll-off schedule) is computed in JS from the
-- clean-cycle model in lib/rolloff.js; see db/database.js for why the old
-- active_cpas_scores view was retired.
