<?php

namespace Yajra\DataTables\Services;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

/**
 * Compatibility shim for older DataTable base class name.
 *
 * This class intentionally does NOT extend the newer DataTableAbstract to
 * avoid signature conflicts (render, protected vs public, different args).
 * It implements a minimal API surface expected by the legacy app/DataTables
 * classes so they can operate without fully refactoring to yajra v12.
 */
class DataTable
{
    /**
     * Apply scopes to the given query.
     * In the legacy implementations this usually returns the query as-is.
     *
     * @param  mixed  $query
     * @return mixed
     */
    public function applyScopes($query)
    {
        return $query;
    }

    /**
     * Return the HTML Builder instance from the container if available.
     * Some legacy DataTables call $this->builder() inside html() definitions.
     *
     * @return mixed
     */
    public function builder()
    {
        if (app()->bound('datatables.html')) {
            return app('datatables.html');
        }

        // Fallback: return null if the HTML builder isn't registered.
        return null;
    }

    /**
     * Compatibility render method to support older DataTable service classes.
     * Accepts a view name and an optional data array and returns a rendered view.
     * Older code often calls $dataTable->render('view.name', $data).
     *
     * @param  string  $view
     * @return View
     */
    public function render($viewOrRequest = null, array $data = [])
    {
        if ($viewOrRequest instanceof Request || (is_object($viewOrRequest) && method_exists($viewOrRequest, 'ajax'))) {
            return $this->ajax();
        }

        if (! is_string($viewOrRequest)) {
            return response()->json([]);
        }

        $dataWithTable = array_merge($data, ['dataTable' => $this]);

        return view($viewOrRequest, $dataWithTable);
    }

    /**
     * Minimal make() fallback to return an empty JSON response so that
     * calling ->make() doesn't fatal while a full refactor is pending.
     *
     * @return JsonResponse
     */
    public function make(bool $mDataSupport = true)
    {
        return response()->json([]);
    }
}
