<?php
session_start();
require_once 'config.php';

header('Content-Type: application/json');

if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$activity_id = $_POST['activity_id'] ?? 0;
$school_id = $_POST['school_id'] ?? 0;

if ($activity_id == 0 || $school_id == 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid parameters']);
    exit;
}

try {
    // First, delete any marks associated with this activity
    $sql_marks = "DELETE FROM integration_activity_marks WHERE activity_id = ?";
    $stmt_marks = $conn->prepare($sql_marks);
    $stmt_marks->bind_param("i", $activity_id);
    $stmt_marks->execute();
    
    // Then delete the activity
    $sql = "DELETE FROM integration_activities WHERE activity_id = ? AND school_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ii", $activity_id, $school_id);
    
    if ($stmt->execute()) {
        echo json_encode(['success' => true, 'message' => 'Activity deleted successfully!']);
    } else {
        echo json_encode(['success' => false, 'message' => 'Error deleting activity']);
    }
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>