<?php

namespace App\Http\Controllers\ticket;

use App\Models\Ticket;
use App\Helpers\Helper;
use App\Models\Comment;
use App\Models\Notification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
    use Helper;

    public function __construct()
    {
        $this->model = new Comment();
    }
    public function index(Request $request){
        $comments = Comment::with('users')
            ->select('*', DB::raw('0 as open_reply'), DB::raw('"" as message'))
            ->where('ticket_id', $request->ticket_id)
            ->where('parent_id', null)
            ->orderBy('id', 'ASC')
            ->with('sub_comments')
            ->get();
            return returnData(2000, $comments);
    }


    public function create()
    {
        //
    }


    public function store(Request $request)
    {
        $ticket = Ticket::where('id', $request->ticket_id)->first();
        if ($ticket){
            $comment = new Comment();
            $comment->ticket_id = $request->ticket_id;
            $comment->user_id = auth()->user()->id;
            $comment->parent_id = $request->id ? $request->id : null;
            $comment->comment = $request->message;
            $comment->save();

            if ($comment->parent_id){
                $comment = Comment::where('id', $comment->parent_id)->first();
                Notification::insert([
                    'user_id' => auth()->user()->id,
                    'send_to' => $comment->user_id,
                    'ticket_id' => $comment->ticket_id,
                    'created_at' =>date('Y-m-d H:i:s'),
                    'title' => getData(auth()->user()->id, 'name').' Replied to your Comment',
                    'notification' => getData($comment->ticket_id, 'title', 'tickets'),
                    'link' => '/admin/ticket/' . $comment->ticket_id,
                ]);
            }else{
                if (auth()->user()->id != $ticket->user_id){
                    Notification::insert([
                        'user_id' => auth()->user()->id,
                        'send_to' => $ticket->user_id,
                        'ticket_id' => $comment->ticket_id,
                        'created_at' =>date('Y-m-d H:i:s'),
                        'title' => getData(auth()->user()->id, 'name').' commented your ticket',
                        'notification' => getData($comment->ticket_id, 'title', 'tickets'),
                        'link' => '/admin/ticket/' . $comment->ticket_id,
                    ]);
                }
            }

            return returnData(2000, $comment, 'Comment Added');
        }
        return returnData(5000, null, 'Ticket not fund');

    }



    public function show($id)
    {
        //
    }

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

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

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