<?php

namespace Modules\Internal\Transformers;

use App\Helpers\Helper;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use App\Models\Organization;
use App\Models\File;
use App\Models\User;
use App\Models\RequestLogBenefit;
use Carbon\Carbon;

class ReportLogResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $filesGroupByType = $this->files->mapToGroups(function($file) {
            return [Str::slug($file->type, '_') => $file];
        });
        $provider = Organization::where('id', $this->organization_id)->first();
        $documentQty = File::where(['fileable_type' => 'App\Models\RequestLog', 'fileable_id' => $this->id])->get()->toArray();
        $parsedDateTime = Carbon::parse($this->created_at);
        // $parsedDateTime->tz = 'Asia/Makassar';
        $formattedDateTime = $parsedDateTime->format('Y-m-d H:i:s');

        $timeInsertBenefit = RequestLogBenefit::where('request_log_id', $this->id)->first();

        if ($timeInsertBenefit){
            $created_final_at = $timeInsertBenefit->created_at;
            $durationFinalGl = Helper::differenceTime($timeInsertBenefit->created_at, $this->approved_final_log_at);
        } else {
            $durationFinalGl = 0;
            $created_final_at = null;
        }

        $durationGl = Helper::differenceTime($formattedDateTime, $this->approved_at);
        $approveByFgl = '-';
        if ($this->import_system) {
            $approveByFgl = 'Import By Excel';
        } else if ($this->final_log == 1 &&  $this->status_final_log == 'requested'){
            $approveByFgl = '-';
        } else if ($this->final_log == 1) {
            $approveByFgl = Helper::userName($this->approved_final_log_by);
        }

        $data = [
            'id' => $this->id,
            'code' => $this->code,
            'created_at' => $formattedDateTime,
            'created_final_at' => $created_final_at,
            'submission_date' => $this->approved_at,
            'approved_by' => $this->approved_by ? Helper::userName($this->approved_by) : '-',
            'approved_final_log_at' => $this->approved_final_log_at,
            'approved_final_log_by' => $approveByFgl,
            'service_name' => $this->service ? $this->service->name : '',
            'provider' => $provider ? $provider->name : '-',
            'document_qty' => count($documentQty),
            'status' => $this->status ?? '-',
            'status_final_log' => $this->status_final_log ?? '-',
            'member_name' => $this->member->name,
            'member_code' => $this->member->member_id,
            'payment_type' => $this->payment_type,
            'payment_type_name' => $this->payment_type_name,
            'duration_gl' => $durationGl,
            'duration_final_gl' => $this->final_log == 1 ? $durationFinalGl : '-',
            'files_by_type' => $filesGroupByType,
            'time_insert_benefit' => $filesGroupByType,
        ];

        return $data;
    }
}
