<?php

namespace App\Console\Commands;

use App\Models\Subscriber;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class CommonOperationCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:common-operation-cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'All common operation cron';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $subscribers = Subscriber::where('yearly_price', '>', 0)
            ->whereDate('end_date', '>', Carbon::now())
            ->get();

        foreach ($subscribers as $subscriber) {
            $subscriber->use_credit = 0;
            $subscriber->save();
        }

        $subscribers = Subscriber::where('status', SUBSCRIPTION_STATUS_ACTIVE)
            ->whereDate('end_date', '<', Carbon::now())
            ->get();

        foreach ($subscribers as $subscriber) {
            $subscriber->status = SUBSCRIPTION_STATUS_CANCEL;
            $subscriber->save();
        }

        $this->info('Credits have been refilled for active yearly subscribers.');
    }
}
