<?php

namespace App\Http\Controllers;

use Twilio\Rest\Client;
use Illuminate\Http\Request;

class MessagingController extends Controller
{
    protected $twilio;
    protected $accountSid;
    protected $authToken;
    protected $twilioNumber;

    public function __construct()
    {
        $this->accountSid = env('TWILIO_SID');
        $this->authToken = env('TWILIO_AUTH_TOKEN');
        $this->twilioNumber = env('TWILIO_PHONE_NUMBER');

        // Initialize Twilio client
        $this->twilio = new Client($this->accountSid, $this->authToken);
    }

    public function checkIfMMSIsEnabled()
    {
        $phoneNumber = $this->twilio->incomingPhoneNumbers
            ->read(['phoneNumber' => $this->twilioNumber], 1)[0];

        $capabilities = $phoneNumber->capabilities;

        $response = [
            'number' => $phoneNumber->phoneNumber,
            'capabilities' => [
                'voice' => $capabilities->voice,
                'sms'   => $capabilities->sms,
                'mms'   => $capabilities->mms,
            ],
            'mms_enabled' => $capabilities->mms,
        ];

        return response()->json($response);
    }

    public function sendMMS()
    {
        $to = '+918109567672'; // Example: +1234567890

        try {
            $message = $this->twilio->messages->create(
                $to,
                [
                    'from' => $this->twilioNumber,
                    'body' => 'Check out this image!',
                    'mediaUrl' => [
                        'https://s1.1zoom.me/prev/341/340888.jpg'
                    ]
                ]
            );

            return response()->json([
                'status' => 'success',
                'sid' => $message->sid,
                'to' => $to
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 500);
        }
    }
}
