<?php

namespace App\Services;

use App\Models\User;
use Illuminate\Support\Str;

class UserLookupService
{
    /**
     * Resolves a login field that may hold either an email or a username.
     * Username is only alpha_dash-validated at registration (case preserved),
     * so it needs an explicit case-insensitive compare rather than relying
     * on the database's collation.
     */
    public function findByLogin(string $login): ?User
    {
        return User::where('email', $login)->first()
            ?? User::whereRaw('LOWER(username) = ?', [Str::lower($login)])->first();
    }
}
