<?php
declare(strict_types=1);

require_once __DIR__ . '/../core/DbHelper.php';

class DashboardService
{
    private PDO $db;

    private const STATUS_ACTIVE  = 'Active';
    private const STATUS_PENDING = 'Pending';

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    public function getDashboardData(): array
    {
        $today      = date('Y-m-d');
        $monthStart = date('Y-m-01');

        return [
            'employees' => $this->employees(),
            'dealers'   => $this->dealers(),
            'products'  => $this->products(),
            'content'   => $this->content(),
            'leads'     => $this->leads($today, $monthStart),
        ];
    }

    private function employees(): array
    {
        return [
            'total'  => DbHelper::count($this->db, 'SELECT COUNT(*) FROM employees'),
            'active' => DbHelper::count(
                $this->db,
                'SELECT COUNT(*) FROM employees WHERE status = :status',
                ['status' => self::STATUS_ACTIVE]
            ),
        ];
    }

    private function dealers(): array
    {
        return [
            'total'   => DbHelper::count($this->db, 'SELECT COUNT(*) FROM dealers'),
            'active'  => DbHelper::count(
                $this->db,
                'SELECT COUNT(*) FROM dealers WHERE status = :status',
                ['status' => self::STATUS_ACTIVE]
            ),
            'pending' => DbHelper::count(
                $this->db,
                'SELECT COUNT(*) FROM dealers WHERE approval_status = :status',
                ['status' => self::STATUS_PENDING]
            ),
        ];
    }

    private function products(): array
    {
        $data = [];

        if (DbHelper::tableExists($this->db, 'retail_products')) {
            $data['retail'] = [
                'total'   => DbHelper::count($this->db, 'SELECT COUNT(*) FROM retail_products'),
                'active'  => DbHelper::count(
                    $this->db,
                    'SELECT COUNT(*) FROM retail_products WHERE status = :status',
                    ['status' => self::STATUS_ACTIVE]
                ),
                'pending' => DbHelper::count(
                    $this->db,
                    'SELECT COUNT(*) FROM retail_products WHERE approval_status = :status',
                    ['status' => self::STATUS_PENDING]
                ),
            ];
        }

        if (DbHelper::tableExists($this->db, 'hospitality_products')) {
            $data['hospitality'] = [
                'total'   => DbHelper::count($this->db, 'SELECT COUNT(*) FROM hospitality_products'),
                'active'  => DbHelper::count(
                    $this->db,
                    'SELECT COUNT(*) FROM hospitality_products WHERE status = :status',
                    ['status' => self::STATUS_ACTIVE]
                ),
                'pending' => DbHelper::count(
                    $this->db,
                    'SELECT COUNT(*) FROM hospitality_products WHERE approval_status = :status',
                    ['status' => self::STATUS_PENDING]
                ),
            ];
        }

        return $data;
    }

    private function content(): array
    {
        return [
            'blogs' => $this->simpleContent('blogs'),
            'faqs'  => $this->simpleContent('faqs'),
            'pages' => $this->simpleContent('pages'),
        ];
    }

    private function simpleContent(string $table): array
    {
        return [
            'total'   => DbHelper::count($this->db, "SELECT COUNT(*) FROM {$table}"),
            'active'  => DbHelper::count(
                $this->db,
                "SELECT COUNT(*) FROM {$table} WHERE status = :status",
                ['status' => self::STATUS_ACTIVE]
            ),
            'pending' => DbHelper::count(
                $this->db,
                "SELECT COUNT(*) FROM {$table} WHERE approval_status = :status",
                ['status' => self::STATUS_PENDING]
            ),
        ];
    }

    private function leads(string $today, string $monthStart): array
    {
        return [
            'today' => DbHelper::count(
                $this->db,
                'SELECT COUNT(*) FROM leads WHERE DATE(created_at) = :today',
                ['today' => $today]
            ),
            'this_month' => DbHelper::count(
                $this->db,
                'SELECT COUNT(*) FROM leads WHERE created_at >= :month_start',
                ['month_start' => $monthStart]
            ),
            'total' => DbHelper::count($this->db, 'SELECT COUNT(*) FROM leads'),
        ];
    }
}
