<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Conversations;
use App\Models\Parents;
use App\Models\ConversationGroups;
use DB;
use Log;

class ConversationsController extends Controller {

    public function create( Request $request ) {
        try {
                DB::beginTransaction();
               
                if (  $request->conversation_group_id == null ) {
                    $conversation_groups = new ConversationGroups();
                    $conversation_groups->sender_id   = $request->sender_id;
                    $conversation_groups->receiver_id = $request->receiver_id;
                    $conversation_groups->is_archive     = 0;
                    $conversation_groups->save();

                    $conversation_groups_id = $conversation_groups->id;
                }

                $conversations = new Conversations();
                $conversations->conversation_group_id = $request->conversation_group_id == null ? $conversation_groups->id : $request->conversation_group_id;
                $conversations->sender_id             = $request->sender_id;
                $conversations->receiver_id           = $request->receiver_id;
                $conversations->message               = $request->message;
                $conversations->is_read               = 0;
                $conversations->save();

                DB::commit();
                return response()->json([ 'conversations' => $conversations, 'message' => 'saved', 'status'=>200 ], 200 );
        } catch (\Exception $e) {
                DB::rollBack();
                Log::error($e);
            return response()->json([ 'message' => $e->getmessage(), 'status'=>500 ], 500);
        }
    }

    public function getChatById( $id ) {
        try {
            $conversations = Conversations::with('sender')->Where( 'conversation_group_id', $id )->get();
            return response()->json([ 'conversations' => $conversations], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
    }


    public function feedbacks(  $id ) {
        try {
            $conversations = DB::table('conversations')
            ->select(DB::raw('count(id) as count, sentimental as type'))
            ->groupBy('sentimental')
            ->Where('conversation_group_id',$id)
            ->get();

            return response()->json([ 'results' => $conversations], 200);
        } catch ( \Exception $e ) {
            Log::error( $e );
            return response()->json( ['message' => $e->getmessage()], 500 );
        }
    }



    public function item_create( Request $request ) {
        try {
                DB::beginTransaction();

                $item = new Item();
                $item->name = $request->name;
                $item->type = $request->type;
                $item->qty  = $request->qty;
             
                $item->save();

                DB::commit();
                // return response()->json([ 'conversations' => $conversations, 'message' => 'saved', 'status'=>200 ], 200 );
        } catch (\Exception $e) {
                DB::rollBack();
                Log::error($e);
            // return response()->json([ 'message' => $e->getmessage(), 'status'=>500 ], 500);
        }
    }
}
