<?php

namespace App\DataTables;

use App\Models\SmsMessage;
use Yajra\DataTables\Facades\DataTables;

class SmsMessageDataTable
{
    public function render($request)
    {
        $query = SmsMessage::with('gateway');

        return DataTables::of($query)
            ->addColumn('gateway_name', function (SmsMessage $message) {
                return $message->gateway?->name ?? '-';
            })
            ->addColumn('status_label', function (SmsMessage $message) {
                return match ($message->status) {
                    'sent' => '<span class="badge bg-success">Sent</span>',
                    'failed' => '<span class="badge bg-danger">Failed</span>',
                    default => '<span class="badge bg-secondary">Pending</span>',
                };
            })
            ->addColumn('sent_at', function (SmsMessage $message) {
                return $message->sent_at?->toDateTimeString() ?? '-';
            })
            ->addColumn('actions', function (SmsMessage $message) {
                return view('admin.sms-messages.partials.actions', compact('message'))->render();
            })
            ->rawColumns(['status_label', 'actions'])
            ->make(true);
    }
}
