<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Controllers\BaseController;
use App\Modules\Twilio\Services\TwilioServiceInterface;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class SMSControllerRESTv1 extends BaseController
{
    protected $twilioService;

    public function __construct(TwilioServiceInterface $twilioService)
    {
        $this->twilioService = $twilioService;
    }

    public function postSend(Request $request)
    {
        try {
            $result = $this->twilioService->sendSMS(
                $request->get('send_to'),
                $request->get('message')
            );

            if ($result['success']) {
                return response()->json([
                    'success' => true,
                    'message_sid' => $result['message_sid'],
                    'status' => $result['status']
                ], 200);
            } else {
                return response()->json([
                    'success' => false,
                    'error' => $result['error']
                ], 400);
            }
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'error' => 'Failed to deliver the text message: ' . $e->getMessage()
            ], 400);
        }
    }
}
