<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\api\controller\user;

use app\api\controller\BaseApiController;
use app\service\api\user\AddressService;
use app\validate\api\AddressValidator;
use app\validate\domain\AddressDomainValidator;
use think\Request;


/**
 * C端用户地址管理控制器
 * 地址列表、详情、创建、更新、删除
 * @package app\api\controller\user
 */
class Address extends BaseApiController
{
    /**
     * 地址列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function list(Request $request)
    {
        $result = $this->app->make(AddressService::class)->list($request);
        $httpStatus = (int)($result['_http_status'] ?? 200);
        unset($result['_http_status']);
        return json($result, $httpStatus);
    }


    /**
     * 地址详情
     * @param Request $request
     * @return \think\response\Json
     */
    public function detail(Request $request)
    {
        $result = $this->app->make(AddressService::class)->detail($request);
        $httpStatus = (int)($result['_http_status'] ?? 200);
        unset($result['_http_status']);
        return json($result, $httpStatus);
    }


    /**
     * 创建地址
     * @param Request $request
     * @return \think\response\Json
     */
    public function create(Request $request)
    {
        $validateResult = $this->validateData($request, 'create');
        if ($validateResult !== null) {
            return $validateResult;
        }

        $result = $this->app->make(AddressService::class)->create($request);
        $httpStatus = (int)($result['_http_status'] ?? 200);
        unset($result['_http_status']);
        return json($result, $httpStatus);
    }


    /**
     * 更新地址
     * @param Request $request
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $validateResult = $this->validateData($request, 'update');
        if ($validateResult !== null) {
            return $validateResult;
        }

        $result = $this->app->make(AddressService::class)->update($request);
        $httpStatus = (int)($result['_http_status'] ?? 200);
        unset($result['_http_status']);
        return json($result, $httpStatus);
    }


    /**
     * 删除地址
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = (int)($request->param('id', 0));
        if ($id <= 0) {
            return json(['code' => 1, 'msg' => '']);
        }

        $result = $this->app->make(AddressService::class)->delete($request);
        $httpStatus = (int)($result['_http_status'] ?? 200);
        unset($result['_http_status']);
        return json($result, $httpStatus);
    }


    /**
     * 验证地址数据
     * @param Request $request
     * @param string $scene
     * @return \think\response\Json|null
     */
    private function validateData(Request $request, string $scene)
    {
        try {
            $this->validate($request->post(), AddressValidator::class . '.' . $scene);
        } catch (\think\exception\ValidateException $e) {
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }

        try {
            $this->validateDomain($this->app->make(AddressDomainValidator::class), $request->post(), $scene);
        } catch (\think\exception\ValidateException $e) {
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }

        return null;
    }
}