<?php

declare(strict_types=1);

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

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\Song\Collection\PerformanceCollection;
use App\Http\Resources\Wiki\Song\Resource\PerformanceJsonResource;
use App\Models\Wiki\Song\Performance;
use Illuminate\Http\JsonResponse;

class PerformanceController extends BaseController
{
    public function __construct()
    {
        parent::__construct(Performance::class, 'performance');
    }

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

        $performances = $query->hasSearchCriteria()
            ? $action->search($query, $request->schema())
            : $action->index(Performance::query(), $query, $request->schema());

        return new PerformanceCollection($performances, $query);
    }

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

        return new PerformanceJsonResource($performance, new Query());
    }

    public function show(ShowRequest $request, Performance $performance, ShowAction $action): PerformanceJsonResource
    {
        $query = new Query($request->validated());

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

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

    public function update(UpdateRequest $request, Performance $performance, UpdateAction $action): PerformanceJsonResource
    {
        $updated = $action->update($performance, $request->validated());

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

    public function destroy(Performance $performance, DestroyAction $action): PerformanceJsonResource
    {
        $deleted = $action->destroy($performance);

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

    public function restore(Performance $performance, RestoreAction $action): PerformanceJsonResource
    {
        $restored = $action->restore($performance);

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

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

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