<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tests\Traits\DatabaseTransactions;
use App\Cawangan;
use App\Gadai;
use App\Support\v2\Penebusan as V2Penebusan;
use Carbon\Carbon;

class TransactionTest extends TestCase
{
    use DatabaseTransactions;

    public function testTransactionAT(){
        $cawangan = Cawangan::on('mysql7')->where('kodcaw', 'KB2')->first();
        $this->setTransactionConnections([$cawangan['mysql'], 'mysql7']);
        $current = new Carbon();
        $array_test = [
            'norujukan' => 'KB2240327-0009',
            "nopelanggan" => "M03-00133879",
            "jenistebus" => "AT",
            "jbg" => 6.5,
            "kadarupah" => 0.56,
            "pinjaman_lama" => 2000,
            "pinjaman" => 2000,
            "ansuran" => 0,
            "addpinjaman" => 0,
            "bakipinjaman" => 2221,
            "bakiupah" => 221,
            "historygadaian" => "KB2240327-0009",
            "marhun" => "1 R/L,",
            "kuantiti_marhun" => 1,
            "total_berat" => 8.5,
            "kadarkeuntungan" => 0.56,
            "noic" => "980412035872",
            "jenisbayarantebus" => "nottawarruq",
            "teller" => "MOHD KHALID ABDUL RAHMAN",
            "jeniskeuntungan" => "onepercent",
            "nilai_marhun" => 2943.21,
            "jenisAT" => "none",
            "totalbayaran" => 221,
            "bayaran" => 300,
            "bakipelanggan" => 79,
            "nowakil" => 0,
            "total_nilai_marhun" => 3171.265,
            "marhun_detail" => "1 R/L,",
        ];

        // Create a mock request
        $request = new Request($array_test);

        $penebusan_services = new V2Penebusan('KB2',$cawangan);
        $response = $penebusan_services->transaksiPembiayaanSemula('KB2','KB2240327-0009',$request);
        $responseArray = json_decode($response->getContent(), true);
        $this->assertIsArray($responseArray, 'Response is not an array');
        $this->assertEquals('success', $responseArray['result'], 'Result should be success');
        $this->assertEquals(1, $responseArray['data'], 'Data should be 1');

        /* Tebus Assert */
        $this->assertDatabaseHasThroughNorujukan('tebus', [
            'kodcaw'=> "KB2",
            'norujukan'=> "KB2240327-0009",
            'nopelanggan' => "M03-00133879",
            'jenisbyr' => "TN",
            'jenistebus' => "A",
            'glcode' => "0",
            't_tebus' => $current->toDateString(),
            'jbg' => "6.50",
            'kadarupah' => "0.56",
            'nilaimarhun' => "2943.21",
            'pinjaman' => "2000.00",
            'ansuran' => "0.00",
            'addpinjaman' => 0,
            'bakipjm' => "2221",
            'bakiupah' => "221",
            'terima' => "*",
            'kodgl' => "0",
            'nowakil' => 0,
            'rebate' => 0,
            'marhuntebus' => "1 R/L,",
            'historygadaian' => "KB2240327-0009",
            'tarikhbyrn' => $current->toDateString(),
            'teller' => "MOHD KHALID ABDUL RAHMAN",
            'ujrah' => 0,
            'onepercent' => 0,
        ], $cawangan['mysql']);


        $this->assertDatabaseHasThroughNorujukan('gadai', [
            'norujukan' => 'KB2240327-0009',
            'sttebus' => 'T'
        ], $cawangan['mysql']);
    }

