<?php

namespace App\Http\Controllers\Backend;

use App\Components\Functions;
use App\Jobs\SendNotificationAdmin;
use App\Models\Hostel;
use App\Models\Notification;
use App\Models\RenterRoom;
use App\Models\Room;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\Datatables\Datatables;

class NotificationController extends AdminController {
	//
	public function getNotificationByAttributeView() {
		$hostels = Hostel::orderBy( 'id', 'desc' );

		if ( auth( 'backend' )->check() ) {
			if ( auth( 'backend' )->user()->type == User::OWNER ) {
				$hostels = Hostel::where( 'owner_id', auth( 'backend' )->user()->id );
			}
		}

		$hostels = $hostels->get();

		return view( 'admin.notification.index', compact( 'hostels' ) );
	}

	public function getNotificationByAttribute() {
		$items = Notification::publish();

		if ( auth( 'backend' )->check() ) {
			if ( auth( 'backend' )->user()->type == User::OWNER ) {
				$hostelIds = Hostel::where( 'owner_id', auth( 'backend' )->user()->id )->pluck( 'id' )->toArray();
				$items     = $items->whereIn( 'notifications.hostel_id', $hostelIds );
			}
		}

		$items = $items->select( \DB::raw( 'notifications.*, hostels.name as hostel, rooms.name as room, users.name as user_name' ) )
		               ->leftjoin( 'hostels', 'notifications.hostel_id', '=', 'hostels.id' )
		               ->leftJoin( 'users', 'notifications.to_user', '=', 'users.id' )
		               ->leftJoin( 'rooms', 'notifications.room_id', '=', 'rooms.id' );


		$items = $items->orderBy('id', 'desc');
		//$items = $items->groupBy( 'notifications.hostel_id', 'notifications.room_id' );

		return Datatables::of( $items )->addColumn( 'action', function () {

		} )->editColumn( 'status', function ( $item ) {
			return $item->status_text_html;
		} )->editColumn( 'created_at', function ( $item ) {
			return $item->created_at->format( 'd/m/Y' );
		} )->make( true );
	}

	public function createByAdmin( Request $request ) {
		$to      = $request->input( 'to' );
		$title   = $request->input( 'title' );
		$content = $request->input( 'content' );

		if ( empty( $to ) ) {
			return response( [
				'status'  => 0,
				'message' => 'Không được bỏ trống đối tượng'
			] );
		}

		if ( empty( $title ) ) {
			return response( [
				'status'  => 0,
				'message' => 'Không được bỏ trống tiêu đề'
			] );
		}

		if ( empty( $content ) ) {
			return response( [
				'status'  => 0,
				'message' => 'Không được bỏ trống nội dung'
			] );
		}

		$this->dispatch( new SendNotificationAdmin( $to, $title, $content ) );

		return response( [
			'status'  => 1,
			'message' => 'Thành công'
		] );

	}

	public function create( Request $request ) {
		$hostels = $request->input( 'hostels' );
		$rooms   = $request->input( 'rooms' );
		$title   = $request->input( 'title' );
		$content = $request->input( 'content' );

		if ( empty( $content ) ) {
			return response( [
				'status'  => 0,
				'message' => 'Không được bỏ trống nội dung'
			] );
		}


		if ( auth( 'backend' )->check() ) {
			if ( auth( 'backend' )->user()->type == User::OWNER ) {
				if ( empty( $hostels ) ) {
					$hostels = Hostel::where( 'owner_id', auth( 'backend' )->user()->id )
					                 ->pluck( 'id' )->toArray();

					$rooms = Room::wherein( 'hostel_id', $hostels )->pluck( 'id' )->toArray();
				}

			}
		}

		if ( is_array( $hostels ) ) {
			foreach ( $hostels as $hostel ) {

				if ( is_array( $rooms ) ) {
					foreach ( $rooms as $room ) {

						$renterRooms = RenterRoom::where( 'room_id', $room )->get();

						foreach ( $renterRooms as $renterRoom ) {

							Notification::create( [
								'hostel_id' => $hostel,
								'room_id'   => $room,
								'title'     => $title,
								'to_user'   => $renterRoom->user_id,
								'content'   => $content,
								'user_id'   => auth( 'backend' )->user()->id
							] );
						}

					}
				}

			}
		}

		return response( [
			'status'  => 1,
			'message' => 'Thành công'
		] );

	}
}
