<?php

namespace App\Filament\Resources\Monitoring\MediaMonitorings\Pages;

use App\Filament\Actions\BackAction;
use App\Filament\Resources\Monitoring\MediaMonitorings\MediaMonitoringResource;
use App\Filament\Support\CheerfulNotification;
use App\Models\MediaMonitoring;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;

class CreateMediaMonitoring extends CreateRecord
{
    protected static string $resource = MediaMonitoringResource::class;

    protected static ?string $title = 'Tambah Monitoring Media';

    protected function getHeaderActions(): array
    {
        return [
            BackAction::make()
                ->url(ListMediaMonitorings::getUrl()),
        ];
    }

    protected function getCreatedNotification(): ?Notification
    {
        return CheerfulNotification::create();
    }

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

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $latestCode = MediaMonitoring::max('code');

        $lastNumber = $latestCode
            ? intval(substr($latestCode, 3))
            : 0;

        $newNumber = str_pad($lastNumber + 1, 6, '0', STR_PAD_LEFT);

        $data['code'] = 'ADN'.$newNumber;

        return $data;
    }
}
