<?php

namespace Modules\Admin\Http\Controllers;

use Modules\Post\Models\Post;
use Modules\Post\Models\PostCategory;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Modules\User\Models\UserRole;
use Modules\User\Models\Role;
use Modules\User\Models\User;
use Modules\Main\Models\Company\CompanyAdmin;
use Modules\Main\Models\Agent\Agent;

class AdminUsersController extends Controller
{
    public function index(Request $request)
    {
        $users = User::orderBy('name');
        if ($request->role && $request->role != 'all')
        {
            $users->whereIn(
                'id',
                UserRole::where('role_id', $request->role)
                    ->get()
                    ->map(function (UserRole $userRole)
                    {
                        return $userRole->user_id;
                    })
            );
        }
        return view('admin::users.index', [
            'users' => $users->get(),
            'roles' => Role::all(),
            'role'  => $request->role
        ]);
    }
    public function edit(User $user)
    {
        $roles = UserRole::where('user_id', $user->id)->get();
        return view('admin::users.edit', [
            'user'      => $user,
            'userRoles' => $roles,
            'roles'     => Role::class,
            'companyAdmins' => CompanyAdmin::where('user_id', $user->id)->get(),
            'companyAgents' => Agent::where('user_id', $user->id)->get(),
            'backUrl' => route(name: 'admin.users')
        ]);
    }
    public function store(Request $request)
    {
        $data = $request->only('name', 'phone', 'email', 'role_id');
        $user = User::create($data);
        $password = $user->setForcedPassword(false);
        if ($request->has('role')) {
            UserRole::updateOrCreate([
                'user_id' => $user->id,
                'role_id' => $request->role_id
            ]);
        }
        return to_route('admin.users')->withSuccess('Учетная запись для ' . $request->name . ' создана. Пароль пользователя: ' . $password);
    }

    public function delete(Request $request, User $user)
    {
        if ($request->confirmed) {
            $user->delete();
            return to_route('admin.users');
        } else {
            if ($user->hasRole(Role::CLIENT)) {
                return view('admin::users.has-deals',
                    ['user' => $user]
                );
            } else {
                return view('admin::users.confirm-delete',
                    ['user' => $user]
                );
            }
        }
        return to_route('admin.users');
    }
    public function update(Request $request, User $user)
    {
        $user->update($request->only('name', 'email', 'phone'));
        return to_route('admin.users');
    }

    public function deleteUserRole(UserRole $userRole)
    {
        $user = User::find($userRole->user_id);
        $userRole->delete();
        return to_route('admin.users.edit', [
            'user' => $user
        ]);
    }
}
