<?php

namespace App\Models;

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

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

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

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

                $committee = Committee::find($committeeLanguage->committee_id);
                if ($committee) {
                    $trackingService = app(ModelChangeTrackingService::class);
                    $trackingService->trackCommitteeTitleChanged(
                        $committee,
                        $oldTitle,
                        $newTitle,
                        $committeeLanguage->language_id
                    );
                }
            }
        });
    }
}
