<?php

define('BASE_PATH', realpath(__DIR__ . '/../..'));

require_once BASE_PATH . '/config/autoload.php';
require_once BASE_PATH . '/config/bootstrap.php';

$db = \App\Core\Database::getInstance();

echo "AUTHOR SUBMISSION READINESS REBUILD\n";

$db->exec("TRUNCATE TABLE author_submission_readiness");

$rows = $db->query("
    SELECT
        id,
        author_id,
        COALESCE(conference_id,0) AS conference_id,
        title,
        title_en,
        title_ar,
        abstract,
        abstract_en,
        abstract_ar,
        keywords,
        keywords_en,
        keywords_ar,
        file_path,
        original_filename,
        status,
        avg_score,
        recommended_decision
    FROM submissions
    WHERE author_id IS NOT NULL
")->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $r) {

    $metadata = 0;

    if (!empty($r['title']) || !empty($r['title_en']) || !empty($r['title_ar'])) {
        $metadata += 25;
    }

    if (!empty($r['abstract']) || !empty($r['abstract_en']) || !empty($r['abstract_ar'])) {
        $metadata += 35;
    }

    if (!empty($r['keywords']) || !empty($r['keywords_en']) || !empty($r['keywords_ar'])) {
        $metadata += 20;
    }

    if (!empty($r['original_filename'])) {
        $metadata += 20;
    }

    $metadata = min(100, $metadata);

    $fileScore = !empty($r['file_path']) ? 100 : 0;

    $reviewScore = 0;

    if ($r['avg_score'] !== null && $r['avg_score'] !== '') {
        $reviewScore = max(
            0,
            min(
                100,
                (float)$r['avg_score'] * 10
            )
        );
    } else {
        $reviewScore = 50;
    }

    $readiness = round(
        ($metadata * 0.45)
        + ($fileScore * 0.30)
        + ($reviewScore * 0.25),
        2
    );

    $level = 'Needs Improvement';

    if ($readiness >= 85) {
        $level = 'Ready';
    } elseif ($readiness >= 70) {
        $level = 'Almost Ready';
    } elseif ($readiness >= 50) {
        $level = 'Developing';
    }

    $risk = 'Medium';

    if ($readiness >= 80) {
        $risk = 'Low';
    } elseif ($readiness < 50) {
        $risk = 'High';
    }

    $recommendation = 'Complete manuscript metadata, ensure file upload, and respond carefully to review feedback.';

    if ($level === 'Ready') {
        $recommendation = 'Submission appears ready. Maintain clarity, completeness, and formatting quality.';
    } elseif ($risk === 'High') {
        $recommendation = 'Submission requires attention: complete metadata, attach manuscript file, and improve readiness before review.';
    }

    $db->prepare("
        INSERT INTO author_submission_readiness
        (
            author_id,
            conference_id,
            submission_id,
            readiness_score,
            metadata_score,
            file_score,
            review_score,
            readiness_level,
            risk_level,
            recommendation_text
        )
        VALUES
        (?,?,?,?,?,?,?,?,?,?)
    ")->execute([
        (int)$r['author_id'],
        (int)$r['conference_id'],
        (int)$r['id'],
        $readiness,
        $metadata,
        $fileScore,
        $reviewScore,
        $level,
        $risk,
        $recommendation
    ]);
}

echo "DONE\n";
