<?php

namespace App\Repositories;

use App\Models\TransaksiTabungan;
use App\Services\JurnalService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class TransaksiTabunganRepository
{
    protected $transaksiTabungan;
    protected $jurnalService;

    public function __construct(TransaksiTabungan $transaksiTabungan, JurnalService $jurnalService)
    {
        $this->transaksiTabungan = $transaksiTabungan;
        $this->jurnalService = $jurnalService;
    }

    public function getAllTransaksi($tgl_awal, $tgl_akhir, $rekening = null): Collection
    {
        $transaksi = $this->transaksiTabungan->with('tabungan')
            ->whereBetween('trans_tglbuku', [$tgl_awal, $tgl_akhir]);

        if ($rekening) {
            $transaksi->where('rekening', $rekening);
        }
        return $transaksi->get();
    }

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

    public function findTransaksiById(string $id): ?TransaksiTabungan
    {
        return $this->transaksiTabungan->find($id);
    }

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

    public function validasiTransaksi(string $id)
    {
        try {
            DB::transaction(function () use ($id) {
                $transaksiTabungan = $this->transaksiTabungan->find($id);
                $transaksiTabungan->status = 1;
                $transaksiTabungan->save();

                $tabungan = $transaksiTabungan->tabungan;
                $tabungan->saldo += $transaksiTabungan->setor - $transaksiTabungan->tarik;
                $tabungan->save();

                $jurnal = $this->jurnalService->findByTransId($transaksiTabungan->trans_id);
                if ($jurnal instanceof Collection) {
                    $jurnal->each(function ($item) {
                        $item->status = 1;
                        $item->save();
                    });
                } elseif ($jurnal) {
                    $jurnal->status = 1;
                    $jurnal->save();
                }

                return $transaksiTabungan;
            });
            return response()->json(['message' => 'Transaksi berhasil divalidasi'], 200);
        } catch (\Exception $e) {
            throw new \Exception('Error validating transaction: ' . $e->getMessage(), 500);
        }
    }

    public function deleteTransaksi(string $id)
    {
        try {
            DB::transaction(function () use ($id) {
                $transaksiTabungan = $this->transaksiTabungan->find($id);
                $transaksiTabungan->status = 9;
                $transaksiTabungan->save();

                $tabungan = $transaksiTabungan->tabungan;
                $tabungan->saldo -= $transaksiTabungan->setor - $transaksiTabungan->tarik;
                $tabungan->save();

                $jurnal = $this->jurnalService->findByTransId($transaksiTabungan->trans_id);
                if ($jurnal instanceof Collection) {
                    $jurnal->each(function ($item) {
                        $item->status = 9;
                        $item->save();
                    });
                } elseif ($jurnal) {
                    $jurnal->status = 9;
                    $jurnal->save();
                }

                return $transaksiTabungan;
            });
            return response()->json(['message' => 'Transaksi berhasil dihapus'], 200);
        } catch (\Exception $e) {
            throw new \Exception('Error validating transaction: ' . $e->getMessage(), 500);
        }
    }
}
