<?php

namespace App\Providers;

use Illuminate\Auth\EloquentUserProvider;

class TenantUserProvider extends EloquentUserProvider
{
    public function retrieveByCredentials(array $credentials)
    {
        $credentials['active'] = true;

        $query = $this->createModel()->newQuery();

        foreach ($credentials as $key => $value) {
            if (! str_contains($key, 'password')) {
                $query->where($key, $value);
            }
        }

        if (app('tenant')->slug === 'lv') {
            return $query->first();
        }

        $query->where([
            'local_group' => app('tenant')->slug,
            'active' => true

        ]);

        return $query->first();
    }
}
