<?php

namespace App\Metrics;

use App\Support\Period;
use Illuminate\Support\Facades\DB;
use Mtc\MercuryDataModels\ResourceView;

class VehicleViews
{
    public function get(Period $period)
    {
        return ResourceView::query()
            ->where('viewable_type', 'vehicle')
            ->select(DB::raw('SUM(hits) as count'), DB::raw('DATE(`date`) as view_date'))
            ->where('date', '>=', $period->startDate->format('Y-m-d'))
            ->where('date', '<=', $period->endDate->format('Y-m-d'))
            ->groupBy('view_date')
            ->get()
            ->pluck('count', 'view_date')
            ->map(fn ($count) => (int) $count);
    }
}
