<?php

namespace Database\Seeders;

use App\Enums\UserRole;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     *
     * In production this never writes a known password to the database —
     * `admin@niyarasa.com` / `password` is fine for local development, but
     * shipping that exact credential to a live store is the kind of thing
     * that ends up on a "how we got breached" blog post. A random one is
     * generated and printed once, and the sample customer account is
     * skipped entirely.
     */
    public function run(): void
    {
        if (app()->isProduction()) {
            if (! User::where('role', UserRole::Admin)->exists()) {
                $password = Str::password(16);

                User::create([
                    'name' => 'Niayarsa Admin',
                    'email' => 'admin@niyarasa.com',
                    'password' => $password,
                    'role' => UserRole::Admin,
                ]);

                $this->command?->warn("Admin account created — email: admin@niyarasa.com / password: {$password}");
                $this->command?->warn('Save this now — it is not stored or shown again. Change it after first login.');
            }

            return;
        }

        User::firstOrCreate(
            ['email' => 'admin@niyarasa.com'],
            ['name' => 'Niayarsa Admin', 'password' => 'password', 'role' => UserRole::Admin]
        );

        User::firstOrCreate(
            ['email' => 'test@example.com'],
            ['name' => 'Test User', 'password' => 'password', 'role' => UserRole::Customer]
        );
    }
}
