<?php
require_once __DIR__ . '/../inc/auth.php';

require_login();

$mainConfig = require __DIR__ . '/../../config/config.php';
$verticals = $mainConfig['verticals'] ?? [];
$posted = $_POST['rules'] ?? [];

$final = [];

foreach ($verticals as $verticalKey => $cfg) {
    $enabled = !empty($posted[$verticalKey]['enabled']);
    $minBid = (float)($posted[$verticalKey]['min_bid'] ?? 0);

    if ($minBid < 0) $minBid = 0;

    $final[$verticalKey] = [
        'enabled' => $enabled,
        'min_bid' => $minBid,
    ];
}

file_put_contents(__DIR__ . '/bids.json', json_encode($final, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
header('Location: index.php?saved=1');
exit;