<?php

namespace App\Http\Controllers;

use App\Models\Message;
use App\Models\MessageComment;
use App\Http\Requests\StoremessageRequest;
use App\Http\Requests\UpdatemessageRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoremessageRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoremessageRequest $request)
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoremessageRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function storeComment(StoremessageRequest $request)
    {
        //
        $message_comment = MessageComment::Create([
            'content' => $request->content,
            'author_id' => $request->User()->id,
            'message_id' => $request->message_id,
        ]);
        return $message_comment;
        
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\message  $message
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, message $message)
    {
        //
        $message = Message::where('id', '=', $request->id)->get()[0];
        $comment = MessageComment::where('message_id', '=', $request->id)->get();
        $response = [
            'blog' => $message,
            'comments' => $comment,
        ];

        return response($response, 201);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\message  $message
     * @return \Illuminate\Http\Response
     */
    public function edit(message $message)
    {
        //
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\message  $message
     * @return \Illuminate\Http\Response
     */
    public function destroy(message $message)
    {
        //
    }

    public function count()
    {
        $count = DB::select('SELECT [messages].[id],Count([message_comments].[id]) AS count FROM [laravel].[dbo].[messages]
        LEFT JOIN [message_comments] ON [message_comments].[message_id] = [messages].[id]
        GROUP BY [messages].[id]');
        return $count;
    }
    public function recent()
    {
        //
        $message = Message::all()->sortByDesc('created_at')->take(4);
        $categories = [
            ['category' => '台灣小吃', 'icon' => 'WatchIcon'],
            ['category' => '新北美食', 'icon' => 'ShoppingCartIcon'],
            ['category' => '熱炒店', 'icon' => 'CommandIcon'],
            ['category' => '吃到飽', 'icon' => 'HashIcon'],
            ['category' => '童坑', 'icon' => 'AlertOctagonIcon']
        ];
        $response = [
            'recentPosts' => $message,
            'categories' => $categories,
        ];
        return $response;
    }
}
