<?php

namespace App\Console\Commands;

use App\Models\CollectSpend;
use App\Models\CollectSpendCycleLog;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        //
        $items = CollectSpend::query()
            ->has('cycle')
            ->get();
        foreach ($items as $item) {
            $log = CollectSpendCycleLog::query()
                ->where('collect_spend_id', $item->id)
                ->latest()
                ->first();
            if ($log) {
                $lastProcess = $log->last_processed_at;
            } else {
                $lastProcess = $item->created_at;
            }

            if ($log) {
                if ($log->remain_cycle == 0) {
                    continue;
                }
            }

            $cycle = $item->cycle;
            $nextDateProcess = null;
            if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_CUSTOM) {
                if (!$cycle->repeat_every_custom) {
                    continue;
                }
                if ($cycle->repeat_type_custom == 1) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addDay($cycle->repeat_every_custom);
                } else if ($cycle->repeat_type_custom == 2) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addWeek($cycle->repeat_every_custom);
                } else if ($cycle->repeat_type_custom == 3) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addMonth($cycle->repeat_every_custom);
                } else if ($cycle->repeat_type_custom == 4) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addYear($cycle->repeat_every_custom);
                }
            } else {

                if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_1_WEEK) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addWeek();
                } else if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_2_WEEK) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addWeek(2);
                } else if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_1_MONTH) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addMonth();
                } else if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_2_MONTH) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addMonth(2);
                } else if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_3_MONTH) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addMonth(3);
                } else if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_6_MONTH) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addMonth(6);
                } else if ($cycle->repeat_every == \App\Models\CollectSpendCycle::REPEAT_EVERY_1_YEAR) {
                    $nextDateProcess = $lastProcess->copy()->startOfDay()->addYear();
                }
            }

            if (empty($nextDateProcess)) {
                continue;
            }

            $current = Carbon::now();
            if ($current->copy()->lessThan($nextDateProcess)) {
                continue;
            }
            $createData = $item->toArray();
            $createData['created_at'] = $current->copy();
            $createData['updated_at'] = $current->copy();
            $createData['date_action'] = $current->copy();
            $cp = CollectSpend::create($createData);

            if ($cycle->unlimited_cycles) {
                $remainCycle = -1;
            } else {
                if ($log) {
                    $remainCycle = $log->remain_cycle - 1;
                } else {
                    $remainCycle = $cycle->cycles - 1;
                }
            }
            CollectSpendCycleLog::create([
                'collect_spend_id' => $item->id,
                'content' => $createData,
                'last_processed_at' => $nextDateProcess,
                'remain_cycle' => $remainCycle
            ]);

            if (!empty($item->owner_id)) {
                $ownerId = $item->owner_id;
            } else {
                $ownerId = $item->hostel->owner_id;
            }

            if ($ownerId) {
                $owner = User::find($ownerId);
                if ($owner) {
                    \Notification::sendNow($owner, new \App\Notifications\CollectSpendCycle($cp->id));
                }
            }

            if ($item->hostel) {
                $staffs = $item->hostel->staffHostels;
                if (!empty($staffs->count())) {
                    foreach ($staffs as $staff) {
                        \Notification::sendNow($staff, new \App\Notifications\CollectSpendCycle($cp->id));
                    }
                }

                $this->line('Processed: ' . $item->id);
            }
        }
    }
}