<?php

namespace App\Repositories\Eloquent;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;

class UserRepository extends BaseRepository
{
    protected function resolveModel(): Model
    {
        return new User();
    }

    public function findByEmail(string $email): ?User
    {
        return $this->executeWithLogging(__FUNCTION__, function () use ($email) {
            return $this->model->where('email', $email)->first();
        });
    }

    public function createWithRole(array $data, string $role): User
    {
        return $this->executeWithLogging(__FUNCTION__, function () use ($data, $role) {
            $user = $this->model->create($data);
            $user->assignRole($role);
            return $user->load('roles');
        });
    }
}
