<?php

namespace App\Console\Commands\Staking;

use App\Models\Staking\StakingUser;
use Illuminate\Console\Command;
use Setting;

class StakingRewardCalucationCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'staking:rewards-calculate';

    /**
     * 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 int
     */
    public function handle()
    {
        // Use chunk to optimize query and avoid memory leak
        StakingUser::active()->chunk(100, function($stakes) {
            foreach ($stakes as $stake) {

                $fixedReward = $stake->apy / 100;

                $amount = math_percentage($stake->amount, $fixedReward);
                $reward = math_formatter($amount / $stake->days, 8);

                $stake->reward = $stake->reward + $reward;
                $stake->save();
            }
        });
    }
}
