<?php

declare(strict_types=1);

namespace App\GraphQL\Controllers\List\Playlist;

use App\Actions\Http\Api\List\Playlist\Track\DestroyTrackAction;
use App\Actions\Http\Api\List\Playlist\Track\StoreTrackAction;
use App\Actions\Http\Api\List\Playlist\Track\UpdateTrackAction;
use App\GraphQL\Controllers\BaseController;
use App\GraphQL\Schema\Mutations\Models\List\Playlist\Track\CreatePlaylistTrackMutation;
use App\GraphQL\Schema\Mutations\Models\List\Playlist\Track\UpdatePlaylistTrackMutation;
use App\Models\List\Playlist;
use App\Models\List\Playlist\PlaylistTrack;
use Illuminate\Support\Arr;

/**
 * @extends BaseController<PlaylistTrack>
 */
class PlaylistTrackController extends BaseController
{
    /**
     * @param  array<string, mixed>  $args
     */
    public function store($root, array $args): PlaylistTrack
    {
        $validated = $this->validated($args, CreatePlaylistTrackMutation::class);

        /** @var Playlist $playlist */
        $playlist = Arr::pull($validated, 'playlist');

        $action = new StoreTrackAction();

        return $action->store($playlist, PlaylistTrack::query(), $validated);
    }

    /**
     * @param  array<string, mixed>  $args
     */
    public function update($root, array $args): PlaylistTrack
    {
        $validated = $this->validated($args, UpdatePlaylistTrackMutation::class);

        /** @var PlaylistTrack $track */
        $track = Arr::pull($validated, self::MODEL);

        $action = new UpdateTrackAction();

        return $action->update($track->playlist, $track, $validated);
    }

    /**
     * @param  array<string, mixed>  $args
     */
    public function destroy($root, array $args): array
    {
        /** @var PlaylistTrack $track */
        $track = Arr::get($args, self::MODEL);

        $action = new DestroyTrackAction();

        $message = $action->destroy($track->playlist, $track);

        return [
            'message' => $message,
        ];
    }
}
