<?php

namespace App\Repositories;

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

class UserRepository
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function getAll(): Collection
    {
        return $this->user->with('role')->get();
    }

    public function findById(int $id): ?User
    {
        return $this->user->with('role')->find($id);
    }

    public function findByUsername(string $username): ?User
    {
        return $this->user->where('username', $username)->first();
    }

    public function findByPegawaiId(string $pegawaiId): ?User
    {
        return $this->user->where('pegawai_id', $pegawaiId)->first();
    }

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

    public function create(array $data): User
    {
        return $this->user->create($data);
    }

    public function update(int $id, array $data): ?User
    {
        $user = $this->findById($id);
        $user->update($data);
        return $user;
    }

    public function delete(string $username): bool
    {
        $user = $this->findByUsername($username);
        return $user->delete();
    }
}
