<?php

namespace App\Observers;

use App\Models\Client;
use App\Models\ClientEditLog;
use App\Models\Clientsinfo;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;

class ClientObserver
{

    public function created(Client $client)
    {
        Cache::flush();
    }

    // protected function storeEditLog(Client $client)
    // {
    //     $new_data_client = $client->toArray();
    //     $new_data_clientInfo = $client->clientsinfo->toArray();
    //     $newData = array_merge($new_data_client, ['clientsinfo' => $new_data_clientInfo]);

    //     $oldClientData = $client->getOriginal();
    //     $oldClientInfoData = $client->clientsinfo->getOriginal();
    //     $oldData = array_merge($oldClientData, ['clientsinfo' => $oldClientInfoData]);

    //     $clientEditLog = new ClientEditLog([
    //         'client_id' => $client->id,
    //         'old_data' => json_encode($oldData),
    //         'new_data' => json_encode($newData),
    //         'user_id' => Auth::user()->id
    //     ]);

    //     $clientEditLog->save();
    // }

    public function updated(Client $client)
    {
        // $this->storeEditLog($client);
        Artisan::call('optimize:clear');
    }

    public function deleted(Client $client)
    {
        Artisan::call('optimize:clear');
    }

    public function restored(Client $client)
    {
        Artisan::call('optimize:clear');
    }

    public function forceDeleted(Client $client)
    {
        Artisan::call('optimize:clear');
    }
}
