<?php

namespace App\Console\Commands;

use App\Models\DnsQueryUsage;
use App\Models\User;
use Illuminate\Console\Command;

class TrackDnsQueryUsage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dns:track-queries {user_id} {count=1}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Incrementează contorul de query-uri DNS pentru un utilizator (pentru testare)';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $userId = $this->argument('user_id');
        $count = $this->argument('count');

        $user = User::find($userId);
        
        if (!$user) {
            $this->error("Utilizatorul cu ID {$userId} nu există!");
            return 1;
        }

        $usage = DnsQueryUsage::getOrCreateForCurrentMonth($userId);
        $usage->incrementQueries($count);

        $this->info("✓ Adăugate {$count} query-uri pentru {$user->name}");
        $this->info("Total query-uri luna curentă: " . number_format($usage->query_count));
        
        $plan = $user->plan ?? $user->resellerPlan;
        if ($plan && $plan->dns_queries_limit) {
            $percentage = ($usage->query_count / $plan->dns_queries_limit) * 100;
            $this->info("Utilizare: " . number_format($percentage, 2) . "%");
            
            if ($percentage >= 80) {
                $this->warn("⚠️ Atenție: Utilizare peste 80%!");
            }
        } else {
            $this->info("Plan: Nelimitat");
        }

        return 0;
    }
}
