<?php
require_once __DIR__.'/_bootstrap.php';
use App\Core\Database;
$job='abandoned_cart_recovery';$started=date('Y-m-d H:i:s');try{$rows=Database::fetchAll('SELECT DISTINCT user_id FROM carts WHERE user_id IS NOT NULL AND abandoned_at IS NULL AND recovered_at IS NULL AND COALESCE(updated_at,created_at)<DATE_SUB(NOW(),INTERVAL 2 HOUR)');$stmt=Database::query('UPDATE carts SET abandoned_at=NOW() WHERE abandoned_at IS NULL AND recovered_at IS NULL AND COALESCE(updated_at,created_at)<DATE_SUB(NOW(),INTERVAL 2 HOUR)');foreach($rows as $r){Database::safeExecute('INSERT INTO notifications (user_id,channel,title,message,created_at) VALUES (?,"in_app","Items waiting in your cart","You still have products in your cart. Complete checkout while stock is available.",NOW())',[$r['user_id']]);}cron_log($job,'success','Marked '.$stmt->rowCount().' cart row(s) abandoned and notified '.count($rows).' customer(s).',$started);}catch(Throwable $e){cron_log($job,'failed',$e->getMessage(),$started);exit(1);}
