<?php

namespace App\Filament\Resources\BackupScheduleResource\Pages;

use App\Filament\Resources\BackupScheduleResource;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class EditBackupSchedule extends EditRecord
{
    protected static string $resource = BackupScheduleResource::class;

    protected function resolveRecord(int|string $key): Model
    {
        $record = BackupScheduleResource::getEloquentQuery()
            ->withTrashed()
            ->find($key);

        if ($record === null) {
            throw (new ModelNotFoundException)->setModel(BackupScheduleResource::getModel(), [$key]);
        }

        return $record;
    }

    protected function mutateFormDataBeforeSave(array $data): array
    {
        $data = BackupScheduleResource::ensurePolicyTypes($data);

        return BackupScheduleResource::hydrateNextRun($data);
    }
}
