<?php

namespace App\Http\Controllers\Dashboard\Measurement;

use App\Http\Controllers\Controller as ExController;
use Illuminate\Http\Request;
use App\Models\Measurement;

class MeasurementController extends ExController
{
    public function index(Request $request)
    {
        // $this->authorize('view', 'measurements');

        $measurements = Measurement::latest('id')->paginate(30);

        return view('dashboard.measurements.index', compact('measurements'));
    }

    public function create()
    {
        // $this->authorize('create', 'measurements');

        return view('dashboard.measurements.create');
    }

    public function store(Request $request)
    {
        // $this->authorize('create', 'measurements');

        $this->validate($request, [
            'name_uz' => 'required|string|max:255',
            'name_ru' => 'required|string|max:255',
        ]);

        Measurement::create([
            'name' => [
                'uz' => $request->name_uz,
                'ru' => $request->name_ru,
            ],
        ]);

        $this->info(trans('admin.messages.created'));
        return redirect()->route('dashboard.measurements.index');
    }

    public function edit(Measurement $measurement)
    {
        // $this->authorize('update', 'measurements');

        return view('dashboard.measurements.edit', compact('measurement'));
    }

    public function update(Request $request, Measurement $measurement)
    {
        // $this->authorize('update', 'measurements');

        $this->validate($request, [
            'name_uz' => 'required|string|max:255',
            'name_ru' => 'required|string|max:255',
        ]);

        $measurement->update([
            'name' => [
                'uz' => $request->name_uz,
                'ru' => $request->name_ru,
            ],
        ]);

        $this->info(trans('admin.messages.updated'));
        return redirect()->route('dashboard.measurements.index');
    }

    public function destroy(Measurement $measurement)
    {
        // $this->authorize('delete', 'measurements');

        $measurement->delete();

        $this->info(trans('admin.messages.deleted'));
        return redirect()->back();
    }
}
