<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Mtc\MercuryDataModels\CapIdCache;

class CapIdCacheController extends Controller
{
    public function index()
    {
        return CapIdCache::query()
            ->with(['make:id,name', 'model:id,name'])
            ->latest()
            ->paginate()
            ->through(function ($entry) {
                $entry->make_name = $entry->make?->name;
                $entry->model_name = $entry->model?->name;
                unset($entry->make, $entry->model);
                return $entry;
            });
    }

    public function destroy(CapIdCache $capIdCache): Response
    {
        $capIdCache->delete();
        return response('ok');
    }

    public function clear(): Response
    {
        CapIdCache::query()->truncate();
        return response('ok');
    }
}
