<?php

namespace App\Helpers;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Str;

class WhatsappHelper
{
    public static function approveExpense($phones, $expense_number, $url = null)
    {
        $client = new Client();

        // Ensure the URL is formatted correctly
        if ($url && !preg_match('/^https?:\/\//', $url)) {
            $url = 'https://' . $url;
        }

        $data = [];
        foreach ($phones as $phone) {
            $data[] = [
                'phone' => $phone,
                'message' => 'Pengajuan pengeluaran anda dengan nomor *' . $expense_number . '* telah disetujui. Silahkan buka link berikut untuk melihat detailnya: ' . $url,
                'source' => 'web'
            ];
        }

        $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
            'headers' => [
                'Authorization' => env('WABLAS_TOKEN'),
                'Content-Type' => 'application/json'
            ],
            'json' => [
                'data' => $data
            ]
        ]);

        return $response->getBody()->getContents();
    }

    public static function rejectExpense($phones, $expense_number, $url = null)
    {
        $client = new Client();

        if ($url && !preg_match('/^https?:\/\//', $url)) {
            $url = 'https://' . $url;
        }

        $data = [];
        foreach ($phones as $phone) {
            $data[] = [
                'phone' => $phone,
                'message' => 'Pengajuan pengeluaran anda dengan nomor *' . $expense_number . '* telah ditolak. Silahkan buka link berikut untuk melihat detailnya: ' . $url,
                'source' => 'web'
            ];
        }

        $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
            'headers' => [
                'Authorization' => env('WABLAS_TOKEN'),
                'Content-Type' => 'application/json'
            ],
            'json' => [
                'data' => $data
            ]
        ]);

        return $response->getBody()->getContents();
    }

    public static function finalApprove($phones, $expense_number, $url = null)
    {
        $client = new Client();

        if ($url && !preg_match('/^https?:\/\//', $url)) {
            $url = 'https://' . $url;
        }

        $data = [];
        foreach ($phones as $phone) {
            $data[] = [
                'phone' => $phone,
                'message' => 'Pengajuan pengeluaran anda dengan nomor *' . $expense_number . '* telah ditutup karena telah selesai diproses. Silahkan buka link berikut untuk melihat detailnya: ' . $url,
                'source' => 'web'
            ];
        }

        $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
            'headers' => [
                'Authorization' => env('WABLAS_TOKEN'),
                'Content-Type' => 'application/json'
            ],
            'json' => [
                'data' => $data
            ]
        ]);

        return $response->getBody()->getContents();
    }

    public static function newExpense($phones, $expense_number, $url = null)
    {
        $client = new Client();

        if ($url && !preg_match('/^https?:\/\//', $url)) {
            $url = 'https://' . $url;
        }

        $data = [];
        foreach ($phones as $phone) {
            $data[] = [
                'phone' => $phone,
                'message' => 'Pengajuan pengeluaran baru dengan nomor *' . $expense_number . '* telah dibuat. Silahkan buka link berikut untuk melihat detailnya: ' . $url,
                'source' => 'web'
            ];
        }

        $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
            'headers' => [
                'Authorization' => env('WABLAS_TOKEN'),
                'Content-Type' => 'application/json'
            ],
            'json' => [
                'data' => $data
            ]
        ]);

        return $response->getBody()->getContents();
    }
}
