<?php

declare(strict_types=1);

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

use App\Actions\Http\Api\DestroyAction;
use App\Actions\Http\Api\IndexAction;
use App\Actions\Http\Api\ShowAction;
use App\Actions\Http\Api\StoreAction;
use App\Http\Api\Query\Query;
use App\Http\Controllers\Api\Pivot\PivotController;
use App\Http\Requests\Api\IndexRequest;
use App\Http\Requests\Api\ShowRequest;
use App\Http\Requests\Api\StoreRequest;
use App\Http\Resources\Pivot\Wiki\Collection\AnimeSeriesCollection;
use App\Http\Resources\Pivot\Wiki\Resource\AnimeSeriesJsonResource;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Series;
use App\Pivots\Wiki\AnimeSeries;
use Illuminate\Http\JsonResponse;

class AnimeSeriesController extends PivotController
{
    public function __construct()
    {
        parent::__construct(Anime::class, 'anime', Series::class, 'series');
    }

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

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

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

    /**
     * @param  StoreAction<AnimeSeries>  $action
     */
    public function store(StoreRequest $request, Anime $anime, Series $series, StoreAction $action): AnimeSeriesJsonResource
    {
        $validated = array_merge(
            $request->validated(),
            [
                AnimeSeries::ATTRIBUTE_ANIME => $anime->getKey(),
                AnimeSeries::ATTRIBUTE_SERIES => $series->getKey(),
            ]
        );

        $animeSeries = $action->store(AnimeSeries::query(), $validated);

        return new AnimeSeriesJsonResource($animeSeries, new Query());
    }

    public function show(ShowRequest $request, Anime $anime, Series $series, ShowAction $action): AnimeSeriesJsonResource
    {
        $animeSeries = AnimeSeries::query()
            ->where(AnimeSeries::ATTRIBUTE_ANIME, $anime->getKey())
            ->where(AnimeSeries::ATTRIBUTE_SERIES, $series->getKey())
            ->firstOrFail();

        $query = new Query($request->validated());

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

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

    public function destroy(Anime $anime, Series $series, DestroyAction $action): JsonResponse
    {
        $animeSeries = AnimeSeries::query()
            ->where(AnimeSeries::ATTRIBUTE_ANIME, $anime->getKey())
            ->where(AnimeSeries::ATTRIBUTE_SERIES, $series->getKey())
            ->firstOrFail();

        $action->destroy($animeSeries);

        return new JsonResponse([
            'message' => "Anime '{$series->getName()}' has been detached from Series '{$anime->getName()}'.",
        ]);
    }
}
