<?php
/**
 * Created by PhpStorm.
 * User: stana
 * Date: 25.02.2017
 * Time: 19:17
 */

namespace App\Http\Controllers;
use App\Accommodation;
use App\AccommodationEquipment;
use App\Address;
use App\Booked;
use App\Customer;
use App\Image;
use App\Reservation;
use App\Reviews;
use App\Room;
use App\User;
use Auth;

class DeleteAccountController
{
    public function index(){
        return view('delete_account');
    }

    public function deleteAccount(){
        $user = User::find(Auth::user()->user_id);

        if($user->role == 'admin'){
            $anotherAdmins = User::where('role','=','admin')->where('user_id','!=',$user->user_id)->first();

            if($anotherAdmins == null){
                flash('Nelze smazat tento účet, protože neexistuje jiný administrátor', 'danger');
                return redirect('delete_account');
            }
        }

        if($user->owner == 'true'){
            $accommodation_id = User::where('user_id', '=', Auth::user()->user_id)->pluck('accommodation_id')->first();
            $accommodation = Accommodation::find($accommodation_id);


            if($accommodation != null){
                $rooms = Room::where('accommodation_id','=',$accommodation->accommodation_id)->get();

                foreach ($rooms as $room) {
                    $booked = Booked::where('room_id','=',$room->room_id)->get();
                    // DELETE BOOKED
                    foreach ($booked as $item) {
                        $item->delete();
                    }

                    $reservations = Reservation::where('room_id','=',$room->room_id)->get();
                    if($reservations != null){
                        foreach ($reservations as $reservation) {
                            $customer = Customer::where('customer_id','=',$reservation->customer_id)->first();
                            $reservation->delete();
                            if($customer != null){
                                $customer->delete();
                            }
                        }
                    }

                    $image = Image::find($room->image_id);
                    if($image != null){
                        $image->delete();
                    }
                    $room->delete();
                }

                $address_id = Accommodation::where('accommodation_id', '=', $accommodation->accommodation_id)->first();
                $address = Address::find($address_id);
                $address->delete();

                $reviews = Reviews::where('accommodation_id','=',$accommodation->accommodation_id)->get();
                if($reviews != null){
                    foreach ($reviews as $review) {
                        $review->delete();
                    }
                }

                $image = Image::find($accommodation->image_id);
                if($image != null){
                    $image->delete();
                }

                AccommodationEquipment::where('accommodation_id', '=', $accommodation->accommodation_id)->delete();
                /*$accommodation_equipment = AccommodationEquipment::where('accommodation_id','=',$accommodation->accommodation_id)->get();
                if($accommodation_equipment != null){
                    foreach ($accommodation_equipment as $item) {
                        echo dd($item);
                        $item->delete();
                    }
                }*/

                $all_users_in_acco = User::where('accommodation_id','=',$accommodation->accommodation_id)->get();
                Auth::logout();
                foreach ($all_users_in_acco as $user){
                    $user->delete();
                }
                $accommodation->delete();
            }
        }else{
            Auth::logout();
            $user->delete();
        }

        flash('Účet byl úspěšně smazán','success');
        return redirect('home');
    }
}