<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Art;
use App\Models\Book;
use App\Models\Branch;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
use App\Models\Law;
use App\Models\Part;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;

class ArtController extends Controller
{
    public function index(Request $request)
    {
        $query = Art::query();

        if ($request->filled('q')) {
            $q = $request->q;
            $query->where(function ($qry) use ($q) {
                $qry->where('title', 'like', "%{$q}%")->orWhere('text', 'like', "%{$q}%");
            });
        }

        $perPage = min(max((int) $request->input('per_page', 15), 10), 100);
        $arts = $query->paginate($perPage)->withQueryString();

        return view('admin.art.index', compact('arts'));
    }

    public function create()
    {
        $chapters = Chapter::all();
        $parts = Part::all();
        $gates = Gate::all();
        $books = Book::all();
        $volums = Volum::all();
        $laws = Law::all();
        $sections = Section::all();
        $divisions = Division::all();
        $branches  = Branch::all();

        return view('admin.art.create', compact('chapters', 'parts', 'gates', 'books', 'volums', 'laws', 'sections','branches','divisions'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required',
            'text' => 'required',
            'number' => 'required',
            'chapter_id' => 'nullable',
            'part_id' => 'nullable',
            'gate_id' => 'nullable',
            'section_id' => 'nullable',
            'book_id' => 'nullable',
            'volum_id' => 'nullable',
            'law_id' => 'required',
            'division_id' => 'nullable',
            'branch_id' => 'nullable',
            'is_free' => 'nullable'
        ]);

        $validated['is_free'] = $request->input('is_free') ? true : false;

        art::query()->create($validated);

        return redirect(route('art.index'));
    }

    public function edit(art $art)
    {
        $chapters = Chapter::all();
        $parts = Part::all();
        $gates = Gate::all();
        $books = Book::all();
        $volums = Volum::all();
        $laws = Law::all();
        $sections = Section::all();
        $divisions = Division::all();
        $branches  = Branch::all();


        return view('admin.art.update', compact('art', 'chapters', 'parts', 'gates', 'books', 'volums', 'laws', 'sections','branches','divisions'));
    }

    public function update(Request $request, Art $art)
    {

        $validated = $request->validate([
            'title' => 'required',
            'text' => 'required',
            'number' => 'required',
            'chapter_id' => 'nullable',
            'part_id' => 'nullable',
            'gate_id' => 'nullable',
            'section_id' => 'nullable',
            'book_id' => 'nullable',
            'volum_id' => 'nullable',
            'law_id' => 'required',
            'division_id' => 'nullable',
            'branch_id' => 'nullable',
            'is_free' => 'nullable'
        ]);

        $validated['is_free'] = $request->input('is_free') ? true : false;

        $art->update($validated);

        return redirect(route('art.edit',$art->id));
    }

    public function destroy(Art $art)
    {
        $art->delete();

        return redirect(route('art.index'));
    }
}
