<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Twilio\Rest\Client;

class TwilioController extends Controller
{
    public function sendWhatsAppMessage(Request $request)
    {
        // $request->validate([
        //     'to' => 'required',
        //     'message' => 'required',
        // ]);

        $sid = config('twilio.sid');
        $token = config('twilio.auth_token');
        $from = config('twilio.from');
        $twilio = new Client($sid, $token);

        try {
            $message = $twilio->messages
            ->create("whatsapp:+60126194469", // to
                array(
                "from" => $from,
                "contentSid" => "HXb5b62575e6e4ff6129ad7c8efe1f983e",
                "contentVariables" => '{"1":"12/1","2":"3pm"}',
                "body" => "Your Message"
                )
            );

            return response()->json(['status' => 'Message sent successfully', 'message_sid' => $message->sid]);
        } catch (\Exception $e) {
            return response()->json(['status' => 'Failed to send message', 'error' => $e->getMessage()], 500);
        }
    }

    public function handleWhatsAppCallback(Request $request)
    {
        $data = $request->all();
        // Log the callback data
        Log::info('WhatsApp Callback Data:', $data);

        return response()->json(['status' => 'Callback received', 'data' => $data]);
    }
}
