getActiveBrands(); echo json_encode([ 'success' => true, 'data' => $brands ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_get_brands']); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function getAllLandingTypes(): void { try { header('Content-Type: application/json'); $landingTypeModel = new \App\Modules\Advertising\Models\LandingTypeModel(); $landingTypes = $landingTypeModel->getAll(); echo json_encode([ 'success' => true, 'data' => $landingTypes ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_get_all_landing_types']); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function getLandingTypes(string $brandId): void { try { header('Content-Type: application/json'); $brandIdInt = (int) Sanitize::string($brandId); $landingTypeModel = new \App\Modules\Advertising\Models\LandingTypeModel(); $landingTypes = $landingTypeModel->getByBrandId($brandIdInt); if (empty($landingTypes)) { $landingTypeModel->createDefaultForBrand($brandIdInt); $landingTypes = $landingTypeModel->getByBrandId($brandIdInt); } echo json_encode([ 'success' => true, 'data' => $landingTypes ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_get_landing_types', 'brandId' => $brandId]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function createBrand(): void { try { header('Content-Type: application/json'); $input = json_decode(file_get_contents('php://input'), true); if (!$input || !isset($input['name'])) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Nome é obrigatório']); return; } $brandModel = new \App\Modules\Advertising\Models\BrandModel(); $id = $brandModel->create($input); echo json_encode([ 'success' => true, 'message' => 'Marca criada com sucesso', 'data' => ['id' => $id] ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_create_brand']); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro ao criar marca: ' . $e->getMessage()]); } } public function updateBrand(int $id): void { try { header('Content-Type: application/json'); if ($id <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } $input = json_decode(file_get_contents('php://input'), true); if (!$input) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Dados inválidos']); return; } $brandModel = new \App\Modules\Advertising\Models\BrandModel(); $success = $brandModel->update($id, $input); if ($success) { echo json_encode(['success' => true, 'message' => 'Marca atualizada com sucesso']); } else { http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro ao atualizar marca']); } } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_update_brand', 'id' => $id]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function deleteBrand(int $id): void { try { header('Content-Type: application/json'); if ($id <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } $brandModel = new \App\Modules\Advertising\Models\BrandModel(); $success = $brandModel->destroy($id); if ($success) { echo json_encode(['success' => true, 'message' => 'Marca desativada com sucesso']); } else { http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro ao desativar marca']); } } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_delete_brand', 'id' => $id]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function createLandingType(): void { try { header('Content-Type: application/json'); $input = json_decode(file_get_contents('php://input'), true); if (!$input || !isset($input['brand_id']) || !isset($input['name']) || !isset($input['template_url'])) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Campos obrigatórios: brand_id, name, template_url']); return; } // Validar template $validationService = new TemplateValidationService(); $validation = $validationService->validateTemplate($input); if (!$validation['valid']) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => 'Template inválido', 'validation' => $validation ]); return; } $landingTypeModel = new \App\Modules\Advertising\Models\LandingTypeModel(); $id = $landingTypeModel->create($input); // Registrar no histórico $historyService = new LandingTypeHistoryService(); $historyService->logChange($id, 'created', [], $input); echo json_encode([ 'success' => true, 'message' => 'Tipo de landing criado com sucesso', 'data' => ['id' => $id], 'validation' => $validation ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_create_landing_type']); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro ao criar tipo de landing: ' . $e->getMessage()]); } } public function updateLandingType(int $id): void { try { header('Content-Type: application/json'); if ($id <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } $input = json_decode(file_get_contents('php://input'), true); if (!$input) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Dados inválidos']); return; } // Obter dados antigos para histórico $landingTypeModel = new \App\Modules\Advertising\Models\LandingTypeModel(); $oldData = $landingTypeModel->getById($id); // Validar template $validationService = new TemplateValidationService(); $validation = $validationService->validateTemplate($input); if (!$validation['valid']) { http_response_code(400); echo json_encode([ 'success' => false, 'message' => 'Template inválido', 'validation' => $validation ]); return; } $success = $landingTypeModel->update($id, $input); if ($success) { // Registrar no histórico $historyService = new LandingTypeHistoryService(); $historyService->logChange($id, 'updated', $oldData ?? [], $input); echo json_encode([ 'success' => true, 'message' => 'Tipo de landing atualizado com sucesso', 'validation' => $validation ]); } else { http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro ao atualizar tipo de landing']); } } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_update_landing_type', 'id' => $id]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function deleteLandingType(int $id): void { try { header('Content-Type: application/json'); if ($id <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } // Obter dados para histórico $landingTypeModel = new \App\Modules\Advertising\Models\LandingTypeModel(); $oldData = $landingTypeModel->getById($id); $success = $landingTypeModel->destroy($id); if ($success) { // Registrar no histórico $historyService = new LandingTypeHistoryService(); $historyService->logChange($id, 'deleted', $oldData ?? [], []); echo json_encode(['success' => true, 'message' => 'Tipo de landing desativado com sucesso']); } else { http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro ao desativar tipo de landing']); } } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_delete_landing_type', 'id' => $id]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function processTemplateUrl(?int $landingTypeId): void { if ($landingTypeId === null) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Landing Type ID is required']); return; } try { header('Content-Type: application/json'); if ($landingTypeId <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } $input = json_decode(file_get_contents('php://input'), true); if (!$input || !isset($input['variables'])) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Variáveis são obrigatórias']); return; } $landingTypeModel = new \App\Modules\Advertising\Models\LandingTypeModel(); $landingType = $landingTypeModel->getById($landingTypeId); if (!$landingType) { http_response_code(404); echo json_encode(['success' => false, 'message' => 'Tipo de landing não encontrado']); return; } $processedUrl = $landingTypeModel->processTemplateUrl( $landingType['template_url'], $input['variables'] ); echo json_encode([ 'success' => true, 'data' => ['processed_url' => $processedUrl] ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_process_template_url', 'landingTypeId' => $landingTypeId]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function validateTemplate(): void { try { header('Content-Type: application/json'); $input = json_decode(file_get_contents('php://input'), true); if (!$input) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'Dados do template são obrigatórios']); return; } $validationService = new TemplateValidationService(); $validation = $validationService->validateTemplate($input); echo json_encode([ 'success' => true, 'data' => $validation, 'report' => $validationService->generateValidationReport($validation) ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_validate_template']); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function getLandingTypeHistory(int $landingTypeId): void { try { header('Content-Type: application/json'); if ($landingTypeId <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } $historyService = new LandingTypeHistoryService(); $history = $historyService->getHistory($landingTypeId); $statistics = $historyService->getChangeStatistics($landingTypeId); echo json_encode([ 'success' => true, 'data' => [ 'history' => $history, 'statistics' => $statistics, 'report' => $historyService->generateChangeReport($history) ] ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_get_landing_type_history', 'landingTypeId' => $landingTypeId]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } public function getBrandHistory(int $brandId): void { try { header('Content-Type: application/json'); if ($brandId <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ID inválido']); return; } $historyService = new LandingTypeHistoryService(); $history = $historyService->getBrandHistory($brandId); $statistics = $historyService->getChangeStatistics(null, $brandId); echo json_encode([ 'success' => true, 'data' => [ 'history' => $history, 'statistics' => $statistics, 'report' => $historyService->generateChangeReport($history) ] ]); } catch (\Exception $e) { ErrorReporter::report($e, ['context' => 'advertising_brand_api_get_brand_history', 'brandId' => $brandId]); http_response_code(500); echo json_encode(['success' => false, 'message' => 'Erro interno do servidor']); } } }