<?php

namespace App\Services;

use App\Repositories\InspecteurRepository;
use App\Repositories\ProfesseurRepository;
use App\Repositories\EtablissementRepository;
use App\Repositories\InfrastructureRepository;
use App\Repositories\StructurePedagogiqueRepository;

class DashboardService
{
    protected $inspecteurRepository;
    protected $professeurRepository;
    protected $etablissementRepository;
    protected $infrastructureRepository;
    protected $structurePedagogiqueRepository;

    public function __construct(
        InspecteurRepository $inspecteurRepository,
        ProfesseurRepository $professeurRepository,
        EtablissementRepository $etablissementRepository,
        InfrastructureRepository $infrastructureRepository,
        StructurePedagogiqueRepository $structurePedagogiqueRepository
    ) {
        $this->inspecteurRepository = $inspecteurRepository;
        $this->professeurRepository = $professeurRepository;
        $this->etablissementRepository = $etablissementRepository;
        $this->infrastructureRepository = $infrastructureRepository;
        $this->structurePedagogiqueRepository = $structurePedagogiqueRepository;
    }

    public function getKPIs()
    {
        return [
            'inspecteurs' => $this->inspecteurRepository->count(),
            'professeurs' => $this->professeurRepository->count(),
            'etablissements' => $this->etablissementRepository->count(),
            'infrastructures' => $this->infrastructureRepository->count(),
            'structures_pedagogiques' => $this->structurePedagogiqueRepository->count(),
            'total_eleves' => $this->structurePedagogiqueRepository->getStatistics()['total_eleves'] ?? 0,
            'total_filles' => $this->structurePedagogiqueRepository->getStatistics()['total_filles'] ?? 0,
        ];
    }

    public function getStatisticsByWilaya()
    {
        $etablissementsByWilaya = $this->etablissementRepository->getStatistics()['by_wilaya'] ?? [];
        
        $inspecteursByWilaya = [];
        $professeursByWilaya = [];
        
        return [
            'etablissements' => $etablissementsByWilaya,
            'inspecteurs' => $inspecteursByWilaya,
            'professeurs' => $professeursByWilaya,
        ];
    }

    public function getStatisticsByPole()
    {
        return $this->inspecteurRepository->getStatistics()['by_pole'] ?? [];
    }

    public function getStatisticsByDiscipline()
    {
        return [
            'inspecteurs' => $this->inspecteurRepository->getStatistics()['by_discipline'] ?? [],
            'professeurs' => $this->professeurRepository->getStatistics()['by_discipline'] ?? [],
        ];
    }

    public function getImportHistory()
    {
        // This would typically query an import_history table
        return [
            'total_imports' => 0,
            'successful_imports' => 0,
            'failed_imports' => 0,
            'recent_imports' => [],
        ];
    }

    public function getChartData()
    {
        return [
            'by_wilaya' => $this->getStatisticsByWilaya(),
            'by_pole' => $this->getStatisticsByPole(),
            'by_discipline' => $this->getStatisticsByDiscipline(),
        ];
    }
}
