<?php

require_once '../../core/api_bootstrap.php';
require_once '../../core/employee_auth.php';

EmployeeAuth::check('manage_ads');

$data = Request::post();

try {

Validator::required($data,['id']);

$db = new Database();
$conn = $db->connect();

/* check ad exists */

$stmt = $conn->prepare("
SELECT id
FROM ads
WHERE id=:id
LIMIT 1
");

$stmt->execute([
':id'=>$data['id']
]);

if(!$stmt->fetch()){
Response::error("Ad not found");
}

/* delete ad */

$conn->prepare("

DELETE FROM ads
WHERE id=:id

")->execute([
':id'=>$data['id']
]);

Response::success([],"Ad deleted successfully");

}
catch(Exception $e){

Logger::log("system.log",$e->getMessage());

Response::error("Server error",500);

}
