<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Models\Student;

class KnowledgePointMasterySnapshot extends Model
{
    use HasFactory;

    protected $table = 'knowledge_point_mastery_snapshots';
    protected $primaryKey = 'snapshot_id';
    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'snapshot_id',
        'student_id',
        'paper_id',
        'answer_record_id',
        'analysis_id',
        'mastery_data',
        'current_mastery',
        'overall_mastery',
        'weak_knowledge_points_count',
        'strong_knowledge_points_count',
        'snapshot_time',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'mastery_data' => 'array',
        'current_mastery' => 'array',
        'snapshot_time' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'overall_mastery' => 'decimal:4',
    ];

    public function student(): BelongsTo
    {
        return $this->belongsTo(Student::class, 'student_id', 'student_id');
    }

    public function scopeForStudent($query, string $studentId)
    {
        return $query->where('student_id', $studentId);
    }
}
