<?php
session_start();

include __DIR__.'/../database/search_log_config.php';
include "includes/activity_logger.php";

// Check admin login
if(!isset($_SESSION['admin_id'])){
    header("Location: login.php");
    exit;
}

$id = (int)$_GET['id'];

// OPTIONAL: Get keyword before deleting (better logging)
$stmtSelect = $log_conn->prepare("SELECT keyword FROM sponsored_keywords WHERE id=?");
$stmtSelect->bind_param("i", $id);
$stmtSelect->execute();
$result = $stmtSelect->get_result();
$row = $result->fetch_assoc();
$stmtSelect->close();

$keyword = $row['keyword'] ?? 'Unknown';

// Delete safely using prepared statement
$stmt = $log_conn->prepare("DELETE FROM sponsored_keywords WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();

// 🔥 LOG ACTIVITY HERE
logActivity(
    $log_conn,
    $_SESSION['admin_id'],
    "Deleted Sponsored Keyword: ".$keyword." (ID: ".$id.")"
);

// Redirect
header("Location: view_sponsored.php");
exit;
?>