<?php

namespace App\Traits;

use App\Models\Application\Group;
use App\Models\Application\Loan;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use stdClass;

trait ExportTrait
{
    use UserLoggedTrait;

    private $groupfailurecells = [
        'date' => 'B1',
        'executive' => 'B2',
        'supervisor' => 'B3',
        'group' => "B4",
        'expire' => 'L1',
        'quantity' => 'L3',
        'firstdate' => 'D5',
        'client' => 'A13',
        'weeklypayment' => 'C13',
        'endorsement' => 'T13'
    ];

    protected function getGroupFailureData($request)
    {
        $reportdate = Carbon::parse($request->reportdate)->format('Y-m-d');
        $group = Group::findOrFail($request->fk_group);

        $report = new stdClass();

        $loans = $group->loans()
            ->where([
                ['loandate', $reportdate],
                ['is_active', true]
            ])
            ->with(['client:id,firstname,lastname,secondlastname,address,colony,phonenumber', 'endorsements:id,fk_endorsement'])
            ->withCount('payments')
            ->get()
            ->map(function ($loan) {
                return (object) [
                    'payments_count' => $loan->payments_count,
                    'executive' => explode(' ', trim($loan->executive->fullname))[0],
                    'supervisor' => explode(' ', trim($loan->supervisor->fullname))[0],
                    'client' => [
                        'fullname' => trim($loan->client->firstname . ' ' . $loan->client->lastname . ' ' . $loan->client->secondlastname),
                        'address' => $loan->client->address,
                        'colony' => $loan->client->colony,
                        'phonenumber' => $loan->client->phonenumber,
                    ],
                    'weeklypayment' => $loan->weeklypayment,
                    'endorsement' => optional($loan->endorsements->first(), function ($endorsement) {
                        return [
                            'fullname' => trim($endorsement->endorsement->firstname . ' ' . $endorsement->endorsement->lastname . ' ' . $endorsement->endorsement->secondlastname),
                            'address' => $endorsement->endorsement->address,
                            'colony' => $endorsement->endorsement->colony,
                            'phonenumber' => $endorsement->endorsement->phonenumber,
                        ];
                    }),
                ];
            });

        $report->reportdate = $reportdate;
        
        $report->expiredate = $group->loans()
            ->where([
                ['loandate', $reportdate],
                ['is_active', true]
            ])->with('payments:fk_loan,duedate')->get()
            ->flatMap(function ($loan) {
                return $loan->payments->pluck('duedate');
            })->max();

        $report->expiredate = $report->expiredate ? Carbon::parse($report->expiredate)->format('Y-m-d') : null;
        $report->groupname = strtok($group->promoter->fullname, ' ') . ' ' . $group->groupname;
        $report->quantity = (clone $loans)->sum('weeklypayment');

        $report->total = $group->loans()->where([
            ['loandate', $reportdate],
            ['is_active', true]
        ])->sum('loanamount');

        $report->loans = $loans;

        return $report;
    }
}
