<?php

namespace App\Http\Controllers\Api\Pages;

use App\Http\Controllers\Controller;
use App\Models\Pages\Page;
use App\Models\Publications\Publication;
use App\Services\Filters\FiltersService;
use App\Transformers\Pages\PageTransformer;
use App\Transformers\Publications\PublicationTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PagesController extends Controller {
    protected Page $model;

    public function __construct(Page $model) {
        $this->model = $model;
    }

    public function root(Request $request): JsonResponse {
        return fractal(Page::root($request->get('with_trashed')), new PageTransformer($request->get('with_trashed', false)))->respond();
    }

    public function find(Request $request): ?JsonResponse {
        if ($model = Page::byUrl($request->get('url'))) {
            return fractal($model, new PageTransformer())->respond();
        } elseif ($model = Publication::byUrl($request->get('url'))) {
            return fractal($model, new PublicationTransformer())->respond();
        }
        return null;
    }

    public function index(Request $request): JsonResponse {
        $filters = collect($request->has('filters') ? json_decode($request->get('filters'), true) : [])->filter(function($val) {return $val;});
        $query = $this->model->query();
        if ($request->get('with_trashed')) $query->withTrashed();
        $service = FiltersService::getService('pages');
        $service->applyFilters($query, $filters);
        $paginator = $query->paginate(config('app.pagination_limit'));
        return fractal($paginator, new PageTransformer())->respond();
    }

    public function show(Request $request, $id): JsonResponse {
        $query = $this->model->byUuid($id);
        if ($request->get('with_trashed')) $query->withTrashed();
        $model = $query->firstOrFail();
        return fractal($model, new PageTransformer($request->get('with_trashed', false)))->respond();
    }

    public function move(Request $request, $id): JsonResponse {
        $model = $this->model->byUuid($id)->withTrashed()->firstOrFail();
        $parent = Page::byUuid($request->get('parent'))->withTrashed()->first();
        $model->move($request->get('ord'), $parent);
        return fractal($model, new PageTransformer())->respond();
    }

    public function clone(Request $request, $id): JsonResponse {
        $model = $this->model->byUuid($id)->firstOrFail();
        $parent = $this->model->byUuid($request->get('parent'))->first();
        $clone = $model->clone($parent, $request->get('recursive'));
        return fractal($clone, new PageTransformer())->respond();
    }


    public function store(Request $request): void {
    }


    public function update(Request $request, $uuid): void	{
    }

    public function destroy(Request $request, $uuid): JsonResponse {
        $model = $this->model->byUuid($uuid)->withTrashed()->firstOrFail();
        $model->trashed() ? $model->forceDelete() : $model->delete();
        return response()->json(null, 204);
    }

    public function restore(Request $request, $uuid): JsonResponse {
        $model = $this->model->byUuid($uuid)->withTrashed()->firstOrFail();
        $model->restore();
        $model->checkConflictedProps();
        return fractal($model, new PageTransformer())->respond();
    }

}
