<?php

namespace App\Http\Controllers\Dashboard\Problem;

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

class ProblemController extends ExController
{
    public function index(Request $request)
    {
        $problems = Problem::latest('id')->paginate(30);

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

    public function create()
    {
        return view('dashboard.problems.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'title_uz' => 'required|string|max:255',
            'title_ru' => 'required|string|max:255',
        ]);

        Problem::create([
            'title' => [
                'uz' => $request->title_uz,
                'ru' => $request->title_ru,
            ],
        ]);

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

    public function edit(Problem $problem)
    {
        return view('dashboard.problems.edit', compact('problem'));
    }

    public function update(Request $request, Problem $problem)
    {
        $this->validate($request, [
            'title_uz' => 'required|string|max:255',
            'title_ru' => 'required|string|max:255',
        ]);

        $problem->update([
            'title' => [
                'uz' => $request->title_uz,
                'ru' => $request->title_ru,
            ],
        ]);

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

    public function destroy(Problem $problem)
    {
        $problem->delete();

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