<?php

namespace App\Console\Commands;

use App\Models\Notification;
use App\Models\Renter;
use Carbon\Carbon;
use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        //
        $tomorrow = Carbon::now()->addDay();
        $tomorrowDay = $tomorrow->copy()->day;
        $month = $tomorrow->copy()->month;

        $renters = Renter::query()->whereDay('birthday', $tomorrowDay)
            ->where('status', Renter::LIVING)
            ->whereMonth('birthday', $month)
            ->with('hostel')
            ->get();
        foreach ($renters as $renter) {
            if ($renter->hostel) {
                if ($renter->hostel->owner) {
                    Notification::create([
                        'to_user' => $renter->hostel->owner_id,
                        'title' => 'Thông báo từ itro.vn',
                        'user_id' => $renter->hostel->owner_id,
                        'content' => 'Ngày mai là sinh nhật của ' . $renter->name . ', phòng ' . $renter->room_name . ', nhà trọ ' . $renter->hostel_name,
//                        'payload' => json_encode([
//                            'id' => $renter->user_id,
//                            'type' => config('constants.TRANSACTION')
//                        ])
                    ]);
                }
                $this->line('Success send birthday: ' . $renter->user_id);
            }
        }
    }
}
