<?php

namespace App\Http\Controllers;

use App\DataTables\RoomDataTable;
use App\Models\Room;
use App\Models\RoomType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoomController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(RoomDataTable $dataTable)
    {
         if(auth()->user()->can('show_rooms')){

        return $dataTable->render('rooms.index');
         }
         else{
                 return abort(404);

         }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $room_type = RoomType::findOrFail($request->type_id);

        $data = $this->validate($request, [
            'ship_id' => 'required|numeric',
            'room_number' => 'nullable|numeric',
            'type_id' => 'required|numeric',
        ]);

        if ($request->input_type) {
            $this->validate($request, [
                'room_from' => 'required|numeric',
                'room_to' => 'required|numeric',
            ]);
            $room_from = $request->room_from;
            $room_to = $request->room_to;
        } else {
            $room_from = $room_to = $request->room_number;
        }


        for ($i = $room_from; $i <= $room_to; $i++) {
            $request->merge([
                'code' => $room_type->code .'-'. $i,
            ]);
            $this->validate($request, [
                'code' => 'unique:rooms,code',
            ]);
            $data['room_number'] = $i;
            $data['code'] = $request->code;
            $room = Room::create($data);
        }

        return true;
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Room  $room
     * @return \Illuminate\Http\Response
     */
    public function show(Room $room)
    {
        $room->input_type='0';
        return $room;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Room  $room
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Room $room)
    {
        $room_type = RoomType::findOrFail($room->type->id);

        $request->merge([
            'code' => $room_type->code.'-'.$request->room_number
        ]);

        $data = $this->validate($request, [
            'room_number' => 'required|numeric',
            'code' => 'unique:rooms,code,' . $room->id . ',id',
            'status' => 'required|boolean'
        ]);

        unset($data['name']);

        return $room->update($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Room  $room
     * @return \Illuminate\Http\Response
     */
    public function destroy(Room $room)
    {
        return $room->delete();
    }

    public function specificRooms($room_type)
    {
        $rooms = DB::table('rooms')
            ->select('rooms.id as id', 'rooms.code as code', 'rooms.room_number as room_number', 'room_types.capacity as capacity')
            ->addSelect(DB::raw("(select COUNT(*) from boarding_passes as bp join tickets t on t.id=bp.ticket_id where bp.room_id=rooms.id and bp.checked_out_at is null and t.called !='inf') as count_beds_not_available"))
            ->where('type_id', '=', $room_type)
            ->join('room_types', 'rooms.type_id', '=', 'room_types.id')
            ->get();

        return $rooms;
    }
}
