<?php

declare(strict_types=1);

namespace App\GraphQL\Schema\Fields\Wiki\Artist;

use App\Contracts\GraphQL\Fields\BindableField;
use App\Contracts\GraphQL\Fields\CreatableField;
use App\Contracts\GraphQL\Fields\RequiredOnCreation;
use App\Contracts\GraphQL\Fields\UpdatableField;
use App\GraphQL\Filter\StringFilter;
use App\GraphQL\Schema\Fields\StringField;
use App\Models\Wiki\Artist;
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;

class ArtistSlugField extends StringField implements BindableField, CreatableField, RequiredOnCreation, UpdatableField
{
    public function __construct()
    {
        parent::__construct(Artist::ATTRIBUTE_SLUG, nullable: false);
    }

    public function description(): string
    {
        return 'The URL slug & route key of the resource';
    }

    public function getFilter(): StringFilter
    {
        return new StringFilter($this->name(), $this->getColumn())
            ->useEq();
    }

    /**
     * The resolver to cast the model.
     */
    public function bindResolver(array $args): null
    {
        return null;
    }

    /**
     * @param  array<string, mixed>  $args
     */
    public function getCreationRules(array $args): array
    {
        return [
            'required',
            'max:192',
            'alpha_dash',
            Rule::unique(Artist::class),
        ];
    }

    /**
     * @param  array<string, mixed>  $args
     */
    public function getUpdateRules(array $args): array
    {
        return [
            'sometimes',
            'required',
            'max:192',
            'alpha_dash',
            Rule::unique(Artist::class)->ignore(Arr::get($args, 'id')->getKey()),
        ];
    }
}
