<?php

namespace App\DTO;

/**
 * 学情分析数据传输对象
 * 封装学情分析所需的所有数据
 */
class ExamAnalysisDataDto
{
    public function __construct(
        public readonly array $paper,
        public readonly array $student,
        public readonly array $teacher,
        public readonly array $questions,
        public readonly array $mastery,
        public readonly array $examHitKpCodes,
        public readonly array $parentMasteryLevels, // 新增：父节点掌握度数据
        public readonly array $insights,
        public readonly array $recommendations,
        public readonly array $rawAnalysisData = [],
        public readonly ?string $analysisId = null
    ) {}

    /**
     * 从数组创建DTO
     */
    public static function fromArray(array $data): self
    {
        return new self(
            paper: $data['paper'] ?? [],
            student: $data['student'] ?? [],
            teacher: $data['teacher'] ?? [],
            questions: $data['questions'] ?? [],
            mastery: $data['mastery'] ?? [],
            examHitKpCodes: $data['exam_hit_kp_codes'] ?? [],
            parentMasteryLevels: $data['parent_mastery_levels'] ?? [], // 新增：父节点掌握度数据
            insights: $data['insights'] ?? [],
            recommendations: $data['recommendations'] ?? [],
            rawAnalysisData: $data['analysis_data'] ?? [],
            analysisId: $data['analysis_id'] ?? $data['analysisId'] ?? null,
        );
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return [
            'paper' => $this->paper,
            'student' => $this->student,
            'teacher' => $this->teacher,
            'questions' => $this->questions,
            'mastery' => $this->mastery,
            'exam_hit_kp_codes' => $this->examHitKpCodes,
            'parent_mastery_levels' => $this->parentMasteryLevels, // 新增：父节点掌握度数据
            'insights' => $this->insights,
            'recommendations' => $this->recommendations,
            'analysis_data' => $this->rawAnalysisData,
            'analysis_id' => $this->analysisId,
        ];
    }

    /**
     * 获取试卷信息
     */
    public function getPaperInfo(): array
    {
        return [
            'id' => $this->paper['id'] ?? null,
            'name' => $this->paper['name'] ?? '',
            'total_questions' => $this->paper['total_questions'] ?? 0,
            'total_score' => $this->paper['total_score'] ?? 0,
        ];
    }

    /**
     * 获取学生信息
     */
    public function getStudentInfo(): array
    {
        return [
            'id' => $this->student['id'] ?? '',
            'name' => $this->student['name'] ?? '',
            'grade' => $this->student['grade'] ?? '',
            'class' => $this->student['class'] ?? '',
        ];
    }

    /**
     * 获取整体掌握度
     */
    public function getOverallMastery(): float
    {
        return $this->mastery['average'] ?? 0.0;
    }

    /**
     * 获取薄弱知识点
     */
    public function getWeakKnowledgePoints(): array
    {
        return $this->mastery['weak_list'] ?? [];
    }

    /**
     * 获取题目数量
     */
    public function getQuestionCount(): int
    {
        return count($this->questions);
    }

    /**
     * 判断是否有分析数据
     */
    public function hasAnalysisData(): bool
    {
        return !empty($this->insights) || !empty($this->mastery['items']);
    }
}
