<?php

namespace App\Repositories;

use App\Models\JurnalTrans;
use App\Models\TransaksiAkuntansi;
use App\Models\TransaksiPenempatanBank;
use App\Services\JurnalService;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class TransaksiKasRepository
{
    protected $transaksi_kas;
    protected $jurnalService;
    protected $transaskPenempatanBank;

    public function __construct(TransaksiAkuntansi $transaksi_kas, TransaksiPenempatanBank $transaskPenempatanBank, JurnalService $jurnalService)
    {
        $this->transaksi_kas = $transaksi_kas;
        $this->jurnalService = $jurnalService;
        $this->transaskPenempatanBank = $transaskPenempatanBank;
    }

    public function getAllTransaksiKas($tgl_awal, $tgl_akhir, $id): Collection
    {
        $transaksi_kas = $this->transaksi_kas->with('akun', 'kas')
            ->whereBetween('trans_tglbuku', [$tgl_awal, $tgl_akhir]);

        if ($id) {
            $transaksi_kas->where('kas_id', $id);
        }

        $transaksi = $transaksi_kas->get()->sortBy('trans_date')->values();

        return $transaksi;
        // return $this->transaksi_kas->with('akun')->get();
    }

    public function getAllTransaksi($tgl_awal, $tgl_akhir): Collection
    {
        $transaksi_kas = $this->transaksi_kas->with('akun', 'kas')
            ->whereBetween('trans_tglbuku', [$tgl_awal, $tgl_akhir])
            ->get();

        $transaskPenempatanBank = $this->transaskPenempatanBank->with('penempatan')
            ->whereBetween('trans_tglbuku', [$tgl_awal, $tgl_akhir])
            ->get();

        $transaksi_kas->merge($transaskPenempatanBank);

        $transaksi = $transaksi_kas->merge($transaskPenempatanBank)->sortBy('trans_date')->values();

        return $transaksi;
        // return $this->transaksi_kas->with('akun')->get();
    }

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

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

    public function createTransaksi(array $data): TransaksiAkuntansi
    {
        $transaksi_kas =  $this->transaksi_kas->create($data);
        $kas = $transaksi_kas->kas;
        $kas->saldo += $transaksi_kas->masuk - $transaksi_kas->keluar;
        $kas->save();
        // throw new Exception($kas);
        return $transaksi_kas;
    }

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

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

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

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

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

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

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