<?php

namespace App\Reporting\StatsModels;

use App\Contracts\StatsModel;
use App\Facades\Settings;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Mtc\MercuryDataModels\Vehicle;

class VehicleCount implements StatsModel
{
    /**
     * @return string
     */
    public function getKey(): string
    {
        return 'vehicle-count';
    }

    /**
     * @param Carbon $date
     * @return int
     */
    public function getValue(Carbon $date): mixed
    {
        return Vehicle::query()
            ->active()
            ->when(
                $this->excludeSoldVehicles(),
                fn (Builder $query) => $query->where('is_sold', false)
            )
            ->count();
    }

    private function excludeSoldVehicles(): bool
    {
        return (bool) Settings::get('filter-results-exclude-sold-vehicles', false);
    }
}
