<?php
declare(strict_types=1);
if (PHP_SAPI !== 'cli') { http_response_code(404); exit; }
require __DIR__.'/lib/bootstrap.php';
$values=[];
foreach(db()->query("SELECT setting_key,value FROM settings WHERE setting_group='system_settings'") as $row)$values[$row['setting_key']]=$row['value'];
if(($values['reminder_email_enabled']??'0')!=='1')exit("Reminders disabled\n");
$recipients=trim($values['reminder_email_recipients']??'');
if($recipients==='')exit("No recipients\n");
$q=db()->query("SELECT c.name client_name,ci.title,ci.scheduled_at,ci.published_at FROM content_items ci JOIN projects p ON p.id=ci.project_id JOIN clients c ON c.id=p.client_id WHERE DATE(ci.scheduled_at) BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 3 DAY) ORDER BY ci.scheduled_at,c.name");
$today=[];$next=[];
foreach($q as $x){$line=$x['client_name'].' - '.$x['title'].' ('.$x['scheduled_at'].') - '.($x['published_at']?'Published':'Upcoming');if(date('Y-m-d',strtotime($x['scheduled_at']))===date('Y-m-d'))$today[]=$line;else $next[]=$line;}
$body="TODAY - UPCOMING / PUBLISHED\n".($today?implode("\n",$today):'No content scheduled.')."\n\nNEXT 3 DAYS\n".($next?implode("\n",$next):'No upcoming content.');
$from=trim($values['reminder_email_from']??'brandbuilders.consulting21@gmail.com');
$headers="From: Bright Marketing Solutions <{$from}>\r\nReply-To: {$from}\r\nContent-Type: text/plain; charset=UTF-8";
echo mail($recipients,'Bright Marketing Solutions - Content schedule',$body,$headers)?"Sent\n":"Mail server rejected message\n";
