<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class StudentReport extends Model
{
    use HasFactory;

    protected $table = 'student_reports';

    protected $fillable = [
        'student_id',
        'report_type',
        'pdf_url',
        'file_name',
        'file_size',
        'generation_status',
        'paper_id',
        'report_title',
        'report_data',
        'generated_at',
        'expires_at',
        'notes',
    ];

    protected $casts = [
        'report_data' => 'array',
        'generated_at' => 'datetime',
        'expires_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * 获取报告所属学生
     */
    public function student(): BelongsTo
    {
        return $this->belongsTo(Student::class, 'student_id', 'student_id');
    }

    /**
     * 报告类型常量
     */
    const REPORT_TYPE_MASTERY_ANALYSIS = 'mastery_analysis';
    const REPORT_TYPE_EXAM_REPORT = 'exam_report';
    const REPORT_TYPE_LEARNING_PROGRESS = 'learning_progress';

    /**
     * 生成状态常量
     */
    const STATUS_PENDING = 'pending';
    const STATUS_COMPLETED = 'completed';
    const STATUS_FAILED = 'failed';

    /**
     * 作用域：仅获取已完成的报告
     */
    public function scopeCompleted($query)
    {
        return $query->where('generation_status', self::STATUS_COMPLETED);
    }

    /**
     * 作用域：按报告类型筛选
     */
    public function scopeOfType($query, string $type)
    {
        return $query->where('report_type', $type);
    }
}
