<?php

namespace App\Services;

use App\Core\Database;

class HomepagePortfolioService
{

public static function dashboard(
int $orgId
): array
{

$db=
Database::getInstance();

$sql="

SELECT

c.id,

COALESCE(
c.title_en,
c.title_ar,
CONCAT('Conference ',c.id)
)
title,

(

SELECT COUNT(*)

FROM conference_homepage_blocks b

WHERE b.conference_id=c.id

)
blocks,

(

SELECT COUNT(*)

FROM conference_home_deployments d

WHERE d.conference_id=c.id

)
deployments,

(

SELECT COUNT(*)

FROM conference_home_branches br

WHERE br.conference_id=c.id

)
branches

FROM conferences c

WHERE c.org_id=?

ORDER BY c.id DESC

LIMIT 50

";

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

$stmt
->execute([

$orgId

]);

$rows=
$stmt
->fetchAll();

foreach(
$rows
as &$r
){

$r[
'health'
]=

min(

100,

40+

$r[
'blocks'
]

+

(
$r[
'deployments'
]*5
)

+

(
$r[
'branches'
]*3
)

);

}

return [

'count'=>
count(
$rows
),

'rows'=>
$rows

];

}

}
