db = DatabaseManager::getInstance(); } public function getAll(array $filters = []): array { $sql = "SELECT * FROM advertising_brands WHERE 1=1"; $params = []; if (!empty($filters['is_active'])) { $sql .= " AND is_active = :is_active"; $params[':is_active'] = $filters['is_active']; } if (!empty($filters['search'])) { $sql .= " AND (name LIKE :search OR description LIKE :search)"; $params[':search'] = '%' . $filters['search'] . '%'; } $sql .= " ORDER BY name ASC"; return $this->db->fetchAll($sql, $params) ?: []; } public function getById(int $id): ?array { $sql = "SELECT * FROM advertising_brands WHERE id = :id"; return $this->db->fetch($sql, [':id' => $id]); } public function getBySlug(string $slug): ?array { $sql = "SELECT * FROM advertising_brands WHERE slug = :slug"; return $this->db->fetch($sql, [':slug' => $slug]); } public function create(array $data): int { $cuid = $this->generateCuid(); $slug = $this->generateSlug($data['name']); $sql = "INSERT INTO advertising_brands ( cuid, name, slug, website, description, logo_url, settings, is_active ) VALUES ( :cuid, :name, :slug, :website, :description, :logo_url, :settings, :is_active )"; $params = [ ':cuid' => $cuid, ':name' => $data['name'], ':slug' => $slug, ':website' => $data['website'] ?? null, ':description' => $data['description'] ?? null, ':logo_url' => $data['logo_url'] ?? null, ':settings' => !empty($data['settings']) ? json_encode($data['settings']) : null, ':is_active' => $data['is_active'] ?? 1 ]; $this->db->query($sql, $params); return $this->db->lastInsertId(); } public function update(int $id, array $data): bool { $sql = "UPDATE advertising_brands SET name = :name, website = :website, description = :description, logo_url = :logo_url, settings = :settings, is_active = :is_active, updated_at = CURRENT_TIMESTAMP WHERE id = :id"; return $this->db->query($sql, [ ':id' => $id, ':name' => $data['name'], ':website' => $data['website'] ?? null, ':description' => $data['description'] ?? null, ':logo_url' => $data['logo_url'] ?? null, ':settings' => !empty($data['settings']) ? json_encode($data['settings']) : null, ':is_active' => $data['is_active'] ?? 1 ]) > 0; } public function destroy(int $id): bool { $sql = "UPDATE advertising_brands SET is_active = 0 WHERE id = :id"; return $this->db->query($sql, [':id' => $id]) > 0; } public function getActiveBrands(): array { $sql = "SELECT id, name, slug FROM advertising_brands WHERE is_active = 1 ORDER BY name ASC"; return $this->db->fetchAll($sql) ?: []; } public function getLandingTypes(int $brandId): array { $sql = "SELECT * FROM advertising_brand_landing_types WHERE brand_id = :brand_id AND is_active = 1 ORDER BY name ASC"; return $this->db->fetchAll($sql, [':brand_id' => $brandId]) ?: []; } private function generateCuid(): string { return (string) new \Visus\Cuid2\Cuid2(); } private function generateSlug(string $name): string { $slug = strtolower(trim($name)); $slug = preg_replace('/[^a-z0-9-]/', '-', $slug); $slug = preg_replace('/-+/', '-', $slug); $slug = trim($slug, '-'); // Garantir unicidade $baseSlug = $slug; $counter = 1; while ($this->getBySlug($slug)) { $slug = $baseSlug . '-' . $counter; $counter++; } return $slug; } public function getDb(): DatabaseManager { return $this->db; } }