<?php
// Include session check for admin protection
include('session_check.php');

// Include database configuration
include('../../config/database.php');

// Fetch assessments from the database
$sql = "SELECT 
            proposals.full_name AS applicant_name,
            proposals.business_idea_title,
            assessors.name AS assessor_name,
            (assessments.idea_novelty + assessments.idea_breakthrough + assessments.idea_competitors + assessments.idea_technology_solution + 
            assessments.idea_novel_future + assessments.market_need + assessments.market_problem + assessments.market_present_future + 
            assessments.market_share + assessments.market_risk + assessments.market_timing + assessments.return_resources + 
            assessments.return_profit + assessments.return_investors + assessments.return_team_capacity + assessments.advantage_competitors + 
            assessments.advantage_better + assessments.advantage_protectable + assessments.impact_sdg + assessments.impact_jobs) AS total_score
        FROM assessments
        JOIN proposals ON assessments.proposal_id = proposals.id
        JOIN assessors ON assessments.assessor_id = assessors.id";
$result = $conn->query($sql);

$conn->close();
?>

<?php include('../../includes/header.php'); ?>
<div class="container mt-5">
    <h2 class="mb-4">Applicant Scores by Assessor</h2>
    <table class="table table-striped">
        <thead class="table-dark">
            <tr>
                <th>Applicant Name</th>
                <th>Business Idea Title</th>
                <th>Assessor Name</th>
                <th>Total Score</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . htmlspecialchars($row['applicant_name']) . "</td>";
                    echo "<td>" . htmlspecialchars($row['business_idea_title']) . "</td>";
                    echo "<td>" . htmlspecialchars($row['assessor_name']) . "</td>";
                    echo "<td>" . htmlspecialchars($row['total_score']) . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr><td colspan='4'>No assessments found</td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>
<?php include('../../includes/footer.php'); ?>
