<?php

namespace App\Services\Governance;

use App\Core\Database;
use PDO;

/*
|--------------------------------------------------------------------------
| Enterprise Governance Suite
| Reviewer Integrity Service
|
| Read-only service.
| Validates reviewer assignments.
|--------------------------------------------------------------------------
*/

final class ReviewerIntegrityService
{
    public static function summary(): array
    {
        $db = Database::getInstance();

        $healthy = 0;
        $warnings = 0;
        $errors = 0;

        $items = [];

        $stmt = $db->query("
            SELECT

                ra.id,

                ra.submission_id,

                ra.reviewer_id,

                ra.status,

                ra.due_date,

                s.submission_no,

                u.email

            FROM review_assignments ra

            LEFT JOIN submissions s
                ON s.id = ra.submission_id

            LEFT JOIN users u
                ON u.id = ra.reviewer_id

            ORDER BY ra.id DESC
        ");

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

            $state = 'healthy';

            $notes = [];

            /*
            ------------------------------------------------------------
            Missing reviewer
            ------------------------------------------------------------
            */

            if (
                (int)$row['reviewer_id'] <= 0
                ||
                empty($row['email'])
            ) {

                $state = 'error';

                $notes[] =
                    'Reviewer account missing';

            }

            /*
            ------------------------------------------------------------
            Missing submission
            ------------------------------------------------------------
            */

            if (
                (int)$row['submission_id'] <= 0
                ||
                empty($row['submission_no'])
            ) {

                $state = 'error';

                $notes[] =
                    'Submission missing';

            }

            /*
            ------------------------------------------------------------
            Overdue review
            ------------------------------------------------------------
            */

            if (

                !empty($row['due_date'])

                &&

                strtotime($row['due_date']) < time()

                &&

                $row['status'] !== 'completed'

            ) {

                if ($state !== 'error') {

                    $state = 'warning';

                }

                $notes[] =
                    'Review overdue';

            }

            switch ($state) {

                case 'healthy':

                    $healthy++;

                    break;

                case 'warning':

                    $warnings++;

                    break;

                default:

                    $errors++;

            }

            $items[] = [

                'assignment_id' =>
                    (int)$row['id'],

                'submission_no' =>
                    $row['submission_no'],

                'reviewer' =>
                    $row['email'],

                'status' =>
                    $row['status'],

                'state' =>
                    $state,

                'notes' =>
                    $notes

            ];

        }

        $total =
            max(
                1,
                $healthy + $warnings + $errors
            );

        return [

            'score' =>
                (int)round(
                    ($healthy / $total) * 100
                ),

            'healthy' =>
                $healthy,

            'warnings' =>
                $warnings,

            'errors' =>
                $errors,

            'items' =>
                $items

        ];
    }
}
