<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Services\Client\CommonService;
use Illuminate\Http\Request;

/**
 * @group 用户端
 *
 * 公共相关的API接口
 */
class CommonController extends Controller
{
    protected CommonService $service;

    public function __construct(CommonService $service)
    {
        $this->service = $service;
    }

    /**
     * [公共管理] 获取代理商配置
     *
     * 获取指定代理商的配置信息
     *
     * @authenticated
     *
     * @bodyParam agent_id int required 代理商ID. Example: 1
     *
     * @response {
     *   "min_distance": 0,
     *   "min_fee": 0,
     *   "per_km_fee": 0
     * }
     */
    public function getConfig(Request $request)
    {
        $agentId = $request->input('agent_id');

        return $this->service->getAgentConfig($agentId);
    }

    /**
     * [公共管理] 获取技师配置
     *
     * 获取指定技师的配置信息
     *
     * @authenticated
     *
     * @bodyParam coach_id int required 技师ID. Example: 1
     *
     * @response {
     *   "delivery_fee_type": "round_trip",
     *   "charge_delivery_fee": true
     * }
     */
    public function getCoachConfig(Request $request)
    {
        $coachId = $request->input('coach_id');

        return $this->service->getCoachConfig($coachId);
    }

    /**
     * [公共管理] 计算路费金额
     *
     * 根据距离和其他参数计算路费
     *
     * @authenticated
     *
     * @bodyParam coach_id int required 技师ID. Example: 1
     * @bodyParam agent_id int 可选 代理商ID. Example: 1
     * @bodyParam distance float required 距离. Example: 10.5
     * @bodyParam latitude float 纬度. Example: 34.0522
     * @bodyParam longitude float 经度. Example: -118.2437
     *
     * @response {
     *   "fee": 15.75
     * }
     */
    public function calculateDeliveryFee(Request $request)
    {
        $coachId = $request->input('coach_id');
        $agentId = $request->input('agent_id');
        $distance = $request->input('distance');
        $latitude = $request->input('latitude');
        $longitude = $request->input('longitude');

        if ($agentId) {
            return $this->service->calculateDeliveryFee($coachId, $agentId, $distance);
        } else {
            return $this->service->calculateDeliveryFeeByLocation($coachId, $latitude, $longitude, $distance);
        }
    }
}
