<?php namespace Tps\Birzha\Console;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use RainLab\User\Models\User;
use Symfony\Component\Console\Input\InputArgument;
use TPS\Birzha\Models\Payment;

class MakeGiftToUsers extends Command
{
    /**
     * @var string The console command name.
     */
    protected $name = 'birzha:gifttousers';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'birzha:gifttousers {amount}';

    /**
     * @var string The console command description.
     */
    protected $description = 'Makes a gift to every registered user.';

    /**
     * Execute the console command.
     * @return void
     */
    public function handle()
    {
        $amount = $this->argument('amount');

        if(is_numeric($amount)) {

            $users = User::all();

            try {
                    $users->each(function($user) use ($amount) {
                    $newPayment = new Payment;
                    $newPayment->user_id = $user->id;
                    $newPayment->amount = $amount;
                    $newPayment->payment_type = Payment::PAYMENT_TYPE_GIFT;
                    $newPayment->save();
                });
            } catch (\Throwable $th) {
                Log::info('Artisan make gift to users: ' . $th->getMessage());
                $this->error($th->getMessage());
            }
        } else {
            $this->info('the argument should be a number');
        }
    }

    /**
     * Get the console command arguments.
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['amount', InputArgument::REQUIRED, 'An amount of the gift payment.', 100]
        ];
    }

    /**
     * Get the console command options.
     * @return array
     */
    protected function getOptions()
    {
        return [];
    }
}
