<?php
declare(strict_types=1);
namespace App\Admin;

use App\Settings\SettingsRepository;
use PDO;

final class AdminDashboardService
{
    public function __construct(private readonly PDO $pdo, private readonly SettingsRepository $settings) {}
    /** @return array{attention:array<string,int>,health:list<array{label:string,status:string,detail:string}>} */
    public function snapshot(): array
    {
        $count=fn(string $sql):int=>$this->safeCount($sql);
        $attention=[
            'pending_content'=>$count("SELECT (SELECT COUNT(*) FROM questions WHERE status='pending')+(SELECT COUNT(*) FROM topics WHERE status='pending')+(SELECT COUNT(*) FROM answers WHERE status='pending')+(SELECT COUNT(*) FROM comments WHERE status='pending')"),
            'open_reports'=>$count("SELECT COUNT(*) FROM reports WHERE status='open'"),
            'unanswered'=>$count("SELECT COUNT(*) FROM questions WHERE status='published' AND answers_count=0"),
            'new_members'=>$count("SELECT COUNT(*) FROM users WHERE account_type='member' AND created_at>=DATE_SUB(NOW(),INTERVAL 7 DAY)"),
        ];
        $drivers=PDO::getAvailableDrivers();
        $root=dirname(__DIR__,2);$health=[
            ['label'=>'Database','status'=>'pass','detail'=>'PDO connection active.'],
            ['label'=>'PDO MySQL','status'=>in_array('mysql',$drivers,true)?'pass':'fail','detail'=>in_array('mysql',$drivers,true)?'mysql driver available.':'mysql driver unavailable.'],
            ['label'=>'OpenSSL','status'=>extension_loaded('openssl')?'pass':'fail','detail'=>extension_loaded('openssl')?'Extension available.':'Extension unavailable.'],
            ['label'=>'cURL','status'=>function_exists('curl_init')?'pass':'warn','detail'=>function_exists('curl_init')?'Extension available.':'Required for Telegram/Turnstile network calls.'],
            ['label'=>'Logs','status'=>is_writable($root.'/storage/logs')?'pass':'fail','detail'=>'storage/logs '.(is_writable($root.'/storage/logs')?'writable.':'not writable.')],
            ['label'=>'Sessions','status'=>is_writable($root.'/storage/sessions')?'pass':'fail','detail'=>'storage/sessions '.(is_writable($root.'/storage/sessions')?'writable.':'not writable.')],
            ['label'=>'SMTP','status'=>(bool)$this->settings->get('mail.enabled',false)?'pass':'info','detail'=>(bool)$this->settings->get('mail.enabled',false)?'Mail delivery enabled.':'Mail delivery disabled.'],
            ['label'=>'Telegram','status'=>(bool)$this->settings->get('notifications.telegram_enabled',false)?'pass':'info','detail'=>(bool)$this->settings->get('notifications.telegram_enabled',false)?'Telegram notifications enabled.':'Telegram notifications disabled.'],
            ['label'=>'Sitemap','status'=>(bool)$this->settings->get('seo.sitemap_enabled',true)?'pass':'warn','detail'=>(bool)$this->settings->get('seo.sitemap_enabled',true)?'Sitemap enabled.':'Sitemap disabled.'],
        ];
        return ['attention'=>$attention,'health'=>$health];
    }
    private function safeCount(string $sql):int{try{return (int)$this->pdo->query($sql)->fetchColumn();}catch(\Throwable){return 0;}}
}
