<?php

namespace App\Observers;

use App\Models\Schedule;
use Carbon\Carbon;

class ScheduleObserver
{
    public function updated(Schedule $schedule): void
    {
        if (! $schedule->wasChanged('code_verified') || ! $schedule->code_verified) {
            return;
        }

        if (in_array($schedule->status, ['finished', 'cancelled', 'rejected'], true)) {
            return;
        }

        $endedAt = Carbon::parse($schedule->date->format('Y-m-d').' '.$schedule->end_time);

        // if ($endedAt->isFuture()) {
        //    return;
        // }

        $schedule->forceFill(['status' => 'finished'])->saveQuietly();

        $schedule->provider?->increment('total_services');

        $schedule->client?->increment('total_services');
    }
}
