<?php

namespace App\Http\Controllers;

use App\Models\Reviews;
use App\Http\Controllers\CommonController;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class ReviewsController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
        $this->common_class_obj = new CommonController();
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $reviews = Reviews::where('status', '>', 0)->orderBy('created_at', 'DESC')->get();
        return view('reviews.index', compact('reviews'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $reviews = Reviews::where('status', '=', 1)->orderBy('id', 'DESC')->get();
        // if(count($reviews) == 2){
        //     return redirect()->back()->with('error', 'You cant add more than 2 reviews');
        // }
        return view('reviews.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $validatedata = $request->validate([
            'name' => 'required|max:30|min:2',
            'review' => 'required|min:2',
            'rating' => "required|numeric|between:1,5"
            
        ]);

        if($request->name == null || empty($request->name) ){
            return redirect()->route('reviews.index')->with('error', 'Name  should not be empty');
        }

        if($request->review == null || empty($request->review) ){
            return redirect()->route('reviews.index')->with('error', 'Review  should not be empty');
        }

        if($request->rating == null || empty($request->rating) ){
            return redirect()->route('reviews.index')->with('error', 'Rating  should not be empty');
        }

        $reviews = new Reviews();
        $reviews->name = $request->name;
        $reviews->review = $request->review;
        $reviews->rating = $request->rating;
        $reviews->date = $request->date;

        $reviews->created_by = Auth::user()->id;
        $reviews->updated_by = Auth::user()->id;

        $reviews->save();

        if ($reviews) {
            return redirect()->route('reviews.index')->with('success', 'Review Has Been Added Successfully');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Reviews  $reviews
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $data = Reviews::findorFail($id);
        if($data->status == 0){
            return redirect()->route('reviews.index')->with('error', 'No Data Found');
        }
        
        return view('reviews.details', compact('data','id'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Reviews  $reviews
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $review = Reviews::findorFail($id);
        return view('reviews.edit', compact('review','id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Reviews  $reviews
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
        $validatedata = $request->validate([
            'name' => 'required|max:30|min:2',
            'review' => 'required|min:2',
            'rating' => "required|numeric|between:1,5"
            
        ]);

        $data = Reviews::findorFail($id);

        if($request->name == null || empty($request->name) ){
            return redirect()->route('reviews.index')->with('error', 'Name  should not be empty');
        }

        if($request->review == null || empty($request->review) ){
            return redirect()->route('reviews.index')->with('error', 'Review  should not be empty');
        }

        if($request->rating == null || empty($request->rating) ){
            return redirect()->route('reviews.index')->with('error', 'Rating  should not be empty');
        }

        $data->name = $request->name;
        $data->review = $request->review;
        $data->rating = $request->rating;
        $data->date = $request->date;

        $data->updated_by = Auth::user()->id;


        $data->save();

        if ($data) {
            return redirect()->route('reviews.index')->with('success', 'Review Has Been Updated Successfully');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Reviews  $reviews
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $review = Reviews::findorFail($id);

        $review->status = 0;
        $review->updated_by = Auth::user()->id;
        $review->save();

        return redirect()->route('reviews.index')->with('success', 'Review Has Been deleted Successfully');
    }

    // public function accept($id)
    // {
    //     //

    //     $data = Reviews::findorFail($id);

    //     $data->status = 1;
    //     $data->updated_by = Auth::user()->id;
    //     $data->save();

    //     return redirect()->route('reviews.index')->with('success', 'Review has been accepted');
    // }


    // public function reject($id)
    // {
    //     $data = Reviews::findorFail($id);

    //     $data->status = 2;
    //     $data->updated_by = Auth::user()->id;
    //     $data->save();

    //     return redirect()->route('reviews.index')->with('success', 'Review has been rejected');
    // }
}
