<?php

namespace App\Repositories;

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

class TransaksiDepositoRepository
{
    protected $transaksiDeposito;

    public function __construct(TransaksiDeposito $transaksiDeposito)
    {
        $this->transaksiDeposito = $transaksiDeposito;
    }

    public function getAllTransaksi($tgl_awal, $tgl_akhir): Collection
    {
        return $this->transaksiDeposito->with('deposito', 'deposito.anggota')->whereBetween('trans_tglbuku', [$tgl_awal, $tgl_akhir])->get();
    }

    public function getLastTransaksi($data): ?TransaksiDeposito
    {
        $date = $data['trans_tglbuku'];
        return $this->transaksiDeposito->whereDate('trans_tglbuku', $date)->where('trans_kode', $data['trans_kode'])->orderBy('trans_date', 'desc')->first();
    }

    public function findTransaksiByTransId(string $id): ?TransaksiDeposito
    {
        return $this->transaksiDeposito->find($id);
    }

    public function createTransaksi(array $data): TransaksiDeposito
    {
        return $this->transaksiDeposito->create($data);
    }

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

    public function deleteTransaksi(string $id)
    {
        $transaksiDeposito = $this->transaksiDeposito->findByKodeAkun($id);
        return $transaksiDeposito->delete();
    }
}
