<?php

/**
 * 文件功能：用户反馈主表 Model
 * 对应 feedback_items 表，管理用户提交的 Bug报告和功能建议
 * 包含7种处理状态、赞同数/评论数冗余统计
 *
 * @author   ChatRoom Laravel
 *
 * @version  1.0.0
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class FeedbackItem extends Model
{
    /**
     * 允许批量赋值的字段
     */
    protected $fillable = [
        'user_id',
        'username',
        'type',
        'title',
        'content',
        'status',
        'admin_remark',
        'votes_count',
        'replies_count',
    ];

    /**
     * 处理状态配置（中文名 + 图标 + Tailwind 颜色）
     */
    public const STATUS_CONFIG = [
        'pending' => ['label' => '待处理',   'icon' => '⏳', 'color' => 'gray'],
        'accepted' => ['label' => '已接受',   'icon' => '✅', 'color' => 'green'],
        'in_progress' => ['label' => '开发中',   'icon' => '🔧', 'color' => 'blue'],
        'fixed' => ['label' => '已修复',   'icon' => '🐛', 'color' => 'emerald'],
        'done' => ['label' => '已完成',   'icon' => '🚀', 'color' => 'emerald'],
        'rejected' => ['label' => '暂不同意', 'icon' => '❌', 'color' => 'red'],
        'shelved' => ['label' => '已搁置',   'icon' => '📦', 'color' => 'orange'],
    ];

    /**
     * 类型配置
     */
    public const TYPE_CONFIG = [
        'bug' => ['label' => '🐛 Bug报告',  'color' => 'rose'],
        'suggestion' => ['label' => '💡 功能建议', 'color' => 'blue'],
    ];

    // ═══════════════ 关联关系 ═══════════════

    /**
     * 关联赞同记录
     */
    public function votes(): HasMany
    {
        return $this->hasMany(FeedbackVote::class, 'feedback_id');
    }

    /**
     * 关联补充评论
     */
    public function replies(): HasMany
    {
        return $this->hasMany(FeedbackReply::class, 'feedback_id')->orderBy('created_at');
    }

    // ═══════════════ 查询作用域 ═══════════════

    /**
     * 按类型筛选
     *
     * @param  string  $type  bug|suggestion
     */
    public function scopeOfType(Builder $query, string $type): Builder
    {
        return $query->where('type', $type);
    }

    /**
     * 按状态筛选
     *
     * @param  string  $status  处理状态
     */
    public function scopeOfStatus(Builder $query, string $status): Builder
    {
        return $query->where('status', $status);
    }

    /**
     * 待处理的反馈（用于后台徽标计数）
     */
    public function scopePending(Builder $query): Builder
    {
        return $query->where('status', 'pending');
    }

    // ═══════════════ 访问器 ═══════════════

    /**
     * 获取状态对应的配置（标签/图标/颜色）
     */
    public function getStatusConfigAttribute(): array
    {
        return self::STATUS_CONFIG[$this->status] ?? self::STATUS_CONFIG['pending'];
    }

    /**
     * 获取状态中文标签
     */
    public function getStatusLabelAttribute(): string
    {
        return $this->status_config['icon'].' '.$this->status_config['label'];
    }

    /**
     * 获取类型中文标签
     */
    public function getTypeLabelAttribute(): string
    {
        return self::TYPE_CONFIG[$this->type]['label'] ?? '📌 其他';
    }

    /**
     * 判断反馈是否在24小时内（用于普通用户自删权限）
     */
    public function getIsWithin24HoursAttribute(): bool
    {
        return $this->created_at->diffInHours(now()) < 24;
    }

    /**
     * 判断当前状态是否为已处理（已修复/已完成/暂不同意/已搁置）
     */
    public function getIsClosedAttribute(): bool
    {
        return in_array($this->status, ['fixed', 'done', 'rejected', 'shelved']);
    }
}
