<?php

namespace App\Repositories;

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

class AkunRepository
{
    protected $akun;

    public function __construct(Akun $akun)
    {
        $this->akun = $akun;
    }

    public function getAll(): Collection
    {
        return $this->akun->orderBy('kategori')->orderBy('kode_akun')->get();
    }

    public function findById(string $id): ?Akun
    {
        return $this->akun->find($id);
    }

    public function findByKodeAkun(string $kode): ?Akun
    {
        return $this->akun->where('kode_akun', $kode)->first();
    }

    public function createAkun(array $data): Akun
    {
        return $this->akun->create($data);
    }

    public function updateAkun(string $id, array $data): Akun
    {
        $akun = $this->akun->find($data['id']);
        $akun->update($data);
        throw new \Exception($akun);
        return $akun;
    }

    public function deleteAkun(string $id)
    {
        $akun = $this->akun->where('kode_akun', $id)->first();
        if (!$akun) throw new \Exception('Akun tidak ditemukan');
        return $akun->delete();
    }
}
