<?php

namespace App\Http\Controllers\Reports;
use App\Cawangan;
use App\Gadai;
use App\Helper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;

class StokAuditController extends Controller
{

    public function getActiveCawanganConnections()
    {
        $cawangan_details = Cawangan::select(['details_caw', 'mysql'])->get();

        $cawangan_details = $cawangan_details->filter(fn($x) => !in_array(strtolower($x->details_caw), config('report.excluded_cawangan')));

        $connection_active_cawangan = Helper::getActiveCawanganDbConnection();

        return collect($cawangan_details->filter(fn($x) => in_array($x->mysql, $connection_active_cawangan)));

    }

    public function buildStokAuditModel($month)
    {
        // Create the class name dynamically based on the number
        $className = 'App\StokAudit' . $month;

        // Ensure the class exists
        if (!class_exists($className)) {
            abort(404);
        }

        $model = new $className();

        return $model;
    }

    public function getSummaryLaporanStokAudit(Request $request)
    {
        $connections = $this->getActiveCawanganConnections();

        // MODE 1: TARIKH SEMASA
        if ($request->dateOption === 'current') {

            $today = Carbon::today();

            $total_audit = $connections->map(function ($connection) {

                $data = Gadai::on($connection->mysql)
                ->where('tagpalsu', 0)
                ->where(function($q) {
                    $q->where('sttebus', '')
                    ->orWhereNull('sttebus');
                })
                ->get();

                return [
                    'cawangan' => $connection->details_caw,
                    'bilangan_pembiayaan' => $data->count(),
                    'berat' => $data->sum('berat'),
                    'nilai_marhun' => $data->sum('nilaimarhun'),
                    'baki_pembiayaan' => $data->sum('bakipjm'),
                    'pembiayaan' => $data->sum('pinjaman'),
                    'upah_belum_bayar' => $data->sum('upahblmbyar'),
                    'total_utg_onepercent' => 0,
                    'total_ujrah' => $data->sum('ujrah'),
                ];
            });

        } 
        // MODE 2: BULAN TERTENTU
        else {

        $month = $request->month;
        $year = $request->year;

        $stokAudit = $this->buildStokAuditModel($month);
        $connections = $this->getActiveCawanganConnections();

        $total_audit = $connections->map(function ($connection) use ($stokAudit, $year) {
            $data = $stokAudit->on($connection->mysql)->where('tahun', $year)->get();

            return [
                'cawangan' => $connection->details_caw,
                'bilangan_pembiayaan' => $data->count(),
                'berat' => $data->sum('berat'),
                'nilai_marhun' => $data->sum('nilaimarhun'),
                'baki_pembiayaan' => $data->sum('bakipjm'),
                'pembiayaan' => $data->sum('pinjaman'),
                'upah_belum_bayar' => $data->sum('upahblmbyar'),
                'total_utg_onepercent' => $data->sum('utgonepercent'),
                'total_ujrah' => $data->sum('ujrah'),
            ];
        });
        }

        // TOTAL SEMUA CAWANGAN
        $total_all = [
            'cawangan' => 'Total Semua Cawangan',
            'bilangan_pembiayaan' => $total_audit->sum('bilangan_pembiayaan'),
            'berat' => $total_audit->sum('berat'),
            'nilai_marhun' => $total_audit->sum('nilai_marhun'),
            'baki_pembiayaan' => $total_audit->sum('baki_pembiayaan'),
            'pembiayaan' => $total_audit->sum('pembiayaan'),
            'upah_belum_bayar' => $total_audit->sum('upah_belum_bayar'),
            'total_utg_onepercent' => $total_audit->sum('total_utg_onepercent'),
            'total_ujrah' => $total_audit->sum('total_ujrah'),
        ];

        $total_audit->push($total_all);

        return response()->json($total_audit, 200);
    }

}
