<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Models\UserDownload;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class ResetUserDownloads extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:reset-user-downloads';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Resets the daily limit of user downloads';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        User::query()->update([
            'downloads_left' => config('hstream.free_downloads_count'),
        ]);

        // Clear old downloads which have expired
        UserDownload::where('created_at', '<=', Carbon::now()->subHour(6))
            ->delete();
    }
}
