<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Services\Client\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * @group 用户端
 *
 * 订单相关的API接口
 */
class OrderController extends Controller
{
    protected OrderService $service;

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

    /**
     * [订单管理] 订单初始化
     *
     * 初始化订单
     *
     * @authenticated
     *
     * @bodyParam coach_id int required 技师ID. Example: 1
     * @bodyParam area_code string required 区划代码. Example: 370602
     * @bodyParam project_id int required 项目ID. Example: 1
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function initialize(Request $request)
    {
        $data = $request->only(['coach_id', 'area_code', 'project_id']);

        return $this->service->initialize(Auth::user()->id, $data);
    }

    /**
     * [订单管理] 创建订单
     *
     * 创建订单
     *
     * @authenticated
     *
     * @bodyParam project_id int required 项目ID. Example: 1
     * @bodyParam address_id int required 地址ID. Example: 1
     * @bodyParam coach_id int required 技师ID. Example: 6
     * @bodyParam use_balance boolean 使用余额. Example: false
     * @bodyParam service_time datetime required 服务时间. Example: 2024-01-01 10:00:00
     * @bodyParam order_id int 订单ID. Example: null
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function create(Request $request)
    {
        $data = $request->only(['project_id', 'address_id', 'coach_id', 'use_balance', 'order_id', 'service_time']);

        return $this->service->createOrder(Auth::user()->id, $data);
    }

    /**
     * [订单管理] 结束订单
     *
     * 结束订单
     *
     * @authenticated
     *
     * @bodyParam order_id int required 订单ID. Example: 1
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function finish(Request $request)
    {
        $userId = Auth::user()->id;
        $orderId = $request->input('order_id');

        return $this->service->finishOrder($userId, $orderId);
    }

    /**
     * [订单管理] 确认技师离开
     *
     * 确认技师离开
     *
     * @authenticated
     *
     * @bodyParam order_id int required 订单ID. Example: 123
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function confirmLeave(Request $request)
    {
        $userId = Auth::user()->id;
        $orderId = $request->input('order_id');

        return $this->service->confirmLeave($userId, $orderId);
    }

    /**
     * [订单管理] 取消订单
     *
     * 取消订单
     *
     * @authenticated
     *
     * @bodyParam order_id int required 订单ID. Example: 123
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function cancel(Request $request)
    {
        $userId = Auth::user()->id;
        $orderId = $request->input('order_id');

        return $this->service->cancelOrder($userId, $orderId);
    }

    /**
     * [订单管理] 获取订单列表
     *
     * 获取订单列表
     *
     * @authenticated
     *
     * @response {
     *   "status": "success",
     *   "data": []
     * }
     */
    public function list()
    {
        return $this->service->getOrderList(Auth::user()->id);
    }

    /**
     * [订单管理] 获取订单详情
     *
     * 获取订单详情
     *
     * @authenticated
     *
     * @urlParam id required 订单ID. Example: 1
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function detail($id)
    {
        return $this->service->getOrderDetail(Auth::user()->id, $id);
    }

    /**
     * [订单管理] 订单退款
     *
     * 订单退款
     *
     * @authenticated
     *
     * @urlParam id required 订单ID. Example: 1
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function refund($id)
    {
        return $this->service->refundOrder($id);
    }

    /**
     * [订单管理] 获取代理商配置
     *
     * 获取代理商配置
     *
     * @authenticated
     *
     * @bodyParam agent_id int required 代理商ID. Example: 1
     *
     * @response {
     *   "min_distance": 0,
     *   "min_fee": 0,
     *   "per_km_fee": 0
     * }
     */
    public function getAgentConfig(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);
        }
    }

    /**
     * [订单管理] 计算订单金额
     *
     * 计算订单金额
     *
     * @authenticated
     *
     * @bodyParam address_id int required 地址ID. Example: 1
     * @bodyParam coach_id int required 技师ID. Example: 1
     * @bodyParam project_id int required 项目ID. Example: 1
     * @bodyParam agent_id int 代理商ID. Example: 1
     * @bodyParam use_balance boolean 使用余额. Example: 0
     *
     * @response {
     *   "total_amount": 0,
     *   "balance_amount": 0,
     *   "pay_amount": 0,
     *   "coupon_amount": 0,
     *   "tip_amount": 0,
     *   "project_amount": 0,
     *   "delivery_fee": 0
     * }
     */
    public function calculateOrderAmount(Request $request)
    {
        $userId = Auth::user()->id;
        $addressId = $request->input('address_id');
        $coachId = $request->input('coach_id');
        $projectId = $request->input('project_id');
        $agentId = $request->input('agent_id');
        $useBalance = $request->input('use_balance', 0);

        return $this->service->calculateOrderAmount($userId, $addressId, $coachId, $projectId, $agentId, $useBalance);
    }

    /**
     * [订单管理] 加钟
     *
     * 加钟
     *
     * @authenticated
     *
     * @urlParam order_id required 订单ID. Example: 123
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function addTime(Request $request, $orderId)
    {
        $userId = Auth::user()->id;

        return $this->service->addTime($userId, $orderId);
    }

    /**
     * [订单管理] 指定技师
     *
     * 指定技师
     *
     * @authenticated
     *
     * @urlParam order_id required 订单ID. Example: 123
     *
     * @bodyParam coach_id int required 技师ID. Example: 1
     *
     * @response {
     *   "status": "success",
     *   "data": {}
     * }
     */
    public function assignCoach(Request $request, $orderId)
    {
        $userId = Auth::user()->id;
        $coachId = $request->input('coach_id');

        return $this->service->assignCoach($userId, $orderId, $coachId);
    }
}
