<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use App\Models\QuestionMeta;
use App\Models\QuestionAsset;
use App\Models\QuestionKpRelation;

class Question extends Model
{
    use HasFactory;

    protected $fillable = [
        'question_code',
        'question_type',
        'kp_code',
        'stem',
        'options',
        'answer',
        'solution',
        'difficulty',
        'source_file_id',
        'source_paper_id',
        'paper_part_id',
        'textbook_id',
        'source',
        'tags',
        'meta',
    ];

    protected $casts = [
        'difficulty' => 'decimal:2',
        'options' => 'array',
        'meta' => 'array',
    ];

    /**
     * 获取难度标签
     */
    public function getDifficultyLabelAttribute(): string
    {
        $normalized = (float) $this->difficulty;
        if ($normalized > 1) {
            $normalized = $normalized / 5;
        }

        return match (true) {
            $normalized <= 0.4 => '基础',
            $normalized <= 0.7 => '中等',
            default => '拔高',
        };
    }

    /**
     * 获取难度颜色
     */
    public function getDifficultyColorAttribute(): string
    {
        $normalized = (float) $this->difficulty;
        if ($normalized > 1) {
            $normalized = $normalized / 5;
        }

        return match (true) {
            $normalized <= 0.4 => 'success',
            $normalized <= 0.7 => 'warning',
            default => 'danger',
        };
    }

    /**
     * 获取来源标签
     */
    public function getSourceLabelAttribute(): string
    {
        if (str_contains($this->source ?? '', 'ai::')) {
            return 'AI 生成';
        }
        if (str_contains($this->source ?? '', 'manual')) {
            return '手工录入';
        }
        return '未知';
    }

    /**
     * 关联的知识点名称
     */
    public function getKnowledgePointNameAttribute(): string
    {
        // TODO: 从知识图谱 API 获取知识点名称
        // 临时返回 kp_code
        return $this->kp_code ?? '未知';
    }

    public function meta(): HasOne
    {
        return $this->hasOne(QuestionMeta::class);
    }

    public function assets(): HasMany
    {
        return $this->hasMany(QuestionAsset::class);
    }

    public function knowledgeRelations(): HasMany
    {
        return $this->hasMany(QuestionKpRelation::class);
    }

    /**
     * 作用域：按知识点过滤
     */
    public function scopeByKpCode($query, string $kpCode)
    {
        return $query->where('kp_code', $kpCode);
    }

    /**
     * 作用域：按难度过滤
     */
    public function scopeByDifficulty($query, float $difficulty)
    {
        return $query->where('difficulty', $difficulty);
    }

    /**
     * 作用域：按难度范围过滤
     */
    public function scopeByDifficultyRange($query, float $min, float $max)
    {
        return $query->whereBetween('difficulty', [$min, $max]);
    }

    /**
     * 作用域：搜索题目
     */
    public function scopeSearch($query, string $search)
    {
        return $query->where(function ($q) use ($search) {
            $q->where('stem', 'like', "%{$search}%")
              ->orWhere('answer', 'like', "%{$search}%")
              ->orWhere('solution', 'like', "%{$search}%")
              ->orWhere('question_code', 'like', "%{$search}%");
        });
    }
}
