<?php

namespace App\Filament\Resources\SourcePaperResource\Pages;

use App\Filament\Resources\SourcePaperResource;
use Filament\Resources\Pages\ViewRecord;

class ViewSourcePaper extends ViewRecord
{
    protected static string $resource = SourcePaperResource::class;
    protected string $view = 'filament.resources.source-paper-resource.pages.view-source-paper';

    public bool $expandAll = true;

    public function getPaperPartsProperty(): array
    {
        $record = $this->record;

        return $record->parts()
            ->withCount('candidates')
            ->orderBy('order')
            ->get()
            ->map(fn ($part) => [
                'id' => $part->id,
                'title' => $part->title ?: '未命名区块',
                'type' => $part->type ?: '未标注题型',
                'question_count' => $part->question_count ?? 0,
                'candidate_count' => $part->candidates_count ?? 0,
                'raw_markdown' => $part->raw_markdown,
                'has_error' => $part->question_count === null,
            ])
            ->toArray();
    }
}
