<?php
// ajax/delete_submission.php
error_reporting(0);
require_once __DIR__ . '/../config/db.php';
startSession();
header('Content-Type: application/json');

$user    = getCurrentUser();
$user_id = $user['user_id'];
$sid     = (int)($_POST['submission_id'] ?? 0);

if (!$sid) { echo json_encode(['success'=>false,'message'=>'Invalid ID']); exit; }

try {
    // Delete related data first
    $tables = ['manuscript_info','submission_authors','submission_editorial',
               'submission_statements','submission_payment','submission_keywords',
               'suggested_reviewers','author_contributions'];
    foreach($tables as $t){
        try { db()->prepare("DELETE FROM $t WHERE submission_id=?")->execute([$sid]); }
        catch(PDOException $e){}
    }
    // Delete submission
    $q = db()->prepare("DELETE FROM submissions WHERE submission_id=? AND user_id=?");
    $q->execute([$sid, $user_id]);
    echo json_encode(['success'=>true]);
} catch(PDOException $e){
    echo json_encode(['success'=>false,'message'=>$e->getMessage()]);
}