<?php

namespace App\Filament\Resources\TerminalResource\Pages;

use App\Filament\Resources\TerminalResource;
use Filament\Resources\Pages\CreateRecord;

class CreateTerminal extends CreateRecord
{
    protected static string $resource = TerminalResource::class;

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

    protected function getCreatedNotificationTitle(): ?string
    {
        return '终端创建成功';
    }

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        // 提取提示词数据，稍后单独处理
        $this->promptData = $data['prompt'] ?? null;
        unset($data['prompt']);
        
        return $data;
    }

    protected function afterCreate(): void
    {
        // 创建终端后，创建或更新提示词
        if (!empty($this->promptData['prompt_template'])) {
            $this->record->prompt()->create([
                'prompt_template' => $this->promptData['prompt_template'],
                'variables' => $this->promptData['variables'] ?? [],
            ]);
        }
    }

    private ?array $promptData = null;
}
