<?php

namespace App\Services;

use App\Models\Pet;
use Illuminate\Support\Facades\Cache;

class PetStatisticsService
{
    public function getAvailablePetsCount(): int
    {
        $count = Cache::remember(
            'stats.available_pets_count',
            now()->addDays(7),
            fn () => Pet::where('status', 'available')->count()
        );

        return $this->roundDownToTen($count);
    }

    public function getAdoptedPetsCount(): int
    {
        $count = Cache::remember(
            'stats.adopted_pets_count',
            now()->addDays(7),
            fn () => Pet::where('status', 'adopted')->count()
        );

        return $this->roundDownToTen($count);
    }

    public function invalidateCache(): void
    {
        Cache::forget('stats.available_pets_count');
        Cache::forget('stats.adopted_pets_count');
    }

    protected function roundDownToTen(int $count): int
    {
        if ($count < 10) {
            return $count;
        }

        return (int) floor($count / 10) * 10;
    }
}
