<?php

use App\Models\Settings;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Artisan;


function sendCustomResponse(bool $status, $data, $mes, $errors, $code = 200)
{
    $mes = (empty($mes)) ? null : $mes;
    return response()->json([
        'status' => $status,
        'message' => empty($errors) ? $mes : array_values($errors)[0][0],
        'errors' => empty($errors) ? null : $errors,
        'data' => empty($data) ? null : $data,
    ], $code);
}

function sendWhatsappMessage($to, $message)
{
    $request = Http::post("https://noti-fire.com/api/send/message", [
        "device_id" => "44f21fa5-bcf3-446e-bc68-24ef89599147",
        "to" => $to,
        "message" => $message,
    ]);
    $response = $request->json();

    // return $response['success'] ? true : false;
    return (isset($response["success"]) && $response["success"] && $response["message"] === "Message sent") ? true : false;
}

function setting(): mixed
{
    if (!cache()->has('setting')):
        cache()->put('setting', Settings::first());
    endif;
    return cache()->get('setting');
}

function countryCodes()
{
    return [
        [
            'name' => 'Egypt',
            'code' => '+2',
        ]
    ];
}

function CustomPagination($Row)
{
    return [
        'current_page'  => $Row->toArray()['current_page'],
        'first_page_url' => $Row->toArray()['first_page_url'],
        'from'          => $Row->toArray()['from'],
        'last_page'     => $Row->toArray()['last_page'],
        'last_page_url' => $Row->toArray()['last_page_url'],
        'next_page_url' => $Row->toArray()['next_page_url'],
        'path'          => $Row->toArray()['path'],
        'per_page'      => $Row->toArray()['per_page'],
        'prev_page_url' => $Row->toArray()['prev_page_url'],
        'to'            => $Row->toArray()['to'],
        'total'         => $Row->toArray()['total'],
        'links'         => $Row->toArray()['links'],
    ];
}

function BannerScreens()
{
    return [
        'screen1',
        'screen2',
        'screen3',
        'screen4',
    ];
}

function setEnvVal($key, $value)
{
    $path = base_path('.env');

    if (!file_exists($path) || !is_writable($path)) {
        throw new Exception("The .env file does not exist or is not writable.");
    }

    $value = '"' . addslashes($value) . '"'; // دعم القيم التي تحتوي على رموز أو فراغات

    $env = file($path, FILE_IGNORE_NEW_LINES);
    $envUpdated = false;

    foreach ($env as $index => $line) {
        if (strpos($line, "$key=") === 0) {
            $env[$index] = "$key=$value";
            $envUpdated = true;
            break;
        }
    }

    if (!$envUpdated) {
        $env[] = "$key=$value";
    }

    $envContent = implode("\n", $env) . "\n";
    file_put_contents($path, $envContent);

    // تحديث الكاش
    Artisan::call('config:clear');
}


// App Dir
function AppDir()
{
    return app()->getLocale() == 'ar' ? 'rtl' : 'ltr';
}

function AppLang()
{
    return app()->getLocale();
}
