<?php

namespace App\Http\Controllers\Dashboard\Power;

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

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

        $powers = Power::latest('id')->paginate(30);

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

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

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

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

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

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

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

    public function edit(Power $power)
    {
        // $this->authorize('update', 'powers');

        return view('dashboard.powers.edit', compact('power'));
    }

    public function update(Request $request, Power $power)
    {
        // $this->authorize('update', 'powers');

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

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

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

    public function destroy(Power $power)
    {
        // $this->authorize('delete', 'powers');

        $power->delete();

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