<?php

namespace App\Http\Controllers;

use App\Models\CompanyBranch;
use App\Models\CompanyChartsOfAccount;
use Illuminate\Http\Request;

class LedgerMappingsController extends Controller
{
    /**Show Page to See all available Ledger Mappings**/
    public function index(){
        $companyDetails = CompanyBranch::where('id', auth()->user()->branch)->first();

        //dd($this->LedgerType('Tdebtos'));
        $data = [
            'Tdebtors' => $this->LedgerType('Tdebtors'),
            'retainedEarnings' => $this->LedgerType('retainedEarnings'),
            'SalaryControl' => $this->LedgerType('SalaryControl'),
            'staffAccount' => $this->LedgerType('staffAccount'),
            'SalaryExpense' => $this->LedgerType('SalaryExpense'),
            'LeaveDays' => $this->LedgerType('LeaveDays'),
            'PAYE' => $this->LedgerType('PAYE'),
            'NSSFExpense' => $this->LedgerType('NSSFExpense'),
            'NHIFExpense' => $this->LedgerType('NHIFExpense'),
            'PensionExpense' => $this->LedgerType('PensionExpense'),
            'WTaxProfessional' => $this->LedgerType('WTaxProfessional'),
            'WTaxRental' => $this->LedgerType('WTaxRental'),
            'SundryCreditors' => $this->LedgerType('SundryCreditors'),
            'OfficeStationary' => $this->LedgerType('OfficeStationary'),
            'OfficeConsumables' => $this->LedgerType('OfficeConsumables'),
        ];

        return view('mapped-ledgers', compact('companyDetails', 'data'));
    }


    /**Show Ledger Mapping Pop-up**/
    public function showMapLedger($id){
        $unMappedLedgers = [];
        if(CompanyChartsOfAccount::firstWhere('mapped', 0)){
            $unMappedLedgers = CompanyChartsOfAccount::where('mapped', 0)->orderBy('acc_name', 'asc')->get();
        }
        return view('libs.map-ledger-account', compact('id', 'unMappedLedgers'));
    }

    /**Perform the Mapping**/
    public function updateMapLedger($id, Request $request):string
    {

        $account = $request['account'];

        $res= 'empty account';
        if(!empty($account)){
            $res = 'empty mappable ledger';
            if($id != ''){
                $init = CompanyChartsOfAccount::where('id', $account)->first();

                $res = 'error';
                if($init->update(
                    [
                        'mapped' => 1,
                        'mapped_id' => $id,
                        'mapped_by' => auth()->user()->id,
                        'mapped_at' => date('Y-m-d')
                    ]
                )){
                    $res = 'success';
                }
            }
        }
        return $res;
    }

    public function LedgerType($id){
        $ledger = CompanyChartsOfAccount::where('mapped_id', $id)->first();
        $user = $date = '-';
        if($ledger){
            if($ledger->mappedBy){
                $user = $ledger->mappedBy->firstName." ".$ledger->mappedBy->lastName;
            }
            $date = date('d/m/Y', strtotime($ledger->mapped_at));
        }

        return [
            'mapped' => $ledger->mapped ?? "-",
            'mappedName' => $ledger->acc_name ?? "-",
            'mappedBy' =>  $user,
            'mappedAt' =>  $date
        ];
    }

    public function mappedLedgerName($type):string
    {
        $name = '';
        if($type == 'Tdebtors'){
            $name = 'TRADE DEBTORS(clients)';
        }elseif ($type == 'retainedEarnings'){
            $name = 'RETAINED EARNINGS';
        }elseif ($type == 'SalaryControl'){
            $name = 'SALARY CONTROL - LIABILITY';
        }elseif ($type == 'staffAccount'){
            $name = 'STAFF CURRENT ACCOUNT';
        }elseif ($type == 'SalaryExpense'){
            $name = 'SALARIES AND WAGES';
        }elseif ($type == 'LeaveDays'){
            $name = 'LEAVE DAYS PROVISION';
        }elseif ($type == 'PAYE'){
            $name = 'PAYE';
        }elseif ($type == 'NSSFExpense'){
            $name = 'NSSF';
        }elseif ($type == 'NHIFExpense'){
            $name = 'NHIF';
        }elseif ($type == 'PensionExpense'){
            $name = 'PENSION';
        }elseif ($type == 'WTaxProfessional'){
            $name = 'WITHHOLDING TAX PROFESSIONAL';
        }elseif ($type == 'WTaxRental'){
            $name = 'WITHHOLDING TAX RENTAL';
        }elseif ($type == 'SundryCreditors'){
            $name = 'SUNDRY CREDITORS(suppliers)';
        }elseif ($type == 'OfficeStationary'){
            $name = 'OFFICE STATIONARY';
        }elseif ($type == 'OfficeConsumables'){
            $name = 'OFFICE CONSUMABLES';
        }
        return $name;
    }
}
