<?php

namespace App\Http\Controllers;

use App\Models\Sms;
use Illuminate\Http\Request;

class SmsController extends Controller
{

    public function index()
{     
    // ✅ Fetch SMS records ordered by latest
    $smsRecords = Sms::orderBy('id', 'desc')->paginate(1000);

    // ✅ Dashboard statistics
    $total = Sms::count();
    $failed = Sms::where('status', 0)->count();
    $successful = Sms::where('status', 1)->count();
    $usage = $total > 0 ? round(($successful / $total) * 100, 2) : 0; // ✅ Calculate SMS success rate

    return view('admin.sms', compact('smsRecords', 'total', 'failed', 'successful', 'usage'));
}

    
    

    public function search(Request $request)
    {    
        $smsRecords = Sms::whereBetween('created_at', [
            $request->start_date,
            $request->end_date . ' 23:59:59'
        ]);           

        $smsRecords = $smsRecords->paginate(1000);
        $total = $smsRecords->count();
        return view('admin.sms', compact('smsRecords', 'total'));
    }

    public function searchSms (Request $request)
    {    
        $smsRecords = Sms::where('payment_id', $request->search)->get();
        $total = $smsRecords->count();
        return view('admin.sms', compact('smsRecords', 'total'));
    }

    public function view($id)    {    
        $sms = Sms::find($id);       
      
        return view('admin.smsView', compact('sms'));
    }
}