    public function testTransactionP(){
        $cawangan = Cawangan::on('mysql7')->where('kodcaw', 'KB2')->first();
        $this->setTransactionConnections([$cawangan['mysql'], 'mysql7']);
        $current = new Carbon();
        $array_test = [
            'norujukan' => 'KB2240327-0009',
            'nopelanggan' => 'M03-00133879',
            'jenistebus' => 'P',
            'jbg' => 6.5,
            'kadarupah' => 0.56,
            'pinjaman_lama' => 2000,
            'pinjaman' => 2010,
            'ansuran' => 0,
            'addpinjaman' => 10,
            'bakipinjaman' => 2221,
            'bakiupah' => 221,
            'history_gadaian' => 'KB2240327-0009',
            'marhun' => '1 R/L,',
            'kuantiti_marhun' => 1,
            'total_berat' => 8.5,
            'kadarkeuntungan' => 0.56,
            "noic" => "980412035872",
            'jenisbayarantebus' => 'tawarruq',
            "teller" => "MOHD KHALID ABDUL RAHMAN",
            'jeniskeuntungan' => 'onepercent',
            'nilai_marhun' => 3171.265,
            // 'jenisAT' => 'Tiada',
            // 'totalbayaran' => 'Tiada',
            'bayaran' => 222,
            'bakipelanggan' => 1,
            'nowakil' => 0, //Sebenar Tiada Kena Add dalam series
            // 'total_nilai_marhun' => 'Tiada',
            'keuntungan_6bln' => 6753.6,
            'keuntungan_sebulan' => 1125.6,
            // 'marhun_detail' => 'Tiada',
        ];

        // Create a mock request
        $request = new Request($array_test);

        $penebusan_services = new V2Penebusan('KB2',$cawangan);
        $response = $penebusan_services->transaksiTambahPembiayaan('KB2','KB2240327-0009',$request);

        $responseArray = json_decode($response->getContent(), true);
        $this->assertIsArray($responseArray, 'Response is not an array');
        $this->assertEquals('success', $responseArray['result'], 'Result should be success');
        $this->assertEquals(1, $responseArray['data'], 'Data should be 1');

        /* Tebus Assert */
        $this->assertDatabaseHasThroughNorujukan('tebus', [
            'kodcaw'=> "KB2",
            'norujukan'=> "KB2240327-0009",
            'nopelanggan' => "M03-00133879",
            'jenisbyr' => "TN",
            'jenistebus' => "P",
            'glcode' => "0",
            't_tebus' => $current->toDateString(),
            'jbg' => "6.50",
            'kadarupah' => "0.56",
            'nilaimarhun' => "2943.21",
            'pinjaman' => "2000.00",
            'ansuran' => "0.00",
            'addpinjaman' => "10.00",
            'bakipjm' => "2221.00",
            'bakiupah' => "221.00",
            'terima' => "*",
            'kodgl' => "0",
            'nowakil' => 0,
            'rebate' => "0.00",
            'marhuntebus' => "1 R/L,",
            'historygadaian' => "KB2240327-0009",
            'tarikhbyrn' => $current->toDateString(),
            'teller' => "MOHD KHALID ABDUL RAHMAN",
            'ujrah' => "91.00",
            'onepercent' => "130.00",
        ], $cawangan['mysql']);

        /* Gadai Assert */

        /* Create Gadai */
        $gadaiTransaction = Gadai::on($cawangan['mysql'])->where('t_gadai','=',$current->toDateString())->orderBy('sirig', 'desc')->first();
        $gadaiTransaction = optional($gadaiTransaction)->toArray();

        /* Gadai Baru */
        $this->assertDatabaseHasThroughNorujukan('gadai',[
            'kodcaw'=>'KB2',
            'norujukan' => $gadaiTransaction['norujukan'],
            'nopelanggan' => "M03-00133879",
            "nokp" => "980412035872",
            'sirig' => $gadaiTransaction['sirig'],
            't_gadai' => $current->toDateString(),
            'sttebus' => '',
            'masa' =>$current->toTimeString(),
            'tagbaru' => 1,
            'nilaimarhun' => "3171.27",
            'berat'=> "0.00",
            'pinjaman' => "2010.00",
            'kadarupah' => "0.56",
            // 'marhun' => "1 R\/L,",
            'percentpinj' => "0.63",
            'teller' => "MOHD KHALID ABDUL RAHMAN",
            'bakipjm' => "2010.00",
            'bakiupah' => "121.50",
            'jbg' => "0.50",
            't_update' => $current->toDateString(),
            'cetak' => 0,
            'upah12' => "243.00",
            'hargajualan' => "2253.00",
            'bakihargajualan' => "2253.00",
            'tagserah' => 0,
            't_matang' => "2025-05-24",
            't_matang1' => "2025-11-24",
            'tempoh' => "0.50",
            'kuantiti' => 0,
            'tagkomuditi' => 0,
            'upahdibayar' => "0.00",
            'tempohbayar' => 0.00,
            'lelong_tawarruq' => 1,
            'kadarujrah' => "0.56",
            'ujrah' => "0.15",
            'onepercent' => "20.10",
            'margin_semasa' => "0.63",
            'tagbaru' => 1,
            'tunggakanujrah' => "0.05",
            'tunggakanonepercent' => "10.05",
        ], $cawangan['mysql']);

        $this->assertDatabaseHasThroughNorujukan('marhun',[

        ]);

    }
}
