<?php

namespace Modules\User\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use Modules\User\Models\User;
use Modules\User\Requests\Api\BlockProductsRequest;
use Modules\User\Requests\Api\CreateChildrenRequest;
use Modules\User\Requests\Api\EditChildrenRequest;
use Modules\User\Resources\Api\ChildrenResource;
use Modules\User\Services\UserService;
class ChildrenController extends Controller
{

    public function __construct(
        private UserService $userService,
    ) {
    }
    /**
     * Get Childrens.
     *
     * @param Request $request
     * @return void
     */
    public function index(Request $request): void
    {
        $credentials = $request->only('name');
        $credentials['api'] = true;
        $credentials['parent_id'] = auth()->id();
        $query = $this->userService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ChildrenResource::class,
            "returnName" => "childrens"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Get Children.
     *
     * @param Request $request
     * @param User $children
     * @return JsonResponse
     */
    public function show(Request $request, User $children): JsonResponse
    {
        $logedUser = auth()->user();
        $id = $children->id;
        $user = $this->userService->getItem($id);
        if (
            !in_array($logedUser->role, [4, 6]) ||
            ($user->role === 6 && $user->school->canteen->id !== $logedUser->canteen->id) ||
            ($user->role === 4 && $user->parent_id !== auth()->id())
        ) {
            ResponseService::responseFail(['error' => 'not_found']);
        }

        $response = [
            'status' => 200,
            'message' => '',
            'data' => [
                'children' => new ChildrenResource($user),
                'blocked_products' => $user->blockedProducts()->pluck('products.id')
            ]
        ];
        return ResponseService::response($response);
    }
    /**
     * Create Children.
     *
     * @param CreateChildrenRequest $request
     * @return JsonResponse
     */
    public function store(CreateChildrenRequest $request): JsonResponse
    {
        $id = auth()->id();
        $credentials = $request->only('name', 'photo', 'school_id','points_per_day');
        $credentials['parent_id'] = $id;
        $credentials['role'] = 5;
        $credentials['gender'] = $request->gender ?? 0;
        $user = $this->userService->create($credentials);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new ChildrenResource($user)
        ];
        return ResponseService::response($response);
    }
    /**
     * Edit Children.
     *
     * @param EditChildrenRequest $request
     * @return JsonResponse
     */
    public function update(EditChildrenRequest $request): JsonResponse
    {
        $credentials = $request->only('name', 'photo', 'school_id', 'active', 'points_per_day');
        $id = $request->id;
        $credentials['gender'] = $request->gender ?? 0;
        $user = $this->userService->update($credentials, $id);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new ChildrenResource($user)
        ];
        return ResponseService::response($response);
    }

    /**
     * remove Children.
     *
     * @param Request $request
     * @param User $children
     * @return void
     */
    public function delete(Request $request, User $children): void
    {
        if ($children->parent_id !== auth()->id()) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $id = $children->id;
        $this->userService->destroy($id);

        $credentials['api'] = true;
        $credentials['parent_id'] = auth()->id();
        $query = $this->userService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ChildrenResource::class,
            "returnName" => "childrens"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Sync Blocked Products.
     *
     * @param BlockProductsRequest $request
     * @param User $children
     * @return JsonResponse
     */
    public function blockedProducts(BlockProductsRequest $request, User $children): JsonResponse
    {
        if ($children->parent_id !== auth()->id()) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $children->blockedProducts()->sync($request->ids);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => $children->blockedProducts()->pluck('products.id')
        ];
        return ResponseService::response($response);
    }
}
