<?php

namespace Modules\Account\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Modules\Account\Services\FamilyService;

class FamilyController extends BaseController
{
    protected $familyService;

    public function __construct(FamilyService $familyService)
    {
        $this->familyService = $familyService;
    }

    /**
     * 获取家庭信息
     */
    public function info(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->getInfo($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 创建家庭
     */
    public function create(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->create($request);
            $this->data['message'] = '创建成功';
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 加入家庭
     */
    public function join(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->join($request);
            $this->data['message'] = '加入成功';
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 退出家庭
     */
    public function leave(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->leave($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 获取家庭邀请码
     */
    public function inviteCode(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->getInviteCode($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 重新生成邀请码
     */
    public function regenerateInviteCode(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->regenerateInviteCode($request);
            $this->data['message'] = '邀请码已重新生成';
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 移除家庭成员
     */
    public function removeMember(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->removeMember($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 获取家庭成员列表
     */
    public function members(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->getMembers($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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

    /**
     * 转让家主
     */
    public function transferOwner(Request $request)
    {
        try {
            $this->data['data'] = $this->familyService->transferOwner($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

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