<?php

namespace App\Filament\Resources\ActivityLogResource\Pages;

use App\Filament\Resources\ActivityLogResource;
use Filament\Infolists;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;

class ViewActivityLog extends ViewRecord
{
    protected static string $resource = ActivityLogResource::class;

    protected function getHeaderActions(): array
    {
        return [
            // 不允许编辑和删除操作
        ];
    }

    public function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                Infolists\Components\Section::make('基本信息')
                    ->schema([
                        Infolists\Components\TextEntry::make('created_at')
                            ->label('操作时间')
                            ->dateTime('Y-m-d H:i:s'),

                        Infolists\Components\TextEntry::make('causer.name')
                            ->label('操作用户')
                            ->default('系统'),

                        Infolists\Components\TextEntry::make('description')
                            ->label('操作类型')
                            ->badge()
                            ->color(fn (string $state): string => match ($state) {
                                'created' => 'success',
                                'updated' => 'info',
                                'deleted' => 'danger',
                                default => 'gray',
                            })
                            ->formatStateUsing(fn (string $state): string => match ($state) {
                                'created' => '创建',
                                'updated' => '更新',
                                'deleted' => '删除',
                                default => $state,
                            }),

                        Infolists\Components\TextEntry::make('subject_type')
                            ->label('操作对象类型')
                            ->formatStateUsing(function (?string $state): string {
                                if (!$state) {
                                    return '-';
                                }
                                $className = class_basename($state);
                                return match ($className) {
                                    'SystemSetting' => '系统设置',
                                    'User' => '用户',
                                    'Document' => '文档',
                                    'Group' => '分组',
                                    'Terminal' => '终端',
                                    'Guide' => '操作指引',
                                    default => $className,
                                };
                            }),

                        Infolists\Components\TextEntry::make('subject_id')
                            ->label('对象ID'),

                        Infolists\Components\TextEntry::make('log_name')
                            ->label('日志名称')
                            ->default('default'),
                    ])
                    ->columns(2),

                Infolists\Components\Section::make('变更详情')
                    ->schema([
                        Infolists\Components\ViewEntry::make('properties')
                            ->label('')
                            ->view('filament.infolists.components.activity-log-diff')
                            ->columnSpanFull()
                            ->visible(fn ($record) => !empty($record->properties)),
                    ])
                    ->collapsible(),
            ]);
    }
}
