<?php

namespace App\Http\Controllers\V3;

use App\Facades\DataTable\DataTableFacade;
use App\Http\Controllers\Controller;
use App\Http\Traits\ApiResponse;
use App\Models\CMMSMachine;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CMMSMachinesController extends Controller
{
    use ApiResponse;

    public function index(Request $request): JsonResponse
    {
        return response()->json(DataTableFacade::run(
            CMMSMachine::query(),
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedSelects: ['machine_code', 'id', 'plak_number', 'car_name']
        ));
    }

    public function list(): JsonResponse
    {
        $machinesList = CMMSMachine::query()->get(['machine_code', 'id', 'plak_number', 'car_name']);

        return $this->successResponse($machinesList);
    }

    public function search(Request $request): JsonResponse
    {
        $matchedSearchedMachines = CMMSMachine::query()
            ->where('machine_code', 'LIKE', "%{$request->machine_code}%")
            ->get(['machine_code', 'id', 'plak_number', 'car_name']);

        return $this->successResponse($matchedSearchedMachines);
    }
}
