<?php
include('../config/database.php');
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $proposal_id = intval($_POST['proposal_id']);
    $assessor_id = intval($_POST['assessor_id']);

    $idea_novelty = intval($_POST['idea_novelty']);
    $idea_breakthrough = intval($_POST['idea_breakthrough']);
    $idea_competitors = intval($_POST['idea_competitors']);
    $idea_technology_solution = intval($_POST['idea_technology_solution']);
    $idea_novel_future = intval($_POST['idea_novel_future']);
    $market_need = intval($_POST['market_need']);
    $market_problem = intval($_POST['market_problem']);
    $market_present_future = intval($_POST['market_present_future']);
    $market_share = intval($_POST['market_share']);
    $market_risk = intval($_POST['market_risk']);
    $market_timing = intval($_POST['market_timing']);
    $return_resources = intval($_POST['return_resources']);
    $return_profit = intval($_POST['return_profit']);
    $return_investors = intval($_POST['return_investors']);
    $return_team_capacity = intval($_POST['return_team_capacity']);
    $advantage_competitors = intval($_POST['advantage_competitors']);
    $advantage_better = intval($_POST['advantage_better']);
    $advantage_protectable = intval($_POST['advantage_protectable']);
    $impact_sdg = intval($_POST['impact_sdg']);
    $impact_jobs = intval($_POST['impact_jobs']);

    $total_score = $idea_novelty + $idea_breakthrough + $idea_competitors + $idea_technology_solution + 
                   $idea_novel_future + $market_need + $market_problem + $market_present_future + 
                   $market_share + $market_risk + $market_timing + $return_resources + $return_profit + 
                   $return_investors + $return_team_capacity + $advantage_competitors + $advantage_better + 
                   $advantage_protectable + $impact_sdg + $impact_jobs;

    // Check if assessment exists
    $sql = "SELECT * FROM assessments WHERE proposal_id = ? AND assessor_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ii", $proposal_id, $assessor_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $assessment_exists = ($result->num_rows > 0);
    $stmt->close();

    // Insert or update assessment
    if ($assessment_exists) {
        $sql = "UPDATE assessments SET 
                idea_novelty = ?, idea_breakthrough = ?, idea_competitors = ?, idea_technology_solution = ?, 
                idea_novel_future = ?, market_need = ?, market_problem = ?, market_present_future = ?, 
                market_share = ?, market_risk = ?, market_timing = ?, return_resources = ?, return_profit = ?, 
                return_investors = ?, return_team_capacity = ?, advantage_competitors = ?, advantage_better = ?, 
                advantage_protectable = ?, impact_sdg = ?, impact_jobs = ? 
                WHERE proposal_id = ? AND assessor_id = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("iiiiiiiiiiiiiiiiiiiiii", 
            $idea_novelty, $idea_breakthrough, $idea_competitors, $idea_technology_solution, $idea_novel_future, 
            $market_need, $market_problem, $market_present_future, $market_share, $market_risk, $market_timing, 
            $return_resources, $return_profit, $return_investors, $return_team_capacity, $advantage_competitors, 
            $advantage_better, $advantage_protectable, $impact_sdg, $impact_jobs, $proposal_id, $assessor_id);
    } else {
        $sql = "INSERT INTO assessments 
                (proposal_id, assessor_id, idea_novelty, idea_breakthrough, idea_competitors, idea_technology_solution, 
                idea_novel_future, market_need, market_problem, market_present_future, market_share, market_risk, 
                market_timing, return_resources, return_profit, return_investors, return_team_capacity, 
                advantage_competitors, advantage_better, advantage_protectable, impact_sdg, impact_jobs) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("iiiiiiiiiiiiiiiiiiiiii", 
            $proposal_id, $assessor_id, $idea_novelty, $idea_breakthrough, $idea_competitors, $idea_technology_solution, 
            $idea_novel_future, $market_need, $market_problem, $market_present_future, $market_share, $market_risk, 
            $market_timing, $return_resources, $return_profit, $return_investors, $return_team_capacity, 
            $advantage_competitors, $advantage_better, $advantage_protectable, $impact_sdg, $impact_jobs);
    }

    if ($stmt->execute()) {
        $stmt->close();

        // Move proposal to appropriate table based on the new score
        if ($total_score >= 70) {
            // Check if it exists in rejected_proposals and remove it
            $sql = "DELETE FROM rejected_proposals WHERE full_name = (SELECT full_name FROM proposals WHERE id = ?)";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("i", $proposal_id);
            $stmt->execute();
            $stmt->close();

            // Insert into accepted_proposals
            $sql = "INSERT INTO accepted_proposals (full_name, business_idea_title, phone, email)
                    SELECT full_name, business_idea_title, contact, email FROM proposals WHERE id = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("i", $proposal_id);
            $stmt->execute();
            $stmt->close();
        } else {
            // Check if it exists in accepted_proposals and remove it
            $sql = "DELETE FROM accepted_proposals WHERE full_name = (SELECT full_name FROM proposals WHERE id = ?)";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("i", $proposal_id);
            $stmt->execute();
            $stmt->close();

            // Insert into rejected_proposals
            $sql = "INSERT INTO rejected_proposals (full_name, business_idea_title, phone, email)
                    SELECT full_name, business_idea_title, contact, email FROM proposals WHERE id = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("i", $proposal_id);
            $stmt->execute();
            $stmt->close();
        }

        header("Location: index.php");
        exit();
    } else {
        die("Error submitting assessment: " . $stmt->error);
    }
} else {
    die("Invalid request.");
}

$conn->close();
?>
