<?php
/**
 * Created by PhpStorm.
 * User: stana
 * Date: 12.03.2017
 * Time: 13:06
 */

namespace App\Http\Controllers;


use App\Accommodation;
use App\Reviews;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;

class ReviewsController
{
    public function index($accommodation_id)
    {
        $reviews = DB::table('reviews')
             ->join('customers', 'reviews.customer_id', '=', 'customers.customer_id')
             ->select('customers.customer_id','customer_lname','customer_fname','review_text', 'review_id', 'date')
             ->where('reviews.accommodation_id', '=', $accommodation_id)
             ->orderBy('date')
             ->get();

        if($reviews != null){
            foreach ($reviews as $review) {
                $review->date = new \DateTime($review->date);
                $review->date = $review->date->format('d. m. Y');
            }
        }

        $accommodation = Accommodation::where('accommodation_id','=',$accommodation_id)->first();
        return view('reviews_list', ['reviews' => $reviews, 'accommodation' => $accommodation]);
    }

    public function showCreateReview($accommodation_id)
    {
        $accommodation = Accommodation::find($accommodation_id);
        return view('create_review', ['accommodation' => $accommodation]);
    }

    public function createReview(Request $request)
    {
        // REWRITE MESSAGES
        $messages = [
            'email.required' => 'Vyplňte e-mail',
            'review.required' => 'Vyplňte recenzi',
            'review.max' => 'Příliš dlouhé, max 255 znaků',
        ];

        // VALIDATE USER INPUT
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'review' => 'required|max:255',
        ], $messages);

        $customer_id = DB::table('accommodations')
            ->join('rooms', 'rooms.accommodation_id', '=', 'accommodations.accommodation_id')
            ->join('reservations', 'reservations.room_id', '=', 'rooms.room_id')
            ->join('customers', 'reservations.customer_id', '=', 'customers.customer_id')
            ->select('customers.customer_id')
            ->where('customers.customer_email', '=', $request->email)
            ->where('accommodations.accommodation_id', '=', $request->accommodation_id)
            ->first();

        if ($validator->fails()) {
            return redirect('create_review/' . $request->accommodation_id)->withErrors($validator)->withInput();
        } elseif ($customer_id == null) {
            flash('Na zadaný email nebyla pro dané ubytování vytvořena žádná rezervace', 'danger');
            return redirect('create_review/' . $request->accommodation_id)->withErrors($validator)->withInput();
        }
        $review = new Reviews([
            'review_text' => $request->review,
            'rating' => $request->rating,
            'accommodation_id' => $request->accommodation_id,
            'customer_id' => $customer_id->customer_id
        ]);
        $review->save();

        $count = Reviews::where('accommodation_id','=',$request->accommodation_id)->count();
        $sum = Reviews::where('accommodation_id','=',$request->accommodation_id)->sum('rating');

        $accommodation = Accommodation::find($request->accommodation_id);

        if($count != 0){
            $accommodation->rating = $sum / $count;
        }else{
            $accommodation->rating = $request->rating;
        }
        $accommodation->save();

        return ReviewsController::index($request->accommodation_id);
    }

    public function deleteReview($request){
        $review = Reviews::find($request);
        $accommodation = Accommodation::find($review->accommodation_id);

        $review->delete();

        $count = Reviews::where('accommodation_id','=',$request)->count();
        $sum = Reviews::where('accommodation_id','=',$request)->sum('rating');


        if($count != 0){
            $accommodation->rating = $sum / $count;
        }else{
            $accommodation->rating = '0';
        }

        $accommodation->save();

        flash('Recenzebyla úspěšně odstraněna','success');
        return redirect('reviews_list/'.$accommodation->accommodation_id);
    }
}