<?php

namespace App\Services;

use App\Core\Database;
use PDO;
use Throwable;

class HomepageAnalyticsService
{
    public static function build(
        int $conferenceId,
        string $lang = 'en'
    ): array {

        $db =
            Database::getInstance();

        $stats = [
            'submissions' => 0,
            'accepted' => 0,
            'under_review' => 0,
            'speakers' => 0,
            'tracks' => 0,
            'attendees' => 0,
            'acceptance_rate' => 0,
            'review_completion' => 0
        ];

        try {

            /*
            |--------------------------------------------------------------------------
            | SUBMISSIONS
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("
                SELECT COUNT(*) 
                FROM submissions
                WHERE conference_id = ?
            ");

            $stmt->execute([$conferenceId]);

            $stats['submissions'] =
                (int)$stmt->fetchColumn();

            /*
            |--------------------------------------------------------------------------
            | ACCEPTED
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("
                SELECT COUNT(*)
                FROM submissions
                WHERE conference_id = ?
                AND status = 'accepted'
            ");

            $stmt->execute([$conferenceId]);

            $stats['accepted'] =
                (int)$stmt->fetchColumn();

            /*
            |--------------------------------------------------------------------------
            | UNDER REVIEW
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("
                SELECT COUNT(*)
                FROM submissions
                WHERE conference_id = ?
                AND status IN (
                    'under_review',
                    'revision_required'
                )
            ");

            $stmt->execute([$conferenceId]);

            $stats['under_review'] =
                (int)$stmt->fetchColumn();

            /*
            |--------------------------------------------------------------------------
            | SPEAKERS
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("
                SELECT COUNT(*)
                FROM conference_speakers
                WHERE conference_id = ?
            ");

            $stmt->execute([$conferenceId]);

            $stats['speakers'] =
                (int)$stmt->fetchColumn();

            /*
            |--------------------------------------------------------------------------
            | TRACKS
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("
                SELECT COUNT(*)
                FROM tracks
                WHERE conference_id = ?
            ");

            $stmt->execute([$conferenceId]);

            $stats['tracks'] =
                (int)$stmt->fetchColumn();

            /*
            |--------------------------------------------------------------------------
            | ATTENDEES
            |--------------------------------------------------------------------------
            */

            $stmt = $db->prepare("
                SELECT COUNT(*)
                FROM attendance_registrations
                WHERE conference_id = ?
            ");

            $stmt->execute([$conferenceId]);

            $stats['attendees'] =
                (int)$stmt->fetchColumn();

            /*
            |--------------------------------------------------------------------------
            | ACCEPTANCE RATE
            |--------------------------------------------------------------------------
            */

            if (
                $stats['submissions'] > 0
            ) {

                $stats['acceptance_rate'] =
                    round(
                        (
                            $stats['accepted']
                            /
                            $stats['submissions']
                        ) * 100
                    );
            }

            /*
            |--------------------------------------------------------------------------
            | REVIEW COMPLETION
            |--------------------------------------------------------------------------
            */

            if (
                $stats['submissions'] > 0
            ) {

                $stats['review_completion'] =
                    round(
                        (
                            (
                                $stats['accepted']
                                +
                                $stats['under_review']
                            )
                            /
                            $stats['submissions']
                        ) * 100
                    );
            }

        } catch (Throwable $e) {

            error_log(
                'HomepageAnalyticsService ERROR: '
                . $e->getMessage()
            );
        }

        /*
        |--------------------------------------------------------------------------
        | KPI CARDS
        |--------------------------------------------------------------------------
        */

        $statsData = [

            [
                'icon' => 'fa-file-lines',
                'number' => $stats['submissions'],
                'label' => $lang === 'ar'
                    ? 'إجمالي الأبحاث'
                    : 'Submissions',
                'mini' => $lang === 'ar'
                    ? 'أبحاث مستلمة'
                    : 'Research Papers'
            ],

            [
                'icon' => 'fa-circle-check',
                'number' => $stats['accepted'],
                'label' => $lang === 'ar'
                    ? 'الأبحاث المقبولة'
                    : 'Accepted Papers',
                'mini' => $lang === 'ar'
                    ? 'بعد التحكيم'
                    : 'Peer Reviewed'
            ],

            [
                'icon' => 'fa-microphone',
                'number' => $stats['speakers'],
                'label' => $lang === 'ar'
                    ? 'المتحدثون'
                    : 'Keynote Speakers',
                'mini' => $lang === 'ar'
                    ? 'خبراء دوليون'
                    : 'International Experts'
            ],

            [
                'icon' => 'fa-diagram-project',
                'number' => $stats['tracks'],
                'label' => $lang === 'ar'
                    ? 'المسارات'
                    : 'Research Tracks',
                'mini' => $lang === 'ar'
                    ? 'محاور المؤتمر'
                    : 'Conference Themes'
            ],

            [
                'icon' => 'fa-users',
                'number' => $stats['attendees'],
                'label' => $lang === 'ar'
                    ? 'الحضور'
                    : 'Attendees',
                'mini' => $lang === 'ar'
                    ? 'مسجلون بالمؤتمر'
                    : 'Registered Participants'
            ]

        ];

        return [

            'statsData' => $statsData,

            'acceptanceRate' =>
                $stats['acceptance_rate'],

            'reviewCompletion' =>
                $stats['review_completion']

        ];
    }

public static function snapshot(
int $conferenceId
): array
{

$db=
Database::getInstance();

$out=[];

$queries=[

'sections'=>

"

SELECT COUNT(*)

FROM
conference_home_sections

WHERE conference_id=?

",

'blocks'=>

"

SELECT COUNT(*)

FROM
conference_homepage_blocks

WHERE conference_id=?

",

'releases'=>

"

SELECT COUNT(*)

FROM
conference_home_releases

WHERE conference_id=?

"

];

foreach(
$queries
as $key=>$sql
){

$stmt=
$db
->prepare(
$sql
);

$stmt
->execute([

$conferenceId

]);

$out[
$key
]=
(int)
$stmt
->fetchColumn();

}

$out[
'quality'
]=

min(
100,

40
+
(
$out[
'sections'
]
*2
)
+
(
$out[
'blocks'
]
)

);

return
$out;

}

}
