<?php

namespace App\Services;

use App\Repositories\TizinRepository;
use App\Repositories\SisreqdataRepository;
use App\Repositories\DaftarexculRepository;
use App\Repositories\StopexculRepository;
use Illuminate\Support\Facades\Cache;

class DashboardService
{
    protected TizinRepository $tizinRepo;
    protected SisreqdataRepository $sisreqdataRepo;
    protected DaftarexculRepository $daftarexculRepo;
    protected StopexculRepository $stopexculRepo;

    public function __construct(
        TizinRepository $tizinRepo,
        SisreqdataRepository $sisreqdataRepo,
        DaftarexculRepository $daftarexculRepo,
        StopexculRepository $stopexculRepo
    ) {
        $this->tizinRepo = $tizinRepo;
        $this->sisreqdataRepo = $sisreqdataRepo;
        $this->daftarexculRepo = $daftarexculRepo;
        $this->stopexculRepo = $stopexculRepo;
    }

    public function getDashboardData()
    {
        return [
            'tizin_pending' => $this->getTizinPendingData(),
            'sisreqdata_status0' => $this->getSisreqdataStatus0Data(),
            'daftarexcul_status0' => $this->getDaftarexculStatus0Data(),
            'stopexcul_status0' => $this->getStopexculStatus0Data(),
        ];
    }

    private function getTizinPendingData()
    {
        $count = Cache::remember('dashboard.tizin.pending.count', 300, fn() => 
            $this->tizinRepo->getPendingCount()
        );

        return [
            'count' => $count,
            'title' => 'Izin Menunggu',
            'icon' => '⏳',
            'color' => 'yellow',
        ];
    }

    private function getSisreqdataStatus0Data()
    {
        $count = Cache::remember('dashboard.sisreqdata.status0.count', 300, fn() => 
            $this->sisreqdataRepo->getStatus0Count()
        );

        return [
            'count' => $count,
            'title' => 'Data Permohonan Menunggu',
            'icon' => '📋',
            'color' => 'blue',
        ];
    }

    private function getDaftarexculStatus0Data()
    {
        $count = Cache::remember('dashboard.daftarexcul.status0.count', 300, fn() => 
            $this->daftarexculRepo->getStatus0Count()
        );

        return [
            'count' => $count,
            'title' => 'Daftar Excul Menunggu',
            'icon' => '📝',
            'color' => 'purple',
        ];
    }

    private function getStopexculStatus0Data()
    {
        $count = Cache::remember('dashboard.stopexcul.status0.count', 300, fn() => 
            $this->stopexculRepo->getStatus0Count()
        );

        return [
            'count' => $count,
            'title' => 'Stop Excul Menunggu',
            'icon' => '🛑',
            'color' => 'red',
        ];
    }
}
