<?php

namespace Database\Seeders;

use App\Models\User;
use App\Support\PhoneNumber;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class AdminUserSeeder extends Seeder
{
    public function run(): void
    {
        $admins = [
            [
                'name' => env('HOTEL_ADMIN_NAME', 'Администратор Hotel Pineta'),
                'phone' => env('HOTEL_ADMIN_PHONE'),
                'password' => env('HOTEL_ADMIN_PASSWORD'),
            ],
            [
                'name' => env('HOTEL_ADMIN_BACKUP_NAME', 'Резервный администратор Hotel Pineta'),
                'phone' => env('HOTEL_ADMIN_BACKUP_PHONE'),
                'password' => env('HOTEL_ADMIN_BACKUP_PASSWORD'),
            ],
        ];

        foreach ($admins as $adminData) {
            $phone = PhoneNumber::normalize((string) ($adminData['phone'] ?? ''));
            $password = (string) ($adminData['password'] ?? '');

            if ($phone === '' || $password === '') {
                continue;
            }

            $user = User::query()->where('phone', $phone)->first()
                ?: User::query()->where('phone', '+'.$phone)->first();

            $user ??= new User();
            $user->fill([
                'name' => (string) ($adminData['name'] ?? 'Администратор Hotel Pineta'),
                'phone' => $phone,
                'email' => null,
                'password' => Hash::make($password),
                'role' => 'admin',
                'is_active' => true,
            ])->save();
        }
    }
}
