<?php

namespace App\Metrics;

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

class VehiclesSold
{
    public function get(Period $period)
    {
        return Vehicle::query()
            ->withTrashed()
            ->select(DB::raw('count(*) as count'), DB::raw('DATE(sold_at) as date'))
            ->where('sold_at', '>=', $period->startDate->format('Y-m-d'))
            ->where('sold_at', '<=', $period->endDate->format('Y-m-d 23:59:59'))
            ->groupBy('date')
            ->get()
            ->pluck('count', 'date');
    }
}
