<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update member data';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $members = \App\Models\Member::all();

        foreach($members as $member)
        {

            $status = $member->getStatus();
            $status = $status["status"];

            $has_certificate = $member->hasCertificate();
            $certificate = 0;
            $certificate_date = null;
            if($has_certificate["date"] != '')
            {
                if($has_certificate["date"] < date("Y-m-d"))
                    $certificate = 0;

                if($has_certificate["date"] >= date("Y-m-d") && $has_certificate["date"] < date("Y-m-d", strtotime("+1 month")))
                    $certificate = 1;

                if($has_certificate["date"] >= date("Y-m-d", strtotime("+1 month")))
                    $certificate = 2;

                $certificate_date = $has_certificate["date"] != '' ? $has_certificate["date"] : null;
            }

            $member->current_status = $status;
            $member->certificate = $certificate;
            $member->certificate_date = $certificate_date;
            $member->save();

        }

        return Command::SUCCESS;
    }
}
