<?php

namespace App\Filament\Resources\DocumentResource\Pages;

use App\Filament\Resources\DocumentResource;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Storage;

class EditDocument extends EditRecord
{
    protected static string $resource = DocumentResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\ViewAction::make()
                ->label('查看'),
            Actions\DeleteAction::make()
                ->label('删除'),
        ];
    }

    protected function mutateFormDataBeforeFill(array $data): array
    {
        // 将文件路径设置到 file 字段以便显示
        if (isset($data['file_path'])) {
            $data['file'] = $data['file_path'];
        }
        
        return $data;
    }

    protected function mutateFormDataBeforeSave(array $data): array
    {
        // 如果是全局文档，确保 group_id 为 null
        if ($data['type'] === 'global') {
            $data['group_id'] = null;
        }
        
        // 处理文件更新
        if (isset($data['file']) && $data['file'] !== $this->record->file_path) {
            $filePath = $data['file'];
            
            // 删除旧的 Word 文件
            if ($this->record->file_path && Storage::disk('local')->exists($this->record->file_path)) {
                Storage::disk('local')->delete($this->record->file_path);
            }
            
            // 删除旧的 Markdown 文件
            if ($this->record->markdown_path && Storage::disk('markdown')->exists($this->record->markdown_path)) {
                Storage::disk('markdown')->delete($this->record->markdown_path);
            }
            
            // 获取原始文件名（由于使用了 preserveFilenames()，basename 就是原始文件名）
            $originalFileName = basename($filePath);
            
            // 更新文件信息
            $data['file_path'] = $filePath;
            $data['file_name'] = $originalFileName; // 保存原始文件名
            $data['file_size'] = Storage::disk('local')->size($filePath);
            $data['mime_type'] = Storage::disk('local')->mimeType($filePath);
            
            // 重置转换状态，准备重新转换
            $data['conversion_status'] = 'pending';
            $data['markdown_path'] = null;
            $data['markdown_preview'] = null;
            $data['conversion_error'] = null;
        }
        
        // 移除临时的 file 字段
        unset($data['file']);
        
        return $data;
    }

    protected function afterSave(): void
    {
        // 如果文档的转换状态是 pending，说明文件已更新，需要触发重新转换
        if ($this->record->conversion_status === 'pending') {
            $conversionService = app(\App\Services\DocumentConversionService::class);
            $conversionService->queueConversion($this->record);
        }
    }

    protected function getSavedNotification(): ?Notification
    {
        return Notification::make()
            ->success()
            ->title('文档更新成功')
            ->body('文档信息已成功更新。');
    }
}
