<?php

namespace Eminiarts\Aura\Http\Livewire;

use Eminiarts\Aura\Traits\FieldsOnComponent;
use Eminiarts\Aura\Traits\InputFields;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use LivewireUI\Modal\ModalComponent;

class CreateTaxonomy extends ModalComponent
{
    use FieldsOnComponent;
    use InputFields;

    public $post;

    public static function getFields()
    {
        return [
            [
                'name' => 'Name (Singular, e.g. Tag)',
                'instructions' => 'The name of the taxonomy, shown in the admin panel.',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => 'required|alpha:ascii',
                'slug' => 'name',
            ],
        ];
    }

    public function mount()
    {
        abort_if(app()->environment('production'), 403);

        abort_unless(auth()->user()->resource->isSuperAdmin(), 403);
    }

    public function render()
    {
        return view('aura::livewire.create-taxonomy');
    }

    public function rules()
    {
        return Arr::dot([
            'post.fields' => $this->validationRules(),
        ]);
    }

    public function save()
    {
        abort_unless(auth()->user()->resource->isSuperAdmin(), 403);

        $this->validate();

        Artisan::call('aura:taxonomy', [
            'name' => $this->post['fields']['name'],
        ]);

        Artisan::call('cache:clear');

        $this->notify('Erfolgreich Erstellt.');

        $this->emit('closeModal');
    }
}
