<?php

namespace App\DataTables;

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

class MikrotikSecretDataTable
{
    public function render($request)
    {
        $query = MikrotikSecret::with(['mikrotik', 'customer'])->whereNotNull('customer_id');

        if ($request->filled('mikrotik_id')) {
            $query->where('mikrotik_id', $request->input('mikrotik_id'));
        }
        if ($request->filled('status')) {
            $query->where('status', $request->boolean('status'));
        }

        return DataTables::of($query)
            ->addColumn('customer_code', function (MikrotikSecret $secret) {
                if (! $secret->customer) {
                    return '-';
                }

                $code = e($secret->customer->customer_code);

                return auth()->user()->can('view', $secret->customer)
                    ? '<a class="btn btn-sm btn-outline-success fw-semibold text-nowrap" title="Open Customer Ledger" href="'.route('admin.customers.ledger', $secret->customer).'">'.$code.'</a>'
                    : '<span class="fw-semibold">'.$code.'</span>';
            })
            ->addColumn('customer_name', fn (MikrotikSecret $secret) => $secret->customer?->name ?? '-')
            ->addColumn('mikrotik_name', fn (MikrotikSecret $secret) => $secret->mikrotik?->name ?? 'Unknown')
            ->filterColumn('customer_code', function ($query, $keyword) {
                $query->whereHas('customer', fn ($customer) => $customer
                    ->where('customer_code', 'like', '%'.$keyword.'%'));
            })
            ->filterColumn('customer_name', function ($query, $keyword) {
                $query->whereHas('customer', fn ($customer) => $customer
                    ->where('name', 'like', '%'.$keyword.'%'));
            })
            ->filterColumn('mikrotik_name', function ($query, $keyword) {
                $query->whereHas('mikrotik', fn ($mikrotik) => $mikrotik
                    ->where('name', 'like', '%'.$keyword.'%'));
            })
            ->addColumn('status_control', function (MikrotikSecret $secret) {
                if (! auth()->user()->can('update', $secret)) {
                    return $secret->status ? '<span class="badge bg-success">Enabled</span>' : '<span class="badge bg-danger">Disabled</span>';
                }

                $class = $secret->status ? 'btn-outline-warning' : 'btn-outline-success';
                $label = $secret->status ? 'Disable' : 'Enable';
                $enabled = $secret->status ? 0 : 1;

                return '<button type="button" class="btn btn-sm '.$class.' toggle-secret" data-url="'.route('admin.mikrotik-secrets.status', $secret).'" data-enabled="'.$enabled.'" data-username="'.e($secret->username).'">'.$label.'</button>';
            })
            ->addColumn('actions', fn (MikrotikSecret $secret) => view('admin.mikrotik-secrets.partials.actions', compact('secret'))->render())
            ->rawColumns(['customer_code', 'status_control', 'actions'])
            ->make(true);
    }
}
