<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
use Netflie\WhatsAppCloudApi\Message\Template\Component;

class MessageController extends Controller
{
    /**
     * POST /api/v1/messages/send-text?token=
     *
     * Body: { "to": "919876543210", "message": "Hello!" }
     */
    public function sendText(Request $request)
    {
        $request->validate([
            'to'      => ['required', 'string'],
            'message' => ['required', 'string'],
        ]);

        $user = $request->user();

        if (!$user->access_token || !$user->phone_number_id) {
            return response()->json([
                'success' => false,
                'message' => 'WhatsApp not configured. Set access_token and phone_number_id in WhatsApp Setup.',
            ], 422);
        }

        try {
            $whatsapp = new WhatsAppCloudApi([
                'from_phone_number_id' => $user->phone_number_id,
                'access_token'         => $user->access_token,
            ]);

            $response = $whatsapp->sendTextMessage($request->to, $request->message);

            return response()->json([
                'success'  => true,
                'message'  => 'Message sent.',
                'response' => json_decode($response->body(), true),
            ]);
        } catch (\Throwable $e) {
            Log::error('API sendText error', ['user_id' => $user->id, 'error' => $e->getMessage()]);
            return response()->json([
                'success' => false,
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    /**
     * POST /api/v1/messages/send-template?token=
     *
     * Body: { "to": "919876543210", "template": "hello_world", "language": "en_US" }
     */
    public function sendTemplate(Request $request)
    {
        $request->validate([
            'to'       => ['required', 'string'],
            'template' => ['required', 'string'],
            'language' => ['sometimes', 'string'],
        ]);

        $user = $request->user();

        if (!$user->access_token || !$user->phone_number_id) {
            return response()->json([
                'success' => false,
                'message' => 'WhatsApp not configured. Set access_token and phone_number_id in WhatsApp Setup.',
            ], 422);
        }

        try {
            $whatsapp = new WhatsAppCloudApi([
                'from_phone_number_id' => $user->phone_number_id,
                'access_token'         => $user->access_token,
            ]);

            $component = new Component([], []);
            $response  = $whatsapp->sendTemplate(
                $request->to,
                $request->template,
                $request->input('language', 'en_US'),
                $component
            );

            return response()->json([
                'success'  => true,
                'message'  => 'Template sent.',
                'response' => json_decode($response->body(), true),
            ]);
        } catch (\Throwable $e) {
            Log::error('API sendTemplate error', ['user_id' => $user->id, 'error' => $e->getMessage()]);
            return response()->json([
                'success' => false,
                'message' => $e->getMessage(),
            ], 500);
        }
    }
}
