<?php
// delete_blog_post.php - Delete Blog Post
// File location: /bidwheels.net/admin/delete_blog_post.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';

requireAdmin();

$post_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($post_id <= 0) {
    setError('Invalid post ID.');
    redirect('/admin/blog_posts.php');
}

// Get featured image to delete
$post = fetchOne("SELECT featured_image FROM blog_posts WHERE id = $post_id");

if ($post && $post['featured_image']) {
    $filepath = $_SERVER['DOCUMENT_ROOT'] . '/bidwheels/uploads/blog/' . $post['featured_image'];
    if (file_exists($filepath)) {
        unlink($filepath);
    }
}

delete('blog_posts', "id = $post_id");

setSuccess('Blog post deleted successfully.');
redirect('/admin/blog_posts.php');
?>