<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Traits\SendsNotifications;

use Illuminate\Http\Request;

class NotificationController extends Controller
{
    use SendsNotifications;
    //
    public function Walkathoninvitation()
    {
        set_time_limit(0); // Remove the time limit for script execution

        $contacts = DB::table('ContactList')->select('names', 'phone')->get();
        $contacts = DB::table('filedbcontacts')->select('names', 'phone')->get();//bdcontacts
        $n=0;
        foreach ($contacts as $contact) {
           // $name = $contact->names;
            $fullName = $contact->names;
            $firstName = $this->formatFirstName($fullName);
            $phone = $contact->phone;

            // Create the message
            $message = "Hello {$firstName}, this is a test message.";

            $message="Dear {$firstName},\nThe St John #SaveALifeWalkathon 8th June is on. Starting point is at Kenya Railways HQ, off Haille Sellasie Av opposite TUK from 6am for 5KM,10KM or 21KM.\nGet your Tshirt now via PAYBILL 4092409 ACC: WALK. AMOUNT: 1500.\nCall 0721611555 to pick your merchandise.";
            $n=$n+1;
            
           // echo $message.$n."<br>";
           //$Notify = $this->SendNotification($phone, $message);
        }

        echo "Message Sent";

    }

    private function formatFirstName($fullName)
    {
        // Split the full name to get the first name
        $nameParts = explode(' ', trim($fullName));
        $firstName = $nameParts[0];

        // Convert to lowercase and capitalize the first letter
        $formattedFirstName = ucfirst(strtolower($firstName));

        return $formattedFirstName;
    }
}
