<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SupplierNotifier extends Model
{
    use HasFactory;

    protected int $minHour = 0;

    protected array $notifierHour = [3, 8, 24];

    protected array $workHour = [
        1 => [4, 18],
        2 => [4, 18],
        3 => [4, 18],
        4 => [4, 18],
        5 => [4, 16],
        6 => [23, -1],
        0 => [23, -1],

    ];

    private Carbon $nowTime;

    public function __construct()
    {
        parent::__construct();
        $this->minHour = $this->notifierHour[0];
        $this->nowTime = Carbon::now();
    }

    private function getNextTime($timeStr, $needHour): string
    {
        $Time = Carbon::createFromDate($timeStr);
        $nextDay = $Time->copy()->nextWeekday();
        $dayRecentHour = $this->workHour[$nextDay->dayOfWeek][1] - $this->workHour[$nextDay->dayOfWeek][0];
        if ($needHour < $dayRecentHour) {
            // datum visszaad
            return $nextDay->setTime($this->workHour[$nextDay->dayOfWeek][0] + $needHour, $Time->minute)->toDateTimeString();
        } else {
            // return 'rec>'.var_export([$Time,$needHour,$dayRecentHour],true);
            return $this->getNextTime($Time->copy()->nextWeekday(), ($needHour - $dayRecentHour));
        }
    }

    public function getLowestWorkTime($timeStr): string
    {
        $Time = Carbon::createFromDate($timeStr);
        /*
         * ha korábabn adták le mint az adott nap első munkaórája, akkor az kerül beállításra
         */
        if ($Time->hour < $this->workHour[$Time->dayOfWeek][0]) {
            $Time->setTime($this->workHour[$Time->dayOfWeek][0], 0);
            // var_dump($Time->toDateTimeString(),'Set workStart hour');
        }
        /*
         * ha késöbb adták le mint az adott nap utolsó munkaórája, akkor a következő munkanap első órája kerül beállításra
         */
        if ($Time->hour >= $this->workHour[$Time->dayOfWeek][1]) {
            $Time->nextWeekday();
            $Time->setTime($this->workHour[$Time->dayOfWeek][0], 0);
            // var_dump($Time->toDateTimeString(),'Set NexDay');
        }

        return $Time->toDateTimeString();
    }

    public function makeTime($timeStr): array
    {
        /*
                $timeStr='2023-09-08 15:12:12'; //péntek in work
                $timeStr='2023-09-08 6:12:12'; //péntek +3+8
                $timeStr='2023-09-08 17:12:12'; //péntek out of  work
                $timeStr='2023-09-08 11:12:12'; //péntek +3
                $timeStr='2023-09-04 07:12:12'; //hetfő +3+8
                $timeStr='2023-09-04 03:12:12'; //hetfő before work
                $timeStr='2023-09-04 18:12:12'; //hetfő out of  work
                $timeStr='2023-09-04 07:12:12'; //hetfő +3+8
        */
        $Time = Carbon::createFromDate($timeStr);
        // var_dump($Time->toDateTimeString(),'Original');
        $Time = Carbon::createFromDate($this->getLowestWorkTime($timeStr));

        $endOfDay = Carbon::create($Time);
        $endOfDay = $endOfDay->setTime($this->workHour[$Time->dayOfWeek][1], 0);
        // var_dump('aaa>'.$endOfDay->toDateTimeString().' '.$Time->toDateTimeString());
        $dayRecentHour = $Time->diffInHours($endOfDay);

        $notifiedDateTime = [];
        foreach ($this->notifierHour as $needHour) {
            if ($dayRecentHour < $needHour) {
                $notifiedDateTime[] = $this->getNextTime($Time, ($needHour - $dayRecentHour));
            } else {
                $notifiedDateTime[] = $Time->copy()->addHour($needHour)->toDateTimeString();
            }
        }

        return $notifiedDateTime;
    }

    public function addNotificationsForOrderArchive(int $orderArchiveID, int $supplierID, array $times)
    {
        foreach ($times as $time) {
            $SupplierNotifier = new SupplierNotifier;
            $SupplierNotifier->order_archive_id = $orderArchiveID;
            $SupplierNotifier->supplier_id = $supplierID;
            $SupplierNotifier->need_send = $time;
            $SupplierNotifier->save();
        }
    }

    public function makeNotificationsForOrderArchive(int $orderArchiveID)
    {
        $OrderArchive = OrderArchive::find($orderArchiveID);
        $times = $this->makeTime($OrderArchive->sentTime);
        $this->addNotificationsForOrderArchive($OrderArchive->id, $OrderArchive->supplier_id, $times);
    }

    /**
     * The items that belong to the OrderArchive.
     */
    public function orderArchive(): BelongsTo
    {
        return $this->belongsTo(OrderArchive::class);
    }

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class);
    }
}
