<?php

namespace App\DataTables;

use App\Services\MikrotikRouterCacheService;
use Yajra\DataTables\Facades\DataTables;

class MikrotikDisconnectDueDataTable
{
    public function __construct(protected MikrotikRouterCacheService $cacheService) {}

    public function render($request)
    {
        $rows = collect($this->cacheService->getDisconnectByDueCustomers())
            ->map(function ($item) {
                return [
                    'customer_id' => $item['id'] ?? null,
                    'customer_code' => $item['customer_code'] ?? null,
                    'customer_name' => $item['customer_name'] ?? null,
                    'router_name' => $item['mikrotik_name'] ?? null,
                    'router_ip' => $item['mikrotik_ip'] ?? null,
                    'identifier' => $item['identifier'] ?? null,
                    'package' => $item['package'] ?? null,
                    'status' => $item['status'] ?? null,
                    'reason' => $item['reason'] ?? null,
                ];
            })
            ->all();

        return DataTables::of($rows)
            ->editColumn('customer_code', fn ($row) => $row['customer_id']
                ? '<a class="btn btn-sm btn-outline-success fw-semibold text-nowrap" title="Open Customer Ledger" href="'.route('admin.customers.ledger', $row['customer_id']).'">'.e($row['customer_code']).'</a>'
                : '—')
            ->rawColumns(['customer_code'])
            ->make(true);
    }
}
