<?php

namespace App\Modules\Jobseeker;

use App\Http\Resources\JobSeekerNotification\JobSeekerNotificationCollection; 
use App\Models\JobseekerNotification;
use App\Traits\SearchTraits;
use App\Traits\SortingTraits;
use App\Traits\UserTraits;

use Illuminate\Support\Facades\Auth;

class Notification
{

    use UserTraits, SortingTraits, SearchTraits;
    public $userData;

    public function __construct()
    {
        $this->userData = Auth::user();
    }

    public function fetchAllNotification($payload)
    {

        $jobseeker_id = $this->userData->id;

        // sorting
        $sortField = $this->sortField($payload, 'created_at'); // first_name = default column to search
        $sortOrder = $this->sortOrder($payload, 'desc');

        $company_name = $this->searchField($payload->company_name);
        $notification_id = $payload->id;

        return $notif = JobseekerNotification::select(
            'jobseeker_notifications.*',
            'companies.name as company_name'
        )
            ->leftJoin('companies', 'companies.id', '=', 'jobseeker_notifications.company_id')
            ->when(!empty($company_name), function ($query) use ($company_name) {
                return $query->where(function ($query) use ($company_name) {
                    $query->where('companies.name', 'LIKE', '%' . $company_name . '%');
                });
            })
            ->when(!empty($notification_id), function ($query) use ($notification_id) {
                return $query->where(function ($query) use ($notification_id) {
                    $query->where('jobseeker_notifications.id', $notification_id);
                });
            })
            ->where('jobseeker_notifications.worker_id', $jobseeker_id)
            ->orderBy($sortField, $sortOrder)
            ->paginate(20);
    }

    public function fetchNotificationInPaginate($payload)
    {
        $jobseeker_id = $this->userData->id;
        $sortField = $this->sortField($payload, 'created_at'); // created_at = default column to search
        $sortOrder = $this->sortOrder($payload, 'desc');

        $notifications = JobseekerNotification::select(
            'jobseeker_notifications.id',
            'jobseeker_notifications.description',
            'jobseeker_notifications.is_read',
            'jobseeker_notifications.created_at',
            'jobseeker_notifications.type',
            'companies.name',
        )
            ->join('companies', 'companies.id', '=', 'jobseeker_notifications.company_id')
            ->where('jobseeker_notifications.worker_id', $jobseeker_id)
            ->orderBy($sortField, $sortOrder)
            ->paginate(20);

        return (new JobSeekerNotificationCollection($notifications))
            ->additional(
                [
                    'meta' =>
                    [
                        'total_unread_notifications' => $this->getTotalUnreadNotification($jobseeker_id),
                    ]
                ]
            )
            ->response()
            ->setStatusCode(201);
    }

    public function getTotalUnreadNotification($jobseeker_id)
    {
        return JobseekerNotification::where('jobseeker_notifications.worker_id', $jobseeker_id)
            ->where('is_read', 0)
            ->get()
            ->count();
    }

    public function isRead($payload){
        
        $notif_id = $payload->id;
        $notif = JobseekerNotification::where('id', $payload->id)->update(['is_read' => true]);
        if($notif) {
            return array(
                'status' => 'success'
            );
        }
        else {
            return array(
                'status' => 'error'
            );
        }
    }
}
