<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
verify_csrf();
mj_user_limits_ensure_schema();
$adminTitle = 'Listing Expiry Notifications';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    q('INSERT INTO mj_settings(setting_key,setting_value) VALUES("listing_expiry_notify_enabled",?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)', [isset($_POST['listing_expiry_notify_enabled'])?'1':'0']);
    q('INSERT INTO mj_settings(setting_key,setting_value) VALUES("listing_expiry_email_enabled",?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)', [isset($_POST['listing_expiry_email_enabled'])?'1':'0']);
    q('INSERT INTO mj_settings(setting_key,setting_value) VALUES("listing_expiry_auto_renew_link_enabled",?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)', [isset($_POST['listing_expiry_auto_renew_link_enabled'])?'1':'0']);
    $days = preg_replace('/[^0-9,]/', '', (string)($_POST['listing_expiry_notify_days_before'] ?? '7,3,1,0'));
    q('INSERT INTO mj_settings(setting_key,setting_value) VALUES("listing_expiry_notify_days_before",?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)', [$days ?: '7,3,1,0']);
    flash('success','Expiry notification settings saved.');
    redirect('listing-expiry-settings.php');
}

include __DIR__.'/_header.php';
?>
<div class="card card-primary card-outline">
  <div class="card-header">
    <h3 class="card-title"><i class="fas fa-clock mr-2"></i>Listing Expiry Notifications</h3>
    <div class="card-tools"><a class="btn btn-sm btn-outline-primary" href="listing-renewals.php">Renewal Requests</a></div>
  </div>
  <form method="post">
    <div class="card-body">
      <?php show_flash(); ?>
      <?=csrf_input()?>
      <div class="alert alert-info">
        This controls automatic in-app notification and email queue when listings are near expiry or already expired.
        Run cron file: <code>cron/listing-expiry-reminders.php</code>
      </div>

      <div class="custom-control custom-switch mb-3">
        <input type="checkbox" class="custom-control-input" id="notify" name="listing_expiry_notify_enabled" <?=setting('listing_expiry_notify_enabled','1')==='1'?'checked':''?>>
        <label class="custom-control-label" for="notify">Enable in-app expiry notifications</label>
      </div>

      <div class="custom-control custom-switch mb-3">
        <input type="checkbox" class="custom-control-input" id="email" name="listing_expiry_email_enabled" <?=setting('listing_expiry_email_enabled','1')==='1'?'checked':''?>>
        <label class="custom-control-label" for="email">Queue email notification to user</label>
      </div>

      <div class="custom-control custom-switch mb-3">
        <input type="checkbox" class="custom-control-input" id="renew" name="listing_expiry_auto_renew_link_enabled" <?=setting('listing_expiry_auto_renew_link_enabled','1')==='1'?'checked':''?>>
        <label class="custom-control-label" for="renew">Include renew action link</label>
      </div>

      <div class="form-group">
        <label>Notify before expiry days</label>
        <input class="form-control" name="listing_expiry_notify_days_before" value="<?=e(setting('listing_expiry_notify_days_before','7,3,1,0'))?>">
        <small class="text-muted">Comma separated. Example: 7,3,1,0. Use 0 for expired/today notification.</small>
      </div>
    </div>
    <div class="card-footer">
      <button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Settings</button>
    </div>
  </form>
</div>
<?php include __DIR__.'/_footer.php'; ?>
