<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return Blog::all();
    }

    public function index_page(Request $request)
    {
        return Blog::offset($request->offset)->limit($request->rows)->get();
    }

    public function sidebar(Request $request)
    {
        return [
            // 'recentPosts' => Blog::whereIn('id', $request->rankingList)->get(),
            'recentPosts' => Blog::limit(4)->get(),
            'categories' => [
                ['category' => 'Fashion', 'icon' => 'WatchIcon'],
                ['category' => 'Food', 'icon' => 'ShoppingCartIcon'],
                ['category' => 'Gaming', 'icon' => 'CommandIcon'],
                ['category' => 'Quote', 'icon' => 'HashIcon'],
                ['category' => 'Video', 'icon' => 'VideoIcon'],
            ]
        ];
    }

    public function total_count()
    {
        $list = Blog::all();
        return $list->count();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Blog::insert([
            'title' => $request->title,
            'content' => $request->content,
            'tags' => $request->tags,
            'authorId' => $request->userId,
            'bookmarked' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $blog = Blog::where('id', '=', $request->blogId)->first();
        $blog->tags = json_decode($blog->tags);
        return $blog;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        $blog = Blog::where('id', $request->blogId)->first();

        if ($blog->authorId == $request->user()->id) {
            $blog->update([
                'title' => $request->title,
                'content' => $request->content,
                'tags' => $request->tags,
                'updated_at' => now(),
            ]);
            return "success";
        }
        return "fail";
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Blog $blog)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        Blog::where('id', '=', $request->blogId)->delete();
    }
}
