<?php

namespace App\Services;

use App\Repositories\PenempatanBankRepository;
use App\Repositories\TransaksiPenempatanRepository;
use Carbon\Carbon;
use Exception;

class TransaksiPenempatanService
{
    protected $transaksiPenempatanRepository;
    protected $penempatanBankRepository;
    protected $jurnalService;

    public function __construct(TransaksiPenempatanRepository $transaksiPenempatanRepository, PenempatanBankRepository $penempatanBankRepository, JurnalService $jurnalService)
    {
        $this->transaksiPenempatanRepository = $transaksiPenempatanRepository;
        $this->penempatanBankRepository = $penempatanBankRepository;
        $this->jurnalService = $jurnalService;
    }

    public function getAll($tgl_awal, $tgl_akhir, $rekening = null)
    {
        return $this->transaksiPenempatanRepository->getAllTransaksi($tgl_awal, $tgl_akhir, $rekening);
    }

    public function findById($id)
    {
        return $this->transaksiPenempatanRepository->findTransaksiById($id);
    }

    public function createTransaksi(array $data)
    {
        $latest_transaksi = $this->transaksiPenempatanRepository->getLastTransaksi($data);

        if ($latest_transaksi && preg_match('/(\d{5})$/', $latest_transaksi->trans_id, $matches)) {
            $latest_id = (int)$matches[1];
        } else {
            $latest_id = 0;
        }

        $tglbuku_numeric = preg_replace('/\D/', '', $data['trans_tglbuku']);

        $transaksi = [];

        $transaksi['trans_id'] = $data['trans_kode'] . "." . $tglbuku_numeric . "." . str_pad($latest_id + 1, 5, "0", STR_PAD_LEFT);
        $transaksi['trans_date'] = Carbon::now();
        $transaksi['trans_kode'] = $data['trans_kode'];
        $transaksi['keterangan'] = $data['keterangan'] ?? "";
        $transaksi['trans_tglbuku'] = $data['trans_tglbuku'];

        $penempatan = $this->penempatanBankRepository->findByRekening($data['rekening']);
        if (!$penempatan) {
            throw new \Exception('Rekening tidak ditemukan');
        }

        $transaksi['penempatan_id'] = $penempatan->id;
        $akun_kas = $penempatan->akun_penempatan;

        // kas masuk
        if ($data['trans_kode'] == '21') {
            $transaksi['debet'] = $data['nominal'];
            $transaksi['akun_debet'] = $akun_kas;
            $transaksi['kredit'] = 0;
            $transaksi['akun_kredit'] = $data['kode_akun'];

            $this->jurnalService->createJurnal([
                'trans_id' => $transaksi['trans_id'],
                'trans_kode' => $transaksi['trans_kode'],
                'akun_jurnal' =>  $akun_kas,
                'debet' => $data['nominal'],
                'kredit' => 0,
                'keterangan' => $transaksi['keterangan'],
                'trans_tglbuku' => $transaksi['trans_tglbuku'],
                'trans_date' => $transaksi['trans_date']
            ]);
            $this->jurnalService->createJurnal([
                'trans_id' => $transaksi['trans_id'],
                'trans_kode' => $transaksi['trans_kode'],
                'akun_jurnal' => $data['kode_akun'],
                'debet' => 0,
                'kredit' => $data['nominal'],
                'rekening' => $penempatan->rekening,
                'keterangan' => $transaksi['keterangan'],
                'trans_tglbuku' => $transaksi['trans_tglbuku'],
                'trans_date' => $transaksi['trans_date']
            ]);
        }
        // kas keluar
        if ($data['trans_kode'] == '22') {
            $transaksi['akun_debet'] = $data['kode_akun'];
            $transaksi['debet'] = 0;
            $transaksi['akun_kredit'] = $akun_kas;
            $transaksi['kredit'] = $data['nominal'];

            $this->jurnalService->createJurnal([
                'trans_id' => $transaksi['trans_id'],
                'trans_kode' => $transaksi['trans_kode'],
                'akun_jurnal' =>  $akun_kas,
                'debet' => 0,
                'kredit' => $data['nominal'],
                'keterangan' => $transaksi['keterangan'],
                'trans_tglbuku' => $transaksi['trans_tglbuku'],
                'trans_date' => $transaksi['trans_date']
            ]);
            $this->jurnalService->createJurnal([
                'trans_id' => $transaksi['trans_id'],
                'trans_kode' => $transaksi['trans_kode'],
                'akun_jurnal' => $data['kode_akun'],
                'debet' => $data['nominal'],
                'kredit' => 0,
                'rekening' => $penempatan->rekening,
                'keterangan' => $transaksi['keterangan'],
                'trans_tglbuku' => $transaksi['trans_tglbuku'],
                'trans_date' => $transaksi['trans_date']
            ]);
        }
        // throw new Exception(json_encode($transaksi));

        return $this->transaksiPenempatanRepository->createTransaksi($transaksi);
    }

    public function validasiTransaksi($id)
    {
        $akun = $this->transaksiPenempatanRepository->findTransaksiById($id);
        if (!$akun) {
            throw new \Exception('Transaksi tidak ditemukan');
        }

        return $this->transaksiPenempatanRepository->validasiTransaksi($id);
    }

    public function deleteTransaksi(string $id)
    {
        $akun = $this->transaksiPenempatanRepository->findTransaksiById($id);
        if (!$akun) {
            throw new \Exception('Transaksi tidak ditemukan');
        }
        return $this->transaksiPenempatanRepository->deleteTransaksi($id);
    }
}
