<?php

namespace App\Http\Controllers\Admin;

use App\Facades\DataTable\DataTableFacade;
use App\Facades\File\FileFacade;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UserManagement\StoreRequest;
use App\Http\Requests\Admin\UserManagement\UpdateRequest;
use App\Http\Resources\Admin\UserResource;
use App\Http\Traits\ApiResponse;
use App\Models\Province;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserManagementController extends Controller
{
    use ApiResponse;

    public function index(Request $request): JsonResponse
    {
        $data = DataTableFacade::run(
            User::query()->where('username', "<>", "witel"),
            $request,
            allowedFilters: array('id', 'full_name', 'username', 'province_id', 'phone_number', 'national_id', 'telephone_id'),
            allowedSortings: array('id', 'full_name', 'username', 'province_id', 'phone_number', 'national_id', 'telephone_id'),
            allowedRelations: array('roles:id,name_fa')
        );

        return response()->json($data);
    }

    public function store(StoreRequest $request): JsonResponse
    {
        DB::transaction(function () use ($request) {
            $province = Province::query()->find($request->province_id);

            $user = User::query()->create([
                'full_name' => $request->full_name,
                'username' => $request->username,
                'position' => $request->position,
                'gender' => $request->gender,
                'national_id' => $request->national_id,
                'phone_number' => $request->phone_number,
                'province_id' => $province->id,
                'province_fa' =>  $province->name,
                'password' => Hash::make($request->password),
                'email' => $request->email,
                'telephone_id' => $request->telephone_id,
            ]);

            $user->assignRole(Role::query()->find($request->role_id));
        });

        return $this->successResponse();
    }

    public function show(User $user): JsonResponse
    {
        return $this->successResponse(new UserResource($user));
    }

    public function update(UpdateRequest $request, User $user): JsonResponse
    {
        DB::transaction(function () use ($request, $user) {
            $province = Province::query()->find($request->province_id);

            $user->update([
                'full_name' => $request->full_name,
                'username' => $request->username,
                'position' => $request->position,
                'gender' => $request->gender,
                'national_id' => $request->national_id,
                'phone_number' => $request->phone_number,
                'province_id' => $province->id,
                'province_fa' =>  $province->name,
                'email' => $request->email,
                'telephone_id' => $request->telephone_id,
            ]);

            $user->syncRoles(Role::query()->find($request->role_id));
        });

        return $this->successResponse();
    }

    public function destroy(User $user): JsonResponse
    {
        DB::transaction(function () use ($user) {
            if ($user->avatar) {
                FileFacade::delete($user->avatar, true);
            }

            $user->syncRoles([]);
            $user->delete();
        });

        return $this->successResponse();
    }
}
