<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class WhatsAppChatController extends Controller
{
    public function showForm(){
        return view('whatsapp');
    }
    public function sendWhatsAppMessage(Request $request)
    {
        $phone = $request->input('phone');

        $response = Http::withToken(env('WHATSAPP_TOKEN'))
            ->post('https://graph.facebook.com/v22.0/' . env('WHATSAPP_PHONE_NUMBER_ID') . '/messages', [
                'messaging_product' => 'whatsapp',
                'to' => $phone,
                'type' => 'template',
                'template' => [
                    'name' => 'hello_world',
                    'language' => ['code' => 'en_US']
                ]
            ]);

        if ($response->successful()) {
            return back()->with('success', 'Message sent successfully!');
        } else {
            return back()->with('error', 'Failed to send message: ' . $response->body());
        }
    }
}
