<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Tenant;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;

/**
 * @extends Factory<User>
 */
class UserFactory extends Factory
{
    protected static ?string $password = null;

    /**
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'tenant_id' => Tenant::factory(),
            'email' => fake()->unique()->safeEmail(),
            'password_hash' => static::$password ??= Hash::make('password'),
            'first_name' => fake()->firstName(),
            'last_name' => fake()->lastName(),
            'timezone' => 'Europe/Moscow',
            'is_active' => true,
            'totp_enabled' => false,
            'sound_enabled' => true,
            'email_verified_at' => now(),
            // Schema-default matrix (см. schema.sql:699). Eloquent не перечитывает
            // строку после INSERT, поэтому колонки с DB-DEFAULT'ами видны как
            // null на свежесозданной модели — нужно явно задать здесь.
            'notification_preferences' => [
                'new_lead' => ['inapp' => true, 'push' => true, 'email' => true],
                'low_balance' => ['email' => true],
                'zero_balance' => ['email' => true],
                'topup_success' => ['email' => true],
                'invoice_paid' => ['email' => true],
                'new_device_login' => ['email' => true],
                'marketing' => ['email' => false],
            ],
        ];
    }

    public function unverified(): static
    {
        return $this->state(fn (array $attributes) => [
            'email_verified_at' => null,
        ]);
    }

    public function inactive(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_active' => false,
        ]);
    }
}
