<?php

namespace App\Components\Admin\Users;

use App\Models\User\Role;
use App\Models\User\User;
use Livewire\Component;

class UserEdit extends Component
{
    public User $user;

    public string $name_first;
    public string $name_last;
    public string $email;
    public string $username;
    public ?string $password = null;
    public array $roles = [];

    /** Workspaces this user is allowed to access. Empty = all allowed (null stored). */
    public array $allowedWorkspaces = [];

    /** When true, the allowed_workspaces column is set to null (unrestricted). */
    public bool $allWorkspaces = true;

    public const WORKSPACES = ['ha', 'cpf', 'wills'];

    public function mount(User $user)
    {
        $this->authorize('edit', $user);

        $this->fill([
            'name_first' => $user->name_first,
            'name_last'  => $user->name_last,
            'email'      => $user->email_address,
            'username'   => $user->username,
            'roles'      => $user->roles->pluck('name')->toArray(),
        ]);

        if ($user->allowed_workspaces === null) {
            $this->allWorkspaces      = true;
            $this->allowedWorkspaces  = [];
        } else {
            $this->allWorkspaces      = false;
            $this->allowedWorkspaces  = $user->allowed_workspaces;
        }
    }

    public function save()
    {
        $this->user->update([
            'name_first'         => $this->name_first,
            'name_last'          => $this->name_last,
            'email_address'      => $this->email,
            'username'           => $this->username,
            'allowed_workspaces' => $this->allWorkspaces
                ? null
                : array_values(array_intersect($this->allowedWorkspaces, self::WORKSPACES)),
        ]);

        if ($this->password) {
            $this->user->update([
                'password' => bcrypt($this->password),
            ]);
        }

        $this->user->syncRoles($this->roles);

        $this->dispatch('toast',
            title: 'Saved',
            message: 'Successfully saved data.',
            type: 'success'
        );
    }

    public function render() {
        return view('components.admin.users.user-edit', [
            'allRoles' => Role::orderBy('name')->get(),
        ]);
    }
}
