CREATE TABLE IF NOT EXISTS cached_reports (
  id            INT AUTO_INCREMENT PRIMARY KEY,
  company_id    INT NOT NULL,
  report_type   VARCHAR(50) NOT NULL,
  period_start  DATE NOT NULL,
  period_end    DATE NOT NULL,
  summarize_by  VARCHAR(20) NOT NULL DEFAULT 'Total',
  report_json   JSON NOT NULL,
  fetched_at    TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY unique_report (company_id, report_type, period_start, period_end, summarize_by),
  FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
