<?php

namespace App\Models;

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

class MenuConfig extends Model
{
    use HasFactory;

    protected $table = 'menu_configs';

    protected $fillable = [
        'menu_key',
        'menu_label',
        'menu_group',
        'menu_url',
        'sort_order',
        'is_managed',
        'is_active',
        'description',
    ];

    protected $casts = [
        'is_managed' => 'boolean',
        'is_active' => 'boolean',
        'sort_order' => 'integer',
    ];

    /**
     * 获取所有启用的菜单配置
     */
    public static function getActiveConfigs()
    {
        return self::where('is_active', true)
            ->orderBy('sort_order')
            ->get();
    }

    /**
     * 获取所有启用的受管理菜单配置
     */
    public static function getManagedConfigs()
    {
        return self::where('is_active', true)
            ->where('is_managed', true)
            ->orderBy('sort_order')
            ->get();
    }

    /**
     * 检查菜单是否受管理
     */
    public static function isManaged(string $menuKey): bool
    {
        $config = self::where('menu_key', $menuKey)->first();
        return $config && $config->is_managed && $config->is_active;
    }

    /**
     * 切换菜单管理状态
     */
    public static function toggleManaged(string $menuKey): bool
    {
        $config = self::where('menu_key', $menuKey)->first();
        if ($config) {
            $config->is_managed = !$config->is_managed;
            return $config->save();
        }
        return false;
    }

    /**
     * 初始化默认菜单配置
     */
    public static function initializeDefaultConfigs(): void
    {
        $defaultMenus = [
            'dashboard' => [
                'label' => '仪表盘',
                'group' => '管理',
                'url' => '/admin',
                'sort' => 0,
            ],
            'exam-history' => [
                'label' => '考试历史',
                'group' => '管理',
                'url' => '/admin/exam-history',
                'sort' => 1,
            ],
            'exam-analysis' => [
                'label' => '考试分析',
                'group' => '分析',
                'url' => '/admin/exam-analysis',
                'sort' => 2,
            ],
            'ocr-paper-grading' => [
                'label' => 'OCR试卷批改',
                'group' => '工具',
                'url' => '/admin/ocr-paper-grading',
                'sort' => 3,
            ],
            'intelligent-exam-generation' => [
                'label' => '智能出卷',
                'group' => '工具',
                'url' => '/admin/intelligent-exam-generation',
                'sort' => 4,
            ],
            'knowledge-graph' => [
                'label' => '知识图谱',
                'group' => '分析',
                'url' => '/admin/knowledge-graph',
                'sort' => 5,
            ],
            'student-management' => [
                'label' => '学生管理',
                'group' => '管理',
                'url' => '/admin/student-management',
                'sort' => 6,
            ],
            'teacher-management' => [
                'label' => '老师管理',
                'group' => '管理',
                'url' => '/admin/teacher-management',
                'sort' => 7,
            ],
        ];

        foreach ($defaultMenus as $key => $data) {
            self::updateOrCreate(
                ['menu_key' => $key],
                [
                    'menu_label' => $data['label'],
                    'menu_group' => $data['group'],
                    'menu_url' => $data['url'],
                    'sort_order' => $data['sort'],
                    'is_managed' => true,
                    'is_active' => true,
                ]
            );
        }
    }
}
