<?php

namespace App\Controller;

use App\Constants\HttpEnumCode;
use App\Request\PatientFamilyRequest;
use App\Services\PatientFamilyService;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * 家庭成员
 */
class PatientFamilyController extends AbstractController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    /**
     * 获取家庭成员列表
     * @return ResponseInterface
     */
    public function getFamilyList(): ResponseInterface
    {
        $patientFamilyService = new PatientFamilyService();
        $data = $patientFamilyService->getFamilyList();
        return $this->response->json($data);
    }

    /**
     * 新增家庭成员
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws GuzzleException
     */
    public function addFamily(): ResponseInterface
    {
        $request = $this->container->get(PatientFamilyRequest::class);
        $request->scene('addFamily')->validateResolved();

        // 检测入参身份证号
//        $res = $this->checkRequestIdCard();
//        if (!empty($res)) {
//            return $this->response->json(fail(HttpEnumCode::HTTP_ERROR, $res));
//        }

        $patientFamilyService = new PatientFamilyService();
        $data = $patientFamilyService->addFamily();
        return $this->response->json($data);
    }

    /**
     * 修改家庭成员
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function editFamily(): ResponseInterface
    {
        $request = $this->container->get(PatientFamilyRequest::class);
        $request->scene('editFamily')->validateResolved();

        // 检测入参身份证号
        $res = $this->checkRequestIdCard();
        if (!empty($res)) {
            return $this->response->json(fail(HttpEnumCode::HTTP_ERROR, $res));
        }

        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->editFamily();

        return $this->response->json($data);
    }

    /**
     * 删除家庭成员
     * @return ResponseInterface
     */
    public function deleteFamily(): ResponseInterface
    {
        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->deleteFamily();

        return $this->response->json($data);
    }

    /**
     * 获取家庭成员详情
     * @return ResponseInterface
     */
    public function getFamily(): ResponseInterface
    {
        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->getFamily();

        return $this->response->json($data);
    }

    /**
     * 获取家庭成员详情-个人情况
     * @return ResponseInterface
     */
    public function getFamilyPersonal(): ResponseInterface
    {
        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->getFamilyPersonal();

        return $this->response->json($data);
    }

    /**
     * 修改家庭成员-个人情况
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function editFamilyPersonal(): ResponseInterface
    {
        $request = $this->container->get(PatientFamilyRequest::class);
        $request->scene('editFamilyPersonal')->validateResolved();

        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->editFamilyPersonal();

        return $this->response->json($data);
    }

    /**
     * 新增家庭成员-个人情况
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function addFamilyPersonal(): ResponseInterface
    {
        $request = $this->container->get(PatientFamilyRequest::class);
        $request->scene('addFamilyPersonal')->validateResolved();

        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->addFamilyPersonal();

        return $this->response->json($data);
    }

    /**
     * 获取家庭成员-健康状况
     * @return ResponseInterface
     */
    public function getFamilyHealth(): ResponseInterface
    {
        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->getFamilyHealth();

        return $this->response->json($data);
    }

    /**
     * 修改家庭成员-健康情况
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function editFamilyHealth(): ResponseInterface
    {
        $request = $this->container->get(PatientFamilyRequest::class);
        $request->scene('editFamilyHealth')->validateResolved();

        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->editFamilyHealth();

        return $this->response->json($data);
    }

    /**
     * 新增家庭成员-健康情况
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function addFamilyHealth(): ResponseInterface
    {
        $request = $this->container->get(PatientFamilyRequest::class);
        $request->scene('addFamilyHealth')->validateResolved();

        $patientFamilyService = new PatientFamilyService();

        $data = $patientFamilyService->addFamilyHealth();

        return $this->response->json($data);
    }

    /**
     * 获取家庭成员用药记录列表
     * @return ResponseInterface
     */
    public function getFamilyProductRecord(): ResponseInterface
    {
        $patientFamilyService = new PatientFamilyService();
        $data = $patientFamilyService->getFamilyProductRecord();
        return $this->response->json($data);
    }

    /**
     * 检测入参身份证号
     * @return string
     */
    private function checkRequestIdCard(): string
    {
        $messages = [
            'id_number.regex' => '证件号错误',
        ];

        $validator = $this->validationFactory->make($this->request->all(), [
            'type' => 'required',
        ], $messages);

        $validator->sometimes(
            ['id_number'],
            ['regex:/^(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dxX]$/'],
            function ($input) {
                return $input->type == 1;
            }
        );
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        return "";
    }

}