<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\Api\UserResource;
use App\Models\City;
use App\Models\Province;
use App\Models\User;
use App\Models\EdarateShahri;
use App\Models\EdarateOstani;
use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Permission;
use App\Models\Role;
use App\Models\HttpErorrsList;
use Illuminate\Support\Facades\Storage;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    // public function index(Request $request)
    // {
    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1038);
    //     }

    //     $province = $request->province ?? null;
    //     $city = $request->city ?? null;
    //     $province_office = $request->province_office ?? null;

    //     if (Auth::user()->checkUserHasPermission('full-user-management')) {
    //         $users = User::when($province, function ($query, $province) {
    //             return $query->where('province_id', $province);
    //         })
    //         ->when($city, function ($query, $city) {
    //             return $query->where('city_id', $city);
    //         })
    //         ->when($province_office, function ($query, $province_office) {
    //             if ($province_office == -1) {
    //                 return $query->where('edarate_ostani_id', null)->whereNotNull('edarate_shahri_id');
    //             } else {
    //                 return $query->where('edarate_ostani_id', $province_office);
    //             }
    //         })
    //         ->with(['roles' => function ($query) {
    //             $query->where('for_report', 1);
    //         }])
    //         ->get();
    //     } elseif (Auth::user()->checkUserHasPermission('limited-user-management')) {
    //         $users = User::when($province, function ($query, $province) {
    //             return $query->where('province_id', $province);
    //         })
    //         ->where('province_id',Auth::user()->province_id)
    //         ->when($city, function ($query, $city) {
    //             return $query->where('city_id', $city);
    //         })
    //         ->when($province_office, function ($query, $province_office) {
    //             if ($province_office == -1) {
    //                 return $query->where('edarate_ostani_id', null)->whereNotNull('edarate_shahri_id');
    //             } else {
    //                 return $query->where('edarate_ostani_id', $province_office);
    //             }
    //         })
    //         ->with(['roles' => function ($query) {
    //             $query->where('for_report', 1);
    //         }])
    //         ->get();
    //     } else {
    //         return response()->json([
    //             'message' => 'Forbidden'
    //         ], 403);
    //     }

    //     return response()->json([
    //         'status' => '200',
    //         'message' => 'success',
    //         'data' => $users,
    //     ], 200);
    // }

    // public function store(Request $request)
    // {
    //     $validData = $request->validate([
    //         'username' => 'string|unique:users',
    //         'national_code' => 'required|unique:users,national_code',
    //         'mobile' => 'digits:11|regex:/^09[0-9]{9}$/i',
    //     ]);

    //     $createUser = new User();

    //     $createUser->province_id = $request->province_id == 0 ? null : $request->province_id;
    //     $createUser->province_fa = ($request->province_id == 0) ? 'اداره کل' : Province::find($request->province_id)->name_fa;

    //     $createUser->city_id = $request->city_id == 0 ? null : $request->city_id;
    //     $createUser->city_fa = ($request->city_id == 0) ? null : City::find($request->city_id)->name_fa ;

    //     if ($request->edarate_ostani_id != 'null') {
    //         $temp = EdarateOstani::find($request->edarate_ostani_id)->name_fa;
    //         $createUser->edarate_ostani_id = $request->edarate_ostani_id;
    //         $createUser->edarate_ostani_name = $temp;
    //         if ($request->province_id == 0) {
    //             $createUser->name = $temp .' '. $createUser->province_fa;
    //         } else {
    //             $createUser->name = $temp .' استان '. $createUser->province_fa;
    //         }
    //     } else {
    //         $temp = EdarateShahri::find($request->edarate_shahri_id)->name_fa;
    //         $createUser->edarate_shahri_id = $request->edarate_shahri_id;
    //         $createUser->edarate_shahri_name = $temp;

    //         if ($request->province_id == 0) {
    //             $createUser->name = $temp .' '. $createUser->province_fa;
    //         } else {
    //             $createUser->name = $temp .' استان '. $createUser->province_fa;
    //         }
    //     }

    //     $createUser->username = $request->username;
    //     $createUser->password = Hash::make($request->password);
    //     $createUser->first_name = $request->first_name;
    //     $createUser->last_name = $request->last_name;
    //     $createUser->position = $request->position;
    //     $createUser->mobile = $request->mobile;
    //     $createUser->degree = $request->degree;
    //     $createUser->major = $request->major;
    //     $createUser->national_code = $request->national_code;

    //     $createUser->save();

    //     if (! $request->permissions == null) {
    //         $createUser->syncPermissions(explode(",", $request->permissions));
    //     }

    //     $roles = $request->only('roles');
    //     if (! $roles == null) {
    //         $createUser->syncRoles($roles);
    //     }

    //     if ($request->has('avatar')) {
    //         $destinationPath = 'avatar/' . $request->username;
    //         $filename = time() .".". $request->file('avatar')->getClientOriginalExtension();
    //         $request->file('avatar')->storeAs($destinationPath, $filename, 'public');
    //         $createUser->update(['avatar' => 'storage/'.$destinationPath ."/". $filename]);
    //     }

    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1039, $createUser);
    //     }

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $createUser
    //     ], 200);
    // }

    // public function simpleAddUser(Request $request)
    // {
    //     $request->validate([
    //         'province_id' => 'required',
    //         'username' => 'required|unique:users,username',
    //         'national_code' => 'required|unique:users,national_code',
    //         'password' => 'required'
    //     ]);

    //     $user = new User();
    //     $user->username = $request->username;

    //     $user->province_id = $request->province_id == 0 ? null : $request->province_id;
    //     $user->province_fa = ($request->province_id == 0) ? 'اداره کل' : Province::find($request->province_id)->name_fa;

    //     $user->city_id = $request->city_id == 0 ? null : $request->city_id;
    //     $user->city_fa = ($request->city_id == 0) ? null : City::find($request->city_id)->name_fa ;

    //     if ($request->edarate_ostani_id != 'null') {
    //         $temp = EdarateOstani::find($request->edarate_ostani_id)->name_fa;
    //         $user->edarate_ostani_id = $request->edarate_ostani_id;
    //         $user->edarate_ostani_name = $temp;
    //         if ($request->province_id == 0) {
    //             $user->name = $temp .' '. $user->province_fa;
    //         } else {
    //             $user->name = $temp .' استان '. $user->province_fa;
    //         }
    //     } else {
    //         $temp = EdarateShahri::find($request->edarate_shahri_id)->name_fa;
    //         $user->edarate_shahri_id = $request->edarate_shahri_id;
    //         $user->edarate_shahri_name = $temp;

    //         if ($request->province_id == 0) {
    //             $user->name = $temp .' '. $user->province_fa;
    //         } else {
    //             $user->name = $temp .' استان '. $user->province_fa;
    //         }
    //     }

    //     $user->password = Hash::make($request->password);
    //     $user->national_code = $request->national_code;
    //     $user->save();

    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1039, $user);
    //     }

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $user
    //     ], 200);
    // }

    // public function show($id)
    // {
    //     $user = User::where('id', $id)->with(['roles', 'permissions'])->get();
    //     return response()->json([
    //         'status' => '200',
    //         'message' => 'success',
    //         'data' => $user
    //     ], 200);
    // }

    // public function update(Request $request, User $user)
    // {
    //     $request->validate([
    //         'national_code' => 'required|unique:users,national_code,'.$user->id,
    //     ]);

    //     if ($request->has('avatar')) {
    //         Storage::delete('public/' . $user->avatar);
    //         $destinationPath = 'avatar/' . $request->username;
    //         $filename = time() .".". $request->file('avatar')->getClientOriginalExtension();
    //         $request->file('avatar')->storeAs($destinationPath, $filename, 'public');
    //         $user->update(['avatar' => 'storage/'.$destinationPath ."/". $filename]);
    //     }



    //     $user->province_id = $request->province_id == 0 ? null : $request->province_id;
    //     $user->province_fa = ($request->province_id == 0) ? 'اداره کل' : Province::find($request->province_id)->name_fa;

    //     $user->city_id = $request->city_id == 0 ? null : $request->city_id;
    //     $user->city_fa = ($request->city_id == 0) ? null : City::find($request->city_id)->name_fa ;

    //     if ($request->edarate_ostani_id != 'null') {
    //         $temp = EdarateOstani::find($request->edarate_ostani_id)->name_fa;
    //         $user->edarate_ostani_id = $request->edarate_ostani_id;
    //         $user->edarate_ostani_name = $temp;

    //         $user->edarate_shahri_id = null;
    //         $user->edarate_shahri_name = null;

    //         if ($request->province_id == 0) {
    //             $user->name = $temp .' '. $user->province_fa;
    //         } else {
    //             $user->name = $temp .' استان '. $user->province_fa;
    //         }
    //     } else {
    //         $temp = EdarateShahri::find($request->edarate_shahri_id)->name_fa;
    //         $user->edarate_shahri_id = $request->edarate_shahri_id;
    //         $user->edarate_shahri_name = $temp;
    //         $user->edarate_ostani_id = null;
    //         $user->edarate_ostani_name = null;
    //         if ($request->province_id == 0) {
    //             $user->name = $temp .' '. $user->province_fa;
    //         } else {
    //             $user->name = $temp .' استان '. $user->province_fa;
    //         }
    //     }

    //     $user->first_name = $request->first_name;
    //     $user->last_name = $request->last_name;
    //     $user->position = $request->position;
    //     $user->mobile = $request->mobile;
    //     $user->degree = $request->degree;
    //     $user->major = $request->major;
    //     $user->national_code = $request->national_code;





    //     if (!is_null($request->password)) {
    //         $request->validate([
    //             'password' => ['string']
    //         ]);
    //         $user->password = Hash::make($request->password);
    //     }

    //     if (! $request->permissions == null) {
    //         $user->syncPermissions(explode(",", $request->permissions));
    //     }

    //     $roles = $request->only('roles');
    //     if (! $roles == null) {
    //         $user->syncRoles($roles);
    //     }

    //     $user->save();

    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1040, $user);
    //     }

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $user
    //     ]);
    // }

    // public function destroy(User $user)
    // {
    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1041, $user);
    //     }

    //     $user->delete();
    //     return response()->json([
    //         'status' => 'success',
    //         'message' => 'user has been deleted.'
    //     ]);
    // }

    // For user

    // public function getAllPermissions()
    // {
    //     $permissions = Permission::select('id', 'name', 'name_fa', 'description', 'type')
    //                                 ->orderBy('type')
    //                                 // ->where('id', '>', 9)
    //                                 ->get();

    //     return response()->json([
    //         'Permissions' => $permissions,
    //     ]);
    // }

    // public function userPermission($id)
    // {
    //     $user = User::find($id);
    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $user->permissions,
    //     ]);
    // }

    // public function assignPermission(Request $request, User $user)
    // {
    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1042);
    //     }
    //     $permissions = explode(",", $request->permissions);
    //     if (! $permissions == null) {
    //         $user->syncPermissions($permissions);
    //     }

    //     return response()->json([
    //         'message' => 'status',
    //         'data' => User::where('id', $user->id)->with(['permissions'])->get()
    //     ]);
    // }

    // public function createRole(Request $request)
    // {
    //     $request->validate([
    //         'role_name' => 'required|string',
    //         'permissions' => 'required'
    //     ], [
    //         'permissions.required' => 'انتخاب دسترسی‌ها الزامی است.',
    //         'role_name.required' => 'نام نقش الزامی است.'
    //     ]);

    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1043);
    //     }

    //     $role = Role::create([
    //         'name' => time(),
    //         'name_fa' => $request->role_name,
    //         'description' => $request->description
    //     ]);

    //     $permissions = $request->permissions;

    //     // $permissions = explode(",", $request->permissions);
    //     $role->givePermissionTo($permissions);

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $role
    //     ]);
    // }

    // public function showAllRoles(Request $request)
    // {
    //     $roles = Role::where('for_report', 0)->get();

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $roles
    //     ]);
    // }

    // public function showRole($id, Request $request)
    // {
    //     $role = Role::where('id', $id)->with('permissions:id,name,name_fa,description')->first();

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => [
    //             'role' => Role::whereId($id)->first(),
    //             'permissions' => $role->permissions
    //         ]
    //     ]);
    // }

    // public function editRole(Request $request, $id)
    // {
    //     $request->validate([
    //         'role_name' => 'required|string',
    //         'permissions' => 'required'
    //     ], [
    //         'permissions.required' => 'انتخاب دسترسی‌ها الزامی است.',
    //         'role_name.required' => 'نام نقش الزامی است.'
    //     ]);

    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1044);
    //     }
    //     $role = Role::find($id);


    //     $updateRole = $role->update([
    //         'name_fa' => $request->role_name,
    //         'description' => $request->description
    //     ]);

    //     $permissions = $request->permissions;
    //     // $permissions = explode(",", $request->permissions);

    //     $role->syncPermissions($permissions);

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $role
    //     ]);
    // }

    // public function deleteRole(Request $request, $id)
    // {
    //     if (auth()->user()) {
    //         auth()->user()->addActivityComplete(1045);
    //     }
    //     $role = Role::find($id)->delete();

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => 1
    //     ]);
    // }

    /**
     * Get user permissions
     */
    // public function getUserPermissions()
    // {
    //     $userPermissions = Auth::user()->getUserPermissions();

    //     return response()->json([
    //         'message' => 'success',
    //         'data' => $userPermissions
    //     ]);
    // }
    
}
