<?php

namespace App\Console\Commands;

use App\Models\ElectricWater;
use App\Models\Hostel;
use App\Models\Notification;
use App\Models\Room;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

class SendNotificationSchedule extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'notification:schedule';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $firstDayOfMonth = Carbon::now()->firstOfMonth()->day;
        $lastDayOfMonth = Carbon::now()->lastOfMonth()->day;

        $currentDay = Carbon::now()->day;

        if ($currentDay == $lastDayOfMonth) {


            $users = User::query()->where('type', User::OWNER)->get();

            foreach ($users as $user) {

                $startTime = Carbon::now()->firstOfMonth()->startOfDay()->toDateTimeString();
                $endTime = Carbon::now()->lastOfMonth()->endOfDay()->toDateTimeString();

                $check = Notification::where('to_user', $user->id)->where('content', 'Cuối tháng rồi, hãy chốt số điện, số nước ngay nào')
                    ->whereBetween('created_at', [$startTime, $endTime])->count();

                if ($check > 0) {
                    continue;
                }

                $hostels = Hostel::where('owner_id', $user->id)->get();
                $cntRoom = Room::query()
                    ->whereIn('hostel_id', $hostels->pluck('id')->toArray())
                    ->count();
                if ($cntRoom > 0) {
                    $payload = json_encode([
                        'id' => null,
                        'type' => null
                    ]);
                    Notification::create([
                        'to_user' => $user->id,
                        'title' => 'Thông báo từ itro.vn',
                        'user_id' => $user->id,
                        'content' => 'Cuối tháng rồi, hãy chốt số điện, số nước ngay nào',
                        'payload' => $payload,
                    ]);
                }
            }
        }

        if ($currentDay == $firstDayOfMonth) {
            $startBeforeMonth = Carbon::now()->subMonth(1)->startOfMonth()->toDateTimeString();
            $endBeforeMonth = Carbon::now()->subMonth(1)->endOfMonth()->toDateTimeString();

            $owners = User::where('type', User::OWNER)->get();


            foreach ($owners as $owner) {

                $startTime = Carbon::now()->firstOfMonth()->toDateTimeString();
                $endTime = Carbon::now()->lastOfMonth()->toDateTimeString();

                $check = Notification::where('to_user', $owner->id)->where('content', 'LIKE', '%phòng nữa chưa chốt điện nước. Hãy kiểm tra và chốt điện nước ngay nào%')
                    ->whereBetween('created_at', [$startTime, $endTime])->count();

                if ($check > 0) {
                    continue;
                }

                $numberNotEw = 0;

                $hostels = Hostel::where('owner_id', $owner->id)->pluck('id')->toArray();

                if (!empty($hostels)) {
                    foreach ($hostels as $hostel) {
                        $roomsEwed = ElectricWater::whereBetween('date_action', [
                            $startBeforeMonth,
                            $endBeforeMonth
                        ])->where('hostel_id', $hostel)->count();

                        $numberRoom = Room::where('hostel_id', $hostel)->count();
                        $remain = $numberRoom - $roomsEwed;
                        $numberNotEw += $remain;
                    }
                }

                $payload = json_encode([
                    'id' => null,
                    'type' => null
                ]);

                if ($numberNotEw > 0) {
                    Notification::create([
                        'to_user' => $owner->id,
                        'title' => 'Thông báo từ itro.vn',
                        'user_id' => $owner->id,
                        'content' => 'Còn ' . $numberNotEw . ' phòng nữa chưa chốt điện nước. Hãy kiểm tra và chốt điện nước ngay nào',
                        'payload' => $payload,
                    ]);
                }


            }


        }

        $time = Carbon::now()->subMinute(30)->toDateTimeString();

        $users = User::where('created_at', '<=', $time)->where('status_phone', User::NOT_CONFIRM)->where('send_notification_first', false)->get();

        foreach ($users as $user) {

        }
    }
}
