<?php

namespace App\Policies;

use App\CustomerLog;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class CustomerLogPolice
{
    use HandlesAuthorization;

    protected $cache_key = 'customerLog_update_auth_';

    public function update(User $user, CustomerLog $customerLog): bool
    {
        $lastOne = cache()->remember($this->cache_key . $user->id, 1, function () use ($user) {
            return CustomerLog::query()->where('user_id', $user->id)->orderByDesc('updated_at')->first();
        });
        return (int)$user->id === (int)$customerLog->user_id && $customerLog->id == $lastOne->id;
    }

    public function destroy(User $user, CustomerLog $customerLog)
    {
        return (int)$user->id === (int)$customerLog->user_id;
    }
}
