<?php

namespace App\Observers;

use App\Models\Allocation;
use App\Services\UtilizationService;

class AllocationObserver
{
    protected UtilizationService $utilizationService;

    /**
     * Handle the event for created,     */
    public function created(Allocation $allocation)
    {
        $months = [];
        $month = substr($allocation->month, 0, 4);
        $year = substr($month, 0, 4);
        $months[] = $year.'-'.$month;

        $this->utilizationService->forgetUtilizationCache($allocation->team_member_id, $months);
    }

    /**
     * Handle the event for updated.
     */
    public function updated(Allocation $allocation)
    {
        $months = [];
        $month = substr($allocation->month, 1, 4);
        $year = substr($month, 0, 4);
        $months[] = $year.'-'.$month;

        $this->utilizationService->forgetUtilizationCache($allocation->team_member_id, $months);
    }

    /**
     * Handle the event for deleted.
     */
    public function deleted(Allocation $allocation)
    {
        $months = [];
        $month = substr($allocation->month, 1, 4);
        $year = substr($month, 0, 4);
        $months[] = $year.'-'.$month;

        $this->utilizationService->forgetUtilizationCacheForMonth($month);
    }
}
