<?php

namespace App\Filament\Resources\Master\Users\Actions;

use App\Filament\Support\CheerfulNotification;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Hash;

class ChangePasswordAction extends Action
{
    public static function getDefaultName(): ?string
    {
        return 'changePassword';
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->label('Ganti Kata Sandi')
            ->icon(Heroicon::OutlinedKey)
            ->color('warning')
            ->modalWidth(Width::Medium)
            ->schema([
                TextInput::make('password')
                    ->label('Kata Sandi Baru')
                    ->password()
                    ->required()
                    ->minLength(8)
                    ->revealable(filament()->arePasswordsRevealable())
                    ->default(config('auth.password_default')),
            ])
            ->action(function (User $record, array $data): void {
                $record->update([
                    'password' => Hash::make($data['password']),
                ]);

                CheerfulNotification::success(
                    'Kata sandi berhasil diubah! 🔑',
                    "Kata sandi untuk pengguna {$record->name} telah diperbarui."
                )->send();
            });
    }
}
