db = $db; } /** * Initialize database tables */ public function initialize(): void { $this->createReportsTable(); } /** * Create reports table */ private function createReportsTable(): void { $sql = " CREATE TABLE IF NOT EXISTS *PREFIX*analytics_reports ( id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER NOT NULL, client_name TEXT NOT NULL, report_date TEXT NOT NULL, file_path TEXT NOT NULL, file_size INTEGER, created_at TEXT NOT NULL ) "; $this->db->executeUpdate($sql); } /** * Save report to database */ public function saveReport(int $clientId, string $clientName, string $markdown, string $filePath): int { $sql = " INSERT INTO *PREFIX*analytics_reports (client_id, client_name, report_date, file_path, file_size, created_at) VALUES (?, ?, ?, ?, ?, ?) "; $this->db->executeUpdate($sql, [ $clientId, $clientName, date('Y-m-d'), $filePath, strlen($markdown), date('Y-m-d H:i:s') ]); return (int)$this->db->lastInsertId(); } /** * Get report by ID */ public function getReportById(int $id): ?array { $sql = " SELECT * FROM *PREFIX*analytics_reports WHERE id = ? "; $result = $this->db->executeQuery($sql, [$id])->fetch(); return $result ? json_decode(json_encode($result), true) : null; } /** * Get all reports */ public function getAllReports(): array { $sql = " SELECT * FROM *PREFIX*analytics_reports ORDER BY created_at DESC LIMIT 100 "; $result = $this->db->executeQuery($sql)->fetchAll(); return $result; } /** * Get reports by client */ public function getReportsByClient(int $clientId): array { $sql = " SELECT * FROM *PREFIX*analytics_reports WHERE client_id = ? ORDER BY created_at DESC "; $result = $this->db->executeQuery($sql, [$clientId])->fetchAll(); return $result; } /** * Get latest report */ public function getLatestReport(): ?array { $sql = " SELECT * FROM *PREFIX*analytics_reports ORDER BY created_at DESC LIMIT 1 "; $result = $this->db->executeQuery($sql)->fetch(); return $result ? json_decode(json_encode($result), true) : null; } /** * Delete old reports (cleanup) */ public function deleteOldReports(int $days = 90): int { $sql = " DELETE FROM *PREFIX*analytics_reports WHERE created_at < datetime('now', '-' . $days . ' days') "; return $this->db->executeUpdate($sql); } }