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,
    points          INTEGER NOT NULL,
    incident_date   DATE NOT NULL,
    incident_time   TEXT,
    location        TEXT,
    details         TEXT,
    submitted_by    TEXT,
    witness_name    TEXT,
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE VIEW IF NOT EXISTS active_cpas_scores AS
SELECT
    e.id                        AS employee_id,
    e.name                      AS employee_name,
    e.department,
    COALESCE(SUM(v.points), 0)  AS active_points,
    COUNT(v.id)                 AS violation_count
FROM employees e
LEFT JOIN violations v
    ON v.employee_id = e.id
    AND v.incident_date >= DATE('now', '-90 days')
GROUP BY e.id;
