<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require_login();

function build_content_redirect_target($returnQuery, $extraParams)
{
    $returnQuery = trim((string)$returnQuery);
    $returnQuery = preg_replace('/^content\.php\??/i', '', $returnQuery);
    $base = '/admin/content/list.php';
    $query = $returnQuery !== '' ? $returnQuery . '&' : '';
    return $base . '?' . $query . http_build_query($extraParams);
}

if (strtoupper(isset($_SERVER['REQUEST_METHOD']) ? (string)$_SERVER['REQUEST_METHOD'] : 'GET') !== 'POST') {
    redirect('/admin/content/list.php?view=article');
}

require_csrf();
$returnQuery = isset($_POST['return_query']) ? (string)$_POST['return_query'] : 'view=article';
$batchAction = isset($_POST['batch_action']) ? trim((string)$_POST['batch_action']) : '';
$ids = isset($_POST['ids']) && is_array($_POST['ids']) ? $_POST['ids'] : array();
$ids = array_values(array_unique(array_filter(array_map('intval', $ids), function ($id) {
    return $id > 0;
})));

if (empty($ids)) {
    redirect(build_content_redirect_target($returnQuery, array(
        'batch_status' => 'empty',
        'batch_action' => $batchAction
    )));
}

$processedCount = 0;

try {
    if ($batchAction === 'delete') {
        foreach ($ids as $id) {
            if (find_content_by_id($id)) {
                delete_content($id);
                $processedCount++;
            }
        }
    } elseif ($batchAction === 'draft_publish') {
        foreach ($ids as $id) {
            $c = find_content_by_id($id);
            if ($c && isset($c['status']) && $c['status'] === 'draft') {
                update_content_status($id, 'published');
                $processedCount++;
            }
        }
    } elseif ($batchAction === 'static_publish') {
        foreach ($ids as $id) {
            if (find_content_by_id($id)) {
                publish_content_static($id);
                $processedCount++;
            }
        }
    } elseif ($batchAction === 'publish') {
        foreach ($ids as $id) {
            if (find_content_by_id($id)) {
                update_content_status($id, 'published');
                $processedCount++;
            }
        }
    } elseif ($batchAction === 'unpublish') {
        foreach ($ids as $id) {
            if (find_content_by_id($id)) {
                update_content_status($id, 'draft');
                $processedCount++;
            }
        }
    } else {
        redirect(build_content_redirect_target($returnQuery, array(
            'batch_status' => 'error',
            'batch_action' => $batchAction
        )));
    }

    redirect(build_content_redirect_target($returnQuery, array(
        'batch_status' => 'success',
        'batch_action' => $batchAction,
        'batch_count' => $processedCount
    )));
} catch (Exception $e) {
    redirect(build_content_redirect_target($returnQuery, array(
        'batch_status' => 'error',
        'batch_action' => $batchAction
    )));
}
