<?php

namespace App\Http\Controllers\Goods;

use App\Http\Controllers\Controller;
use App\Http\Resources\GoodsBrandResource;
use App\Models\GoodsBrand;
use App\Models\Log as LogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class GoodsBrandsController extends Controller
{
    public function __construct(Request $request)
    {
        $this->log = new LogModel([
            'module' => 'goods',
            'action' => $request->getMethod(),
            'target_type' => 'goods_brand',
        ]);
    }

    public function index(Request $request)
    {
        $goodsBrands = GoodsBrand::query()->paginate($request->get('per_page'));

        return GoodsBrandResource::collection($goodsBrands);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'names' => 'required|array',
            'names.*' => 'required|string|max:191|unique:goods_brands,name',
        ]);
        if ($validator->fails()) {
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());

            return response($this->res, $this->res['httpCode']);
        }
        $goodsBrands = [];
        foreach ($request->names as $name) {
            $goodsBrands[] = ['name' => $name];
        }
        $goodsBrand = new GoodsBrand();
        if (!$goodsBrand->batchInsert($goodsBrands)) {
            $this->res = [
                'httpCode' => 500,
                'errorCode' => 500500,
                'errorMessage' => '批量添加失败',
            ];
        }
        $this->setAfterUpdate($goodsBrands);
        $this->addLog(0, 'add');

        return response($this->res, $this->res['httpCode']);
    }

    public function show($id)
    {
        return new GoodsBrandResource(GoodsBrand::query()->find($id));
    }

    public function update($id, Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => [
                'required',
                'string',
                'max:191',
                Rule::unique('goods_brands')->ignore($id),
            ]
        ]);
        if ($validator->fails()) {
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());

            return response($this->res, $this->res['httpCode']);
        }
        $goodsBrand = GoodsBrand::query()->find($id);
        $this->setBeforeUpdate($goodsBrand->name);
        $goodsBrand->name = request('name');
        $goodsBrand->save();
        $this->setAfterUpdate($goodsBrand->name);
        $this->addLog($id, 'name');

        return new GoodsBrandResource($goodsBrand);
    }

    public function destroy($id)
    {
        $goodsBrand = GoodsBrand::query()->find($id);
        try {
            $goodsBrand->delete();
            $this->addLog($id, 'status');
        } catch (\Exception $e) {
            $this->res = [
                'httpCode' => 500,
                'errorCode' => 500416,
                'errorMessage' => $e->getMessage(),
            ];
        }

        return response($this->res, $this->res['httpCode']);
    }
}
