<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    public function index()
    {
        $sysEmail = (string) env('SYSTEM_ADMIN_EMAIL', '');
        $users = User::when($sysEmail !== '', function ($q) use ($sysEmail) {
            $q->where('email', '!=', $sysEmail);
        })->get();

        return view('users.index', compact('users'));
    }

    private function abortIfSystemAdmin(User $user)
    {
        $sysEmail = (string) env('SYSTEM_ADMIN_EMAIL', '');
        if ($sysEmail !== '' && $user->email === $sysEmail) {
            abort(403);
        }
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $user = new User;
        return view('users.create', compact('user'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $inputs = $request->except('_token');

        if (!empty($inputs['password'])) {
            $inputs['password'] = bcrypt($inputs['password']);
        }

        $inputs['show_cost_price'] = $request->has('show_cost_price');
        $inputs['show_sale_price'] = $request->has('show_sale_price');

        $user = User::create($inputs);

        if ($request->hasFile('photo')) {
            $user->addMedia($request->file('photo'))->toMediaCollection('photo');
        }

        $user->assignRole($inputs['role']);

        if (!empty($inputs['stores'])) {
            $user->stores()->attach($inputs['stores']);
        }

        return redirect(route('users.index'));
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        $user = User::findOrFail($id);
        $this->abortIfSystemAdmin($user);

        return view('users.show', compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(User $user)
    {
        $this->abortIfSystemAdmin($user);

        return view('users.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, User $user)
    {
        $this->abortIfSystemAdmin($user);

        $inputs = $request->except('_token');

        if (!empty($inputs['password'])) {
            $inputs['password'] = bcrypt($request->password);
        } else {
            $inputs['password'] = $user->password;
        }

        $inputs['show_cost_price'] = $request->has('show_cost_price');
        $inputs['show_sale_price'] = $request->has('show_sale_price');

        $user->update($inputs);

        if ($request->hasFile('photo')) {
            $user->media()->delete();
            $user->addMedia($request->file('photo'))->toMediaCollection('photo');
        }

        $user->stores()->detach();

        if (!empty($inputs['stores'])) {
            $user->stores()->attach($inputs['stores']);
        }

        $user->syncRoles($inputs['role']);

        return redirect(route('users.index'));
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(User $user)
    {
        $this->abortIfSystemAdmin($user);

        if ($user->delete()) {
            return "done";
        }

        return "failed";
    }
}