<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;

/**
 * Cria o administrador master da plataforma a partir do .env.
 * Rode com: php artisan db:seed --class=MasterUserSeeder
 */
class MasterUserSeeder extends Seeder
{
    public function run(): void
    {
        $email = env('MASTER_EMAIL');
        $senha = env('MASTER_PASSWORD');

        if (blank($email) || blank($senha)) {
            $this->command->error('Defina MASTER_EMAIL e MASTER_PASSWORD no .env antes de rodar este seeder.');

            return;
        }

        $master = User::withTrashed()->firstOrNew(['email' => $email]);

        $master->fill([
            'name' => env('MASTER_NAME', 'Administrador Master'),
            'password' => $senha,
            'role' => User::ROLE_MASTER,
            'company_id' => null,
            'is_active' => true,
        ]);

        $master->deleted_at = null;
        $master->save();

        $this->command->info("Master pronto: {$email}");
    }
}
