<?php

namespace App\Filament\Resources\TrialExtensions\Pages;

use App\Filament\Resources\TrialExtensions\TrialExtensionResource;
use App\Models\Subscription;
use Filament\Resources\Pages\CreateRecord;

class CreateTrialExtension extends CreateRecord
{
    protected static string $resource = TrialExtensionResource::class;

    /**
     * Ensure user_id and original_trial_ends_at are always set from subscription before creating
     */
    protected function mutateFormDataBeforeCreate(array $data): array
    {
        // Always ensure user_id and original_trial_ends_at are set from subscription
        if (isset($data['subscription_id'])) {
            $subscription = Subscription::find($data['subscription_id']);
            if ($subscription) {
                $data['user_id'] = $subscription->user_id;
                $data['original_trial_ends_at'] = $subscription->trial_ends_at;
            }
        }

        return $data;
    }
}
