<?php

namespace App\Http\Controllers;

use App\Contact;
use App\Warehouse;
use Illuminate\Http\Request;

class WarehouseController extends Controller
{
    /**
     * list all the warehouse from user's contact's where user is freight forwarder
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */ 
    public function index()
    {
        $warehouses = Warehouse::where('com_id', Auth::user()->company())->get();
        
        if (!$warehouses)
        {
            return response()->json(['message'=>'Access Denied!'], 404);
        }

        //$warehouse = Warehouse::all();

        return response()->json($warehouses, 200);

    }

     /**
     * Display warehouse list from freight forwarder. can list the warehouse if it is from the freight forwarder
     * need to do DB query
     * show all the warehouse that belongs to the user company
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $warehouses = DB::table('warehouses')
                            ->select('id', 'address', 'city', 'zip', 'state', 'contact_person', 'contact_person_no', 'branch', 'country', 'com_id')
                            ->where('com_id', Auth::user()->company())
                            ->get();

        if (!$warehouses)
        {
            return response()->json(['message'=>'Access Denied!'], 404);
        }

        return response()->json($warehouse, 200);
    }

    /**
     * Store a  new warehouse.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = Auth::user();

        $warehouse = new Warehouse;
        $warehouse->address = $request->input('address');
        $warehouse->city = $request->input('city');
        $warehouse->zip = $request->input('zip');
        $warehouse->state = $request->input('state');
        $warehouse->contact_person = $request->input('contact_person');
        $warehouse->contact_person_no = $request->input('contact_person_no');
        $warehouse->branch = $request->input('branch');
        $warehouse->country = $request->input('country');
        $warehouse->com_id = $user->company();
        $warehouse->save();

        return response()->json($warehouse, 201);

    }
   
    /**
     * Update warehouse.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $warehouse = Warehouse::where('com_id', Auth::user()->company())->where('id', $id)->firstOrFail();

        if (!$warehouse)
        {
            return response()->json(['message'=>'Access Denied!'], 404);
        }

        $warehouse->address = $request->input('address');
        $warehouse->city = $request->input('city');
        $warehouse->zip = $request->input('zip');
        $warehouse->state = $request->input('state');
        $warehouse->contact_person = $request->input('contact_person');
        $warehouse->contact_person_no = $request->input('contact_person_no');
        $warehouse->branch = $request->input('branch');
        $warehouse->country = $request->input('country');
        $warehouse->com_id = $user->company();
        $warehouse->save();

        return response()->json(['warehouse'=>$warehouse],200);
    }

    /**
     * Remove the warehouse.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Warehouse $id)
    {
        $warehouse = Warehouse::where('com_id', Auth::user()->company())->where('id', $id)->firstOrFail();

        if (!$warehouse)
        {
            return response()->json(['message'=>'Access Denied!'], 404);
        }
        
        $warehouse->delete($id);

        return response()->json($warehouse, 204);
    }

}