<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api\Wiki\Video;

use App\Actions\Http\Api\DestroyAction;
use App\Actions\Http\Api\ForceDeleteAction;
use App\Actions\Http\Api\IndexAction;
use App\Actions\Http\Api\RestoreAction;
use App\Actions\Http\Api\ShowAction;
use App\Actions\Http\Api\StoreAction;
use App\Actions\Http\Api\UpdateAction;
use App\Http\Api\Query\Query;
use App\Http\Controllers\Api\BaseController;
use App\Http\Requests\Api\IndexRequest;
use App\Http\Requests\Api\ShowRequest;
use App\Http\Requests\Api\StoreRequest;
use App\Http\Requests\Api\UpdateRequest;
use App\Http\Resources\Wiki\Video\Collection\ScriptCollection;
use App\Http\Resources\Wiki\Video\Resource\ScriptJsonResource;
use App\Models\Wiki\Video\VideoScript;
use Illuminate\Http\JsonResponse;

class ScriptController extends BaseController
{
    public function __construct()
    {
        parent::__construct(VideoScript::class, 'videoscript');
    }

    public function index(IndexRequest $request, IndexAction $action): ScriptCollection
    {
        $query = new Query($request->validated());

        $resources = $action->index(VideoScript::query(), $query, $request->schema());

        return new ScriptCollection($resources, $query);
    }

    /**
     * @param  StoreAction<VideoScript>  $action
     */
    public function store(StoreRequest $request, StoreAction $action): ScriptJsonResource
    {
        $script = $action->store(VideoScript::query(), $request->validated());

        return new ScriptJsonResource($script, new Query());
    }

    public function show(ShowRequest $request, VideoScript $videoscript, ShowAction $action): ScriptJsonResource
    {
        $query = new Query($request->validated());

        $show = $action->show($videoscript, $query, $request->schema());

        return new ScriptJsonResource($show, $query);
    }

    public function update(UpdateRequest $request, VideoScript $videoscript, UpdateAction $action): ScriptJsonResource
    {
        $updated = $action->update($videoscript, $request->validated());

        return new ScriptJsonResource($updated, new Query());
    }

    public function destroy(VideoScript $videoscript, DestroyAction $action): ScriptJsonResource
    {
        $deleted = $action->destroy($videoscript);

        return new ScriptJsonResource($deleted, new Query());
    }

    public function restore(VideoScript $videoscript, RestoreAction $action): ScriptJsonResource
    {
        $restored = $action->restore($videoscript);

        return new ScriptJsonResource($restored, new Query());
    }

    public function forceDelete(VideoScript $videoscript, ForceDeleteAction $action): JsonResponse
    {
        $message = $action->forceDelete($videoscript);

        return new JsonResponse([
            'message' => $message,
        ]);
    }
}
