<?php

namespace App\Filament\Resources\RekapanGuruResource\Pages;

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

class CreateRekapanGuru extends CreateRecord
{
    protected static string $resource = RekapanGuruResource::class;

    protected function getCreatedNotification(): ?Notification
    {
        return null;
    }
    
    protected function afterCreate(): void
    {
        $hari = ucfirst($this->record->jadwal->hari);
        $waktu_mulai = $this->record->jadwal->waktu_mulai->format('H:i');
        $waktu_selesai = $this->record->jadwal->waktu_selesai->format('H:i');
        $waktu = $this->record->jadwal->jam_ke . ' (' . $waktu_mulai . ' - ' . $waktu_selesai . ')';

        $recipient = Auth::user();

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

        Notification::make()
            ->title('Rekapan Guru Berhasil Dibuat')
            ->success()
            ->body("Rekapan guru {$this->record->guru->nama} untuk mapel {$this->record->mapel->nama_mapel} pada kelas {$this->record->kelas->nama_kelas} di hari {$hari} pada jam ke-{$waktu} berhasil dibuat")
            ->sendToDatabase($recipient);
    }

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