<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Blog;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

     function __construct()
     {
         $this->middleware('role_or_permission:blog access|blog create|blog edit|blog delete', ['only' => ['index']]);
         $this->middleware('role_or_permission:blog create', ['only' => ['create', 'store']]);
         $this->middleware('role_or_permission:blog edit', ['only' => ['edit', 'update']]);
         $this->middleware('role_or_permission:blog delete', ['only' => ['delete']]);
     }

    public function index()
    {
        $datas = Blog::with('category')->get();
        return view('admin.blogs.content.index', compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::all();

        return view('admin.blogs.content.create', compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'image' => ['required', 'image'],
            'category_id' => ['required', 'exists:categories,id'],
            'sort_description' => ['required'],
            'long_description' => ['required']
        ]);

        Blog::create([
            'title' => $request->title,
            'image' => fileupload($request->file('image'), 'homepage-files'),
            'category_id' => $request->category_id,
            'sort_description' => $request->sort_description,
            'long_description' => $request->long_description
        ]);
        $notification = array(
            'messege' => 'Created successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Blog::query()->findOrFail($id);
        $categories = Category::all();

        return view('admin.blogs.content.edit', compact('data', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'title' => 'required',
            'image' => ['nullable', 'image'],
            'category_id' => ['required', 'exists:categories,id'],
            'sort_description' => ['required'],
            'long_description' => ['required']
        ]);


        $blog = Blog::find($id);
        $blog->title = $request->title;
        if ($request->hasFile('image')) {

            if (File::exists($blog->image)) {
                File::delete($blog->image);
            }

            $blog->image =  fileupload($request->file('image'), 'homepage-files');
        }
        $blog->category_id = $request->category_id;
        $blog->sort_description = $request->sort_description;
        $blog->long_description = $request->long_description;
        $blog->save();


        $notification = array(
            'messege' => 'Updated successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $blog = Blog::find($id);
        if (File::exists($blog->image)) {
            File::delete($blog->image);
        }
        $blog->delete();

        $notification = array(
            'messege' => 'Deleted  successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }
}
