<?php

namespace App\Filament\Resources\RekapanSiswaResource\Pages;

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

class CreateRekapanSiswa extends CreateRecord
{
    protected static string $resource = RekapanSiswaResource::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 Siswa Berhasil Dibuat')
            ->success()
            ->body("Rekapan siswa {$this->record->siswa->nama} yang diajar guru {$this->record->guru->nama} pada mapel {$this->record->mapel->nama_mapel} di kelas {$this->record->kelas->nama_kelas} pada hari {$hari} di jam ke-{$waktu} berhasil dibuat")
            ->sendToDatabase($recipient);
    }

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