<?php

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

use App\Filament\Support\SystemNotification;
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
{
    protected function setUp(): void
    {
        parent::setUp();

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

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