<?php

namespace App\Api\Controllers\Area;

use App\Api\Controllers\Controller;
use App\Api\Resources\Area\AreaCodeCollection;
use App\Api\Resources\Area\AreaResource;
use App\Models\Area;
use App\Models\AreaCode;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Jason\Api\Api;

class IndexController extends Controller
{

    /**
     * Notes: 我管理的地区
     *
     * @Author: 玄尘
     * @Date: 2023/1/11 15:37
     */
    public function index(): JsonResponse
    {
        $user  = Api::user();
        $areas = Area::query()
            ->whereHas('areaClerks', function ($q) {
                $q->where('user_id', Api::userId());
            })
            ->get();

        return $this->success(AreaResource::collection($areas));
    }

    /**
     * Notes: 查看库存量
     *
     * @Author: 玄尘
     * @Date: 2023/1/16 11:19
     * @return JsonResponse|mixed
     */
    public function show()
    {
        $area = Area::query()
            ->whereHas('areaClerks', function ($q) {
                $q->where('user_id', Api::userId());
            })
            ->first();

        if (! $area) {
            return $this->failed('您没有管理的区域无法查看');
        }

        $codes   = $area->areaCodes()->latest()->paginate();
        $release = $area->areaCodes()->count();

        $data = [
            'count' => [
                'all'       => $area->stocks()->sum('amount'),
                'stock'     => $area->stock,
                'release'   => $release,
                'unrelease' => bcsub($area->stock, $release),
            ],
            'codes' => new AreaCodeCollection($codes)
        ];

        return $this->success($data);
    }

    /**
     * Notes: 查看管理的提货码
     *
     * @Author: 玄尘
     * @Date: 2023/1/11 15:42
     * @param  Request  $request
     * @return JsonResponse|mixed
     */
    public function codes(Request $request)
    {
        $status = $request->status ?? '';

        $area = Area::query()
            ->whereHas('areaClerks', function ($q) {
                $q->where('user_id', Api::userId());
            })
            ->first();
        if (! $area) {
            return $this->failed('您没有管理的区域无法查看');
        }

        $codes = $area->areaCodes()
            ->when($status, function ($q) use ($status) {
                $q->where('status', $status);
            })
            ->paginate();

        return $this->success(new AreaCodeCollection($codes));
    }

    /**
     * Notes: 生成提货码
     *
     * @Author: 玄尘
     * @Date: 2023/1/11 15:52
     * @param  Area  $area
     * @param  Request  $request
     * @return JsonResponse|mixed
     */
    public function generate(Request $request)
    {
        $area = Area::query()
            ->whereHas('areaClerks', function ($q) {
                $q->where('user_id', Api::userId());
            })
            ->first();
        if (! $area) {
            return $this->failed('您没有管理的区域无法查看');
        }

        $res = $area->generate(Api::userId(), $request->num);
        if ($res === true) {
            return $this->success('生成成功');
        } else {
            return $this->failed($res);
        }
    }

}
