<?php

namespace App\Modules\Mobile\Manager;

use App\Models\ProjectSupervisors;
use App\Models\CompanyWorkerTimesheet;
use App\Http\Resources\Mobile\Manager\CheckSupervisorProjectTimeLogResource;
use App\Traits\CreateMobileNotificationTraits;

use Carbon\Carbon; 

use Illuminate\Support\Facades\Auth;

class CheckSupervisorProjectTimeLogModule
{
    use CreateMobileNotificationTraits;

    /**
     * Fetch Items 
     * 
     * @param array $data
     *
     * @return CheckSupervisorProjectTimeLogResource
     */
    public function fetchItems(array $data) 
    {
        $currentDate        = Carbon::parse($data['currentDate'])->toDateString();
        $projectManagerId   = Auth::user()->id; 

        $projects       = ProjectSupervisors::query() 
                            ->with(
                                [
                                        'project', 
                                        'timesheets' => function ($query) use ($currentDate, $data)
                                        {
                                            $query->where('worker_id', '=', $data['supervisorId'])
                                                ->whereDate('created_at', '=', $currentDate);
                                        } 
                                    ]
                                )
                            ->whereRelation('project', 'project_manager_id', '=', $projectManagerId)
                            ->where(
                                [
                                    [ 'user_id',  '=',  $data['supervisorId'] ],
                                    [ 'date_from','<=', $currentDate          ],
                                    [ 'date_to',  '>=', $currentDate          ], 
                                ]
                            )
                            ->get();

        return CheckSupervisorProjectTimeLogResource::collection($projects); 
    }

    /**
     * Decline Item 
     * 
     * @param array $data 
     * @param int   $timesheetId 
     * 
     * @return void 
     */
    public function declineItem(array $data, int $timesheetId) : void
    {
        $item = CompanyWorkerTimesheet::findOrFail($timesheetId); 

        $item->update(
            [
                'request_modification_message' => $data['message'],
                'status'                       => 'request modification',
                'role'                         => 'supervisor',
            ]
        );

        $mobileNotificationData = array(
            'worker_id' => $item->worker_id,              
            'message'   => 'A project manager is requesting to modify your Time Log.',
            'type'      => 'user',
            'link'      => '/company/supervisor/timelogs',
        );

        $this->createMobileNotification($mobileNotificationData);
    }

    /**
     * Approve Item 
     * 
     * @param int   $timesheetId 
     * 
     * @return void 
     */
    public function approveItem( int $timesheetId): void
    {
        $item = CompanyWorkerTimesheet::findOrFail($timesheetId);

        $item->update(
            [
                'status' => 'approved',
            ]
        );

        $mobileNotificationData = array(
            'worker_id' => $item->worker_id,                                   //supervisor id
            'message'   => 'A project manager has approved your Time Log.',
            'type'      => 'user',
            'link'      => '/company/supervisor/timelogs',
        );
        
        $this->createMobileNotification($mobileNotificationData);
    }
}