<?php

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

try {

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

/* subscriptions expiring in 3 days */

$stmt = $conn->prepare("

SELECT
p.email,
ps.expiry_date

FROM provider_subscriptions ps

JOIN providers p
ON ps.provider_id = p.id

WHERE ps.expiry_date = DATE_ADD(CURDATE(),INTERVAL 3 DAY)
AND ps.status='active'

");

$stmt->execute();

$providers = $stmt->fetchAll();

foreach($providers as $provider){

/* send email */

mail(
$provider['email'],
"Subscription Expiring Soon",
"Your subscription will expire on ".$provider['expiry_date']
);

}

echo "Reminders sent";

}
catch(Exception $e){

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

}
