<?php

namespace App\Services;

use App\Classes\MikrotikService\SyncWithMk;
use App\Models\Client;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class RadiusClientSync
{
    public function syncSingleRadiusClient($id)
    {
        // dd("comes");
        $client = Client::with('pops.nas', 'packages')->where('id', $id)->first();
        if ($client) {



            $isExpire = (new SyncWithMk())->isExpire($client);


            // i want if client is disable or deactive then $checkActive will be false
            $checkActive = ($client->is_free == 1 || !$isExpire || $client->pops->experity_check == "Yes" || $client->experity_check == "No");

            $checkDisableOrDeactive = $client->clients_status == 'disable' || $client->clients_status == 'deactive';

            $checkEnable = ($checkActive && !$checkDisableOrDeactive);

            // dd($checkActive, !$checkDisableOrDeactive, $checkEnable);

            // check if client is active then add := in reccheck table and also check if client is free or not
            if ($checkEnable) {

                $op = ':=';

                DB::table('radcheck')->updateOrInsert(
                    [
                        'username' => $client->userid,
                        'attribute' => 'Cleartext-Password'
                    ],
                    [
                        'op' => $op,
                        'value' => $client->password
                    ]
                );

                DB::table('radcheck')->updateOrInsert(
                    [
                        'username' => $client->userid,
                        'attribute' => 'NAS-IP-Address'
                    ],
                    [
                        'op' => $op,
                        'value' => $client->pops->nas->nasname
                    ]
                );



                DB::table('radusergroup')->updateOrInsert(
                    ['username' => $client->userid], // where clause
                    [
                        'groupname' => $client->packages->id,
                        'priority' => 1
                    ] // values to update/insert
                );
            } else {
                // check if client is expire then add != in reccheck table and also change client status to expire

                $op = globalPermission('RadiusExpiration') ? ':=' : '!=';



                DB::table('radcheck')->updateOrInsert(
                    [
                        'username' => $client->userid,
                        'attribute' => 'Cleartext-Password'
                    ],
                    [
                        'op' => $op,
                        'value' => $client->password
                    ]
                );

                DB::table('radcheck')->updateOrInsert(
                    [
                        'username' => $client->userid,
                        'attribute' => 'NAS-IP-Address'
                    ],
                    [
                        'op' => $op,
                        'value' => $client->pops->nas->nasname
                    ]
                );




                DB::table('radusergroup')->updateOrInsert(
                    ['username' => $client->userid], // where clause
                    [
                        'groupname' => $client->packages->id,
                        'priority' => 1
                    ] // values to update/insert
                );
            }
        } else {
        }
    }
}
