<?php


namespace App\Http\Controllers\DynamicData;

use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\Plan;
use App\Models\Settings\DataSettings\Bank;
use App\Models\Settings\DataSettings\PaymentMethod;
use App\Models\Settings\RegionalSettings\City;
use App\Models\Settings\RegionalSettings\Nationality;
use App\Models\Settings\RegionalSettings\Region;
use App\Models\Subscription;
use App\Models\User;
use App\Traits\Basics\Select2Ajax;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;

class DynamicSelectController extends Controller
{
    use Select2Ajax;

    public function getRolesAjax(Request $request): JsonResponse
    {
        $search     = $request->input('search', '');
        $page       = $request->input('page', 1);
        $requestName  = $request->input('name');

        $query = Role::query()->select('id', 'name');

        if ($requestName) {
            $query->whereIn('name', (array)$requestName);
            $items = $query->get();
            return response()->json([
                'items' => $items->map(fn($item) => [
                    'id' => $item->name,
                    'text' => $item->name,
                ])->toArray(),
            ]);
        }

        if ($search) {
            $query->where('name', 'like', "%$search%");
        }

        $items = $query->orderBy('name')->paginate(LIMIT_SELECT2, ['*'], 'page', $page);

        return response()->json([
            'items' => $items->map(fn($item) => [
                'id' => $item->name,
                'text' => $item->name,
            ])->toArray(),
            'more' => $items->hasMorePages(),
        ]);
    }

    public function getUsersAjax(Request $request): JsonResponse
    {
        $query = User::query()
            ->select('id', 'name', 'username', 'email', 'mobile', 'role')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['name', 'username', 'email', 'mobile'], ['name'])
        );
    }

    public function getCoachesAjax(Request $request): JsonResponse
    {
        $query = User::query()
            ->select('id', 'name', 'username', 'email', 'mobile', 'role')
            ->where('role', '=','coach')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['name', 'username', 'email', 'mobile'], ['name'])
        );
    }

    public function getNationalitiesAjax(Request $request): JsonResponse
    {
        $query = Nationality::query()
            ->select('id', 'code', 'name_ar', 'name_en')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['code','name_ar', 'name_en'], [LOCALIZE_NAME])
        );
    }

    public function getCountriesCodesAjax(Request $request): JsonResponse
    {
        $query = Nationality::query()
            ->select('id', 'code', 'country_code', 'country_ar', 'country_en')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['code', 'country_code', 'country_ar', 'country_en'], [ 'country_code', app()->getLocale() == 'ar' ? 'country_ar' : 'country_en'])
        );
    }
    public function getCustomersAjax(Request $request): JsonResponse
    {
        $query = Customer::query()
            ->select('id', 'code', 'name')
            ->active();

        $except = request()->input('except');
        if (!empty($except)) {
            $query->whereNot('id', $except);
        }

        return response()->json(
            $this->select2Response($query, $request, ['code', 'name'], ['code', 'name'])
        );
    }

    public function getSubscriptionsAjax(Request $request): JsonResponse
    {
        $query = Subscription::query()
            ->with('customer')
            ->select('id','code','amount','customer_id')
            ->where('status','active');

        return response()->json(
            $this->select2Response(
                $query,
                $request,
                ['code', 'customer.name'],
                ['code', 'customer.name']
            )
        );
    }

    public function getBanksAjax(Request $request): JsonResponse
    {
        $query = Bank::query()
            ->select('id', 'name_ar', 'name_en')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['name_ar', 'name_en'], [LOCALIZE_NAME])
        );
    }

    public function getPlansAjax(Request $request): JsonResponse
    {
        $query = Plan::query()
            ->select('id', 'name_ar', 'name_en', 'price')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['name_ar', 'name_en', 'price'], [LOCALIZE_NAME, 'price'])
        );
    }

    public function getPaymentMethodsAjax(Request $request): JsonResponse
    {
        $query = PaymentMethod::query()
            ->select('id', 'name_ar', 'name_en', 'payment_type')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['name_ar', 'name_en', 'payment_type'], [LOCALIZE_NAME, 'payment_type'])
        );
    }

    public function getRegionsAjax(Request $request): JsonResponse
    {
        $query = Region::query()
            ->select('id', 'name_ar', 'name_en')
            ->where('is_active', true);

        return response()->json(
            $this->select2Response($query, $request, ['name_ar', 'name_en', 'payment_type'], [LOCALIZE_NAME])
        );
    }

    public function getCitiesAjax(Request $request): JsonResponse
    {
        $regionId = request()->input('region_id');

        $query = City::query()
            ->select('id', 'name_ar', 'name_en', 'region_id')
            ->where('is_active', true);

        if (!empty($regionId)) {
            $query->where('region_id', $regionId);
        } else {
            $query = [];
        }

        return response()->json(
            $this->select2Response($query, $request, ['name_ar', 'name_en'], [LOCALIZE_NAME])
        );
    }

}
