<?php

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

use App\Filament\Resources\Monitoring\MediaMonitorings\MediaMonitoringResource;
use App\Models\MediaMonitoring;
use Filament\Actions\Action;
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 [
            Action::make('back')
                ->label('Kembali')
                ->url(ListMediaMonitorings::getUrl())
                ->outlined()
                ->color('secondary'),
        ];
    }

    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;
    }
}
