<?php

namespace App\Modules\Mobile\Supervisor;

use App\Http\Resources\Mobile\Supervisor\SupervisorProjectTimeLogResource; 
use App\Models\CompanyWorkerTimesheet;
use App\Models\Projects;
use App\Traits\CreateMobileNotificationTraits;
use App\Traits\UserTraits;

use Carbon\Carbon; 

use Illuminate\Support\Facades\Auth;

class SupervisorProjectTimeLogModule 
{
    //** TRAITS */
    use UserTraits; 
    use CreateMobileNotificationTraits;

    /**
     * Fetch Items. 
     * 
     * @param array $data
     * 
     * @return SupervisorProjectTimeLogResource 
     */
    public function fetchItems (array $data) : mixed
    {
        $currentDate    = Carbon::parse($data['currentDate'])->toDateString();
        
        //! Supervisor Id 
        $workerId       = Auth::user()->id;
        
        $items          = CompanyWorkerTimesheet::query()
                            ->with('project', 'project.company')
                            ->where(
                        [
                                    [ 'worker_id',  '=', $workerId    ],
                                    [ 'date',       '=', $currentDate ], 
                                    [ 'role',       '=', 'supervisor'  ]
                                ]
                            )
                            ->get();

        return SupervisorProjectTimeLogResource::collection($items);
    }

    /**
     * Create Item. 
     * 
     * @param array $data 
     * 
     * @return void 
     */
    public function createItem (array $data) : void 
    {
        $project    =   Projects::query()
                            ->select('id', 'name', 'company_id')
                            ->where('id', '=', $data['projectId'])
                            ->firstOrFail(); 

        CompanyWorkerTimesheet::create(
            [
                'worker_id'    => Auth::user()->id,
                'company_id'   => $project->company_id,
                'date'         => Carbon::parse($data['date']),
                'reference_id' => $project->id,
                'rate'         => $this->getCurrentUserRate(),
                'description'  => "Hours rendered for project {$project->name}",
                'status'       => 'submitted',
                'time_in'      => $data['timeIn'],
                'time_out'     => $data['timeOut'],
                'total_hours'  => $data['totalHours'],
                'type'         => 'project',
                'role'         => 'supervisor'
            ]
        );

        $workerFullName = Auth::user()->first_name . " " . Auth::user()->last_name;

        $mobileNotificationData = array(
            'worker_id' => $this->fetchProjectManagerId($project->id),
            'message'   => $workerFullName . ' submitted a Time Log.',
            'type'      => 'user',
            'link'      => '/company/manager/supervisor/timelog',
        );

        $this->createMobileNotification($mobileNotificationData);
    }

    /**
     * Update Item. 
     * 
     * @param array $data 
     * @param int   $projectId 
     * 
     * @return void 
     */
    public function updateItem(array $data, int $timeLogId): void
    {
        $workerId   =   Auth::user()->id;

        CompanyWorkerTimesheet::query() 
            ->where('id', '=', $timeLogId)
            ->where('worker_id', '=', $workerId)
            ->firstOrFail()
            ->update(
                [
                    'time_in'      => $data['timeIn'],
                    'time_out'     => $data['timeOut'],
                    'total_hours'  => $data['totalHours'],
                    'status'       => 'submitted'
                ]
            ); 
    }

    private function fetchProjectManagerId($projectId)
    {
        $project = Projects::where('id', $projectId)->first();

        return $project->project_manager_id;
    }
}