<?php
require '../config/function.php';

$paramResult = checkParamId('projectID');
if (is_numeric($paramResult)) {
    $projectID = validate($paramResult);
    $projects = getBymId('projects', $projectID);
    if ($projects['status'] == 200) {
        // Delete associated milestones
        $deleteMilestonesQuery = "DELETE FROM milestones WHERE projectID = $projectID";
        $milestonesDeleted = mysqli_query($conn, $deleteMilestonesQuery);

        if ($milestonesDeleted) {
            // Now delete the project
            $projectDeleteRes = deletePQuery('projects', $projectID);

            if ($projectDeleteRes) {
                redirect('projects.php', 'Project and Associated Milestones Deleted Successfully');
            } else {
                $errorInfo = mysqli_error($conn); // Get the MySQL error message
                redirect('projects.php', 'Failed to delete the project. Error: ' . $errorInfo);
            }
        } else {
            redirect('projects.php', 'Failed to Delete Associated Milestones.');
        }
    } else {
        redirect('projects.php', $projects['message']);
    }
} else {
    redirect('projects.php', $paramResult);
}
?>