<?php

namespace App\Services\Client;

use App\Models\OrderComment;
use App\Models\CoachUser;
use App\Models\MemberUser;
use Illuminate\Support\Facades\Auth;

class CommentService
{
    /**
     * 创建评价
     */
    public function createComment(int $coachId, string $content, int $rating)
    {
        // 获取当前用户
        $userId = Auth::id();
        $user = MemberUser::findOrFail($userId);
        
        // 检查用户状态
        if ($user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }
        
        // 检查技师是否存在
        $coach = CoachUser::findOrFail($coachId);
        
        // 创建评价
        $comment = new OrderComment();
        $comment->user_id = $userId;
        $comment->coach_id = $coachId;
        $comment->content = $content;
        $comment->rating = $rating;
        $comment->state = 'enable';
        $comment->save();
        
        return ['message' => '评价成功'];
    }
    
    /**
     * 获取评价列表
     */
    public function getCommentList(int $coachId)
    {
        // 获取当前用户
        $userId = Auth::id();
        $user = MemberUser::findOrFail($userId);
        
        // 检查用户状态
        if ($user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }
        
        // 获取评价列表
        $comments = OrderComment::where('coach_id', $coachId)
            ->where('state', 'enable')
            ->orderBy('created_at', 'desc')
            ->with(['user:id,nickname,avatar'])
            ->paginate(10);
            
        return $comments;
    }
} 