<?php

namespace App\Traits;

use stdClass;
use Carbon\Carbon;
use App\Models\Application\Group;
use App\Models\Application\ZoneReport;
use App\Http\Requests\ZoneReportRequest;
use App\Models\Application\ZoneReportDetail;
use Illuminate\Support\Facades\Log;

trait ZoneReportTrait
{
    use ReportTrait;

    protected function saveZoneReport(ZoneReportRequest $request)
    {
        $zoneReport = $this->createZoneReport($request);

        return $zoneReport;
    }

    private function createZoneReport(ZoneReportRequest $request)
    {
        $zoneReport = new ZoneReport();

        $startDate = Carbon::createFromDate($request->reportdate)->subDays(2)->format('Y-m-d');
        $endDate = Carbon::createFromDate($request->reportdate)->addDays(4)->format('Y-m-d');

        $zoneReport->fill([
            'fk_site' => $request->fk_site,
            'fk_office' => $request->fk_office,
            'fk_executive' => $request->fk_executive,
            'reportdate' => $request->reportdate,
            'extraweek' => 0,
            'fund' => 0,
            'delivery' => 0,
            'incoming' => 0,
            'total_incoming' => 0,
            'commissions' => 0,
            'outgoing' => 0,
            'bills' => 0,
            'sales' => 0,
            'total_outgoing' => 0,
            'total' => 0,
            'fk_usercreator' => $request->user()->id,
        ])->save();

        $groups = Group::whereHas('loans', fn($loans) => $loans->where([
            ['fk_executive', $request->fk_executive],
            ['is_active', true]
        ])->whereHas('payments', fn($payments) => $payments->whereBetween('duedate', [$startDate, $endDate])))->get();

        $zoneReportDetails[] = new ZoneReportDetail;

        foreach ($groups as $group) {
            $rawParameters = new stdClass();

            $rawParameters->fk_site = $zoneReport->fk_site;
            $rawParameters->fk_group = $group->id;
            $rawParameters->fk_office = $zoneReport->fk_office;
            $rawParameters->reportdate = Carbon::createFromDate($request->reportdate);

            $itemGroup = $this->createGroupItem($rawParameters, true);

            $item = new ZoneReportDetail([
                'fk_zone_report' => $zoneReport->id,
                'fk_group' => $group->id,
                'delivery' => $itemGroup->amountdue,
                'failure' => $itemGroup->amountfailed,
                'subtotal' => $itemGroup->amountdue - $itemGroup->amountfailed,
                'recovered' => $itemGroup->amountrecovered,
                'total' => $itemGroup->amountdue - $itemGroup->amountfailed + $itemGroup->amountrecovered,
                'sales' => $itemGroup->salesamount,
                'commissionpercent' => $itemGroup->commissionpercent,
                'commission' => $itemGroup->commissionamount,
                'incoming' => $itemGroup->incomingamount,
                'outgoing' => $itemGroup->outgoingamount,
                'extraweek' => $itemGroup->extraweekamount,
                'fk_usercreator' => $request->user()->id,
            ]);

            $item->save();

            $zoneReport->extraweek += $item->extraweek;
            $zoneReport->delivery += $item->delivery;
            $zoneReport->incoming += $item->incoming;
            $zoneReport->commissions += $item->commission;
            $zoneReport->outgoing += $item->outgoing;
            $zoneReport->sales += $item->sales;

            $zoneReportDetails[] = $item;
        }

        $zoneReport->total_incoming = $zoneReport->delivery + $zoneReport->extraweek + $zoneReport->incoming;
        $zoneReport->total_outgoing = $zoneReport->commissions + $zoneReport->outgoing + $zoneReport->sales;
        $zoneReport->total = $zoneReport->total_incoming - $zoneReport->total_outgoing;

        $zoneReport->update();

        $zoneReport->load(['details']);

        return $zoneReport;
    }
}
