<?php

namespace App\Services\Governance;

use App\Core\Database;
use PDO;

/*
|--------------------------------------------------------------------------
| Enterprise Governance Suite
| Production Integrity Service
|
| Read-only service.
| Validates production workflow consistency.
|--------------------------------------------------------------------------
*/

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

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

        $items = [];

        $stmt = $db->query("
            SELECT

                s.id,

                s.submission_no,

                s.status,

                s.stage,

                s.production_status,

                s.decision,

                (
                    SELECT COUNT(*)
                    FROM production_galleys pg
                    WHERE pg.submission_id = s.id
                ) AS galley_count

            FROM submissions s

            ORDER BY s.id DESC
        ");

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

            $state = 'healthy';

            $notes = [];

            /*
            ------------------------------------------------------------
            Production without accepted decision
            ------------------------------------------------------------
            */

            if (

                $row['stage'] === 'production'

                &&

                !in_array(
                    $row['decision'],
                    ['accept','accepted'],
                    true
                )

            ) {

                $state = 'warning';

                $notes[] =
                    'Production entered before acceptance';

            }

            /*
            ------------------------------------------------------------
            Production without galley
            ------------------------------------------------------------
            */

            if (

                $row['stage'] === 'production'

                &&

                (int)$row['galley_count'] === 0

            ) {

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

                    $state = 'warning';

                }

                $notes[] =
                    'No production galley found';

            }

            /*
            ------------------------------------------------------------
            Published without production approval
            ------------------------------------------------------------
            */

            if (

                $row['stage'] === 'published'

                &&

                $row['production_status'] !== 'approved'

            ) {

                $state = 'warning';

                $notes[] =
                    'Published while production status is not approved';

            }

            switch ($state) {

                case 'healthy':

                    $healthy++;

                    break;

                case 'warning':

                    $warnings++;

                    break;

                default:

                    $errors++;

                    break;

            }

            $items[] = [

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

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

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

                'production_status' =>
                    $row['production_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

        ];
    }
}
