<?php

declare(strict_types=1);

namespace App\Nova\Resources\Auth;

use App\Models\Auth\User as UserModel;
use App\Nova\Actions\Auth\User\GivePermissionAction;
use App\Nova\Actions\Auth\User\GiveRoleAction;
use App\Nova\Actions\Auth\User\RevokePermissionAction;
use App\Nova\Actions\Auth\User\RevokeRoleAction;
use App\Nova\Resources\BaseResource;
use Exception;
use Illuminate\Validation\Rule;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\Email;
use Laravel\Nova\Fields\Gravatar;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;
use Laravel\Nova\Query\Search\Column;

/**
 * Class User.
 */
class User extends BaseResource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static string $model = UserModel::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = UserModel::ATTRIBUTE_NAME;

    /**
     * Get the search result subtitle for the resource.
     *
     * @return string|null
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public function subtitle(): ?string
    {
        return (string) data_get($this, UserModel::ATTRIBUTE_EMAIL);
    }

    /**
     * The logical group associated with the resource.
     *
     * @return string
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public static function group(): string
    {
        return __('nova.auth');
    }

    /**
     * Get the displayable label of the resource.
     *
     * @return string
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public static function label(): string
    {
        return __('nova.users');
    }

    /**
     * Get the displayable singular label of the resource.
     *
     * @return string
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public static function singularLabel(): string
    {
        return __('nova.user');
    }

    /**
     * Get the searchable columns for the resource.
     *
     * @return array
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public static function searchableColumns(): array
    {
        return [
            new Column(UserModel::ATTRIBUTE_NAME),
            new Column(UserModel::ATTRIBUTE_EMAIL),
        ];
    }

    /**
     * Determine if this resource uses Laravel Scout.
     *
     * @return bool
     *
     * @noinspection PhpMissingParentCallCommonInspection
     */
    public static function usesScout(): bool
    {
        return false;
    }

    /**
     * Get the fields displayed by the resource.
     *
     * @param  NovaRequest  $request
     * @return array
     *
     * @throws Exception
     */
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make(__('nova.id'), UserModel::ATTRIBUTE_ID)
                ->sortable()
                ->showOnPreview(),

            Gravatar::make()->maxWidth(50)
                ->showOnPreview(),

            Text::make(__('nova.name'), UserModel::ATTRIBUTE_NAME)
                ->sortable()
                ->copyable()
                ->rules(['required', 'max:192', 'alpha_dash'])
                ->showOnPreview()
                ->filterable(),

            Email::make(__('nova.email'), UserModel::ATTRIBUTE_EMAIL)
                ->sortable()
                ->rules(['required', 'email', 'max:192'])
                ->creationRules(Rule::unique(UserModel::TABLE)->__toString())
                ->updateRules(
                    Rule::unique(UserModel::TABLE)
                        ->ignore($request->get('resourceId'), UserModel::ATTRIBUTE_ID)
                        ->__toString()
                )
                ->showOnPreview()
                ->filterable(),

            BelongsToMany::make(__('nova.roles'), UserModel::RELATION_ROLES, Role::class)
                ->filterable(),

            BelongsToMany::make(__('nova.permissions'), UserModel::RELATION_PERMISSIONS, Permission::class)
                ->filterable(),

            Panel::make(__('nova.timestamps'), $this->timestamps())
                ->collapsable(),
        ];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  NovaRequest  $request
     * @return array
     */
    public function actions(NovaRequest $request): array
    {
        return array_merge(
            parent::actions($request),
            [
                (new GivePermissionAction())
                    ->confirmButtonText(__('nova.confirm'))
                    ->cancelButtonText(__('nova.cancel'))
                    ->showOnIndex()
                    ->showOnDetail()
                    ->showInline(),

                (new GiveRoleAction())
                    ->confirmButtonText(__('nova.confirm'))
                    ->cancelButtonText(__('nova.cancel'))
                    ->showOnIndex()
                    ->showOnDetail()
                    ->showInline(),

                (new RevokePermissionAction())
                    ->confirmButtonText(__('nova.confirm'))
                    ->cancelButtonText(__('nova.cancel'))
                    ->showOnIndex()
                    ->showOnDetail()
                    ->showInline(),

                (new RevokeRoleAction())
                    ->confirmButtonText(__('nova.confirm'))
                    ->cancelButtonText(__('nova.cancel'))
                    ->showOnIndex()
                    ->showOnDetail()
                    ->showInline(),
            ]
        );
    }
}
