<?php

namespace App\Filament\Resources\PengajuanBarangResource\Pages;

use App\Filament\Resources\PengajuanBarangResource;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class CreatePengajuanBarang extends CreateRecord
{
    protected static string $resource = PengajuanBarangResource::class;

    protected function getCreatedNotification(): ?Notification
    {
        return null;
    }
    
    protected function afterCreate(): void
    {
        $formattedJumlah = 'Rp ' . number_format($this->record->jumlah, 0, ',', '.');

        $recipient = Auth::user();

        if (!$recipient) {
            Log::error('No authenticated user available for notification');
            return;
        }

        Notification::make()
            ->title('Pengajuan Barang Berhasil Dibuat')
            ->success()
            ->body("Pengajuan barang {$this->record->nama_barang} dengan total harga {$formattedJumlah} berhasil dibuat")
            ->sendToDatabase($recipient);
    }

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('index');
    }
}
