<?php

namespace App\Services\Client;

use App\Models\CoachUser;
use App\Models\MemberUser;
use App\Models\CoachLocation;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class CoachService
{
    /**
     * 获取技师列表
     */
    public function getCoachList($latitude, $longitude)
    {
        // 获取当前用户
        $userId = Auth::id();
        $user = MemberUser::findOrFail($userId);
        
        // 检查用户状态
        if ($user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }

        // 获取附近的技师
        $coaches = CoachUser::query()
            ->where('state', 'enable')
            ->where('auth_state', 'passed')
            ->with(['user:id,nickname,avatar,gender'])
            ->with(['location'])
            ->whereHas('location', function($query) use ($latitude, $longitude) {
                // TODO: 实现经纬度范围查询
                $query->where('state', 'enable');
            })
            ->paginate(10);
            
        return $coaches;
    }

    /**
     * 获取技师详情
     */
    public function getCoachDetail($coachId, $latitude, $longitude)
    {
        // 获取当前用户
        $userId = Auth::id();
        $user = MemberUser::findOrFail($userId);
        
        // 检查用户状态
        if ($user->state !== 'enable') {
            throw new \Exception('用户状态异常');
        }

        // 获取技师信息
        $coach = CoachUser::where('id', $coachId)
            ->where('state', 'enable')
            ->where('auth_state', 'passed')
            ->with(['user:id,nickname,avatar,gender'])
            ->with(['location'])
            ->firstOrFail();

        // TODO: 计算距离
        $distance = 0;
        if ($coach->location) {
            // 实现距离计算逻辑
        }

        $coach->distance = $distance;
        
        return $coach;
    }
} 