<?php

use App\Livewire\Actions\Logout;
use Illuminate\Support\Facades\Auth;
use Livewire\Volt\Component;

new class extends Component {
    public string $password = '';

    /**
     * Delete the currently authenticated user.
     */
    public function deleteUser(Logout $logout): void
    {
        $this->validate([
            'password' => ['required', 'string', 'current_password'],
        ]);

        tap(Auth::user(), $logout(...))->delete();

        // Add success message to session before redirecting
        session()->flash('account_deleted', 'Your account has been successfully deleted.');

        $this->redirect('/login', navigate: false);
    }
}; ?>

<div class="p-4 shadow bg-white dark:bg-gray-800 rounded-0 mx-auto mt-6" style="max-width: 600px; width: 100%;">
    <div class="mb-5">
        <h3 class="mb-1">{{ __('Delete Account') }}</h3>
        <p class="text-muted">{{ __('Delete your account and all of its resources') }}</p>
    </div>




    <!-- Modal -->
    <div class="">
        <h5 class="">{{ __('Confirm Account Deletion') }}</h5>
    </div>

    <form wire:submit="deleteUser">
        <p class="mb-3">
            {{ __('Once your account is deleted, all of its resources and data will be permanently deleted.') }}
        </p>

        <div class="form-group mb-3">
            <input type="password" wire:model="password" class="form-control rounded-0"
                placeholder="{{ __('Password') }}" required>
            @error('password') <small class="text-danger">{{ $message }}</small> @enderror
        </div>

        <div class="d-flex justify-content-end gap-2">
            <button type="button" class="btn btn-outline-secondary rounded-0" data-bs-dismiss="modal">
                {{ __('Cancel') }}
            </button>
            <button type="submit" class="btn btn-danger rounded-0">
                {{ __('Delete Account') }}
            </button>
        </div>
    </form>
</div>