<?php

namespace App\Console\Commands;

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

class CreateMissingAffiliates extends Command
{
    protected $signature = 'affiliates:create-missing';
    protected $description = 'Erstellt Affiliate-Accounts für alle User die noch keinen haben';

    public function handle(): void
    {
        $users = User::whereDoesntHave('affiliate')->get();

        foreach ($users as $user) {
            Affiliate::create([
                'user_id' => $user->id,
                'code'    => Affiliate::generateCode($user),
                'status'  => 'active',
            ]);

            $this->info("Affiliate erstellt für {$user->name}: " . $user->affiliate->code);
        }

        $this->info("{$users->count()} Affiliate-Accounts erstellt.");
    }
}
