<?php

namespace Modules\Desktopapp\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Modules\Desktopapp\Transformers\CommonResource;

use App\Brands;

/**
 * @group Brand management
 * @authenticated
 *
 * APIs for managing brands
 */
class BrandController extends ApiController
{
    /**
     * List brands
     * @response {
            "data": [
                {
                    "id": 1,
                    "business_id": 1,
                    "name": "Levis",
                    "description": null,
                    "created_by": 1,
                    "deleted_at": null,
                    "created_at": "2018-01-03 21:19:47",
                    "updated_at": "2018-01-03 21:19:47"
                },
                {
                    "id": 2,
                    "business_id": 1,
                    "name": "Espirit",
                    "description": null,
                    "created_by": 1,
                    "deleted_at": null,
                    "created_at": "2018-01-03 21:19:58",
                    "updated_at": "2018-01-03 21:19:58"
                }
            ]
        }
     */
    public function index()
    {
        $user = Auth::user();

        $business_id = $user->business_id;

        $filters = request()->only(['per_page']);

        $search = request()->only(['name']);
        
        //order
        $order_by = null;
        $order_direction = null;

        if(!empty(request()->input('order_by'))){
            $order_by = in_array(request()->input('order_by'), ['name', 'newest']) ? request()->input('order_by') : null;
            $order_direction = in_array(request()->input('order_direction'), ['asc', 'desc']) ? request()->input('order_direction') : 'asc';
        }
        
        $brands = $this->__getBrands($business_id, $filters, $search, true, $order_by, $order_direction);

        return CommonResource::collection($brands);
    }

    /**
     * Get the specified brand
     *
     * @urlParam brand required comma separated ids of the brands Example: 1
     * @response {
            "data": [
                {
                    "id": 1,
                    "business_id": 1,
                    "name": "Levis",
                    "description": null,
                    "created_by": 1,
                    "deleted_at": null,
                    "created_at": "2018-01-03 21:19:47",
                    "updated_at": "2018-01-03 21:19:47"
                }
            ]
        }
     */
    public function show($brand_ids)
    {
        $user = Auth::user();

        $business_id = $user->business_id;
        $brand_ids = explode(',', $brand_ids);

        $brands = Brands::where('business_id', $business_id)
                        ->whereIn('id', $brand_ids)
                        ->get();

        return CommonResource::collection($brands);
    }

        /**
     * Function to query product
     * @return Response
     */
    private function __getBrands($business_id, $filters = [], $search = [], $pagination = false, $order_by = null, $order_direction = null)
    {
        $query = Brands::where('business_id', $business_id);

        if (!empty($search)) {
            $query->where(function ($query) use ($search) {

                if (!empty($search['name'])) {
                    $query->where('brands.name', 'like', '%' . $search['name'] .'%');
                }
                
            });
        }

        //Order by
        if(!empty($order_by)){
            if($order_by == 'name'){
                $query->orderBy('brands.name', $order_direction);
            }

            if($order_by == 'newest'){
                $query->orderBy('brands.id', $order_direction);
            }
        }

        $perPage = !empty($filters['per_page']) ? $filters['per_page'] : $this->perPage;
        if ($pagination && $perPage != -1) {
            $brands = $query->paginate($perPage);
            $brands->appends(request()->query());
        } else{
            $brands = $query->get();
        }

        return $brands;
    }
}
