<?php

namespace App\Models;

use App\Libs\Database\Eloquent\TranslationModel;
use App\Services\MicrosoftGraph\ModelChangeTrackingService;

class WorkGroupLanguage extends TranslationModel
{
    protected $fillable = ['description'];

    /**
     * The "booting" method of the model.
     */
    protected static function boot()
    {
        parent::boot();

        static::updating(function ($workGroupLanguage) {
            // Track title changes
            if ($workGroupLanguage->isDirty('title')) {
                $oldTitle = $workGroupLanguage->getOriginal('title');
                $newTitle = $workGroupLanguage->title;

                $workGroup = WorkGroup::find($workGroupLanguage->work_group_id);
                if ($workGroup) {
                    $trackingService = app(ModelChangeTrackingService::class);
                    $trackingService->trackWorkGroupTitleChanged(
                        $workGroup,
                        $oldTitle,
                        $newTitle,
                        $workGroupLanguage->language_id
                    );
                }
            }
        });
    }
}
