<?php

namespace App\Http\Controllers\Employee;

use App\Http\Controllers\Controller;
use App\Http\Requests\Employee\UserEmployeeRequest;
use App\Models\Department;
use App\Models\Employee;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\View\View;

class UserManagementController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): View
    {
        $users = User::all();

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

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('employee.pages.user.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(UserEmployeeRequest $request)
    {
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password),
            'gender' => $request->gender,
            'birth_date' => $request->birth_date,
            'phone' => $request->phone,
            'address' => $request->address,
            'role' => 'admin',
        ]);

        Employee::create([
            'user_id' => $user->id,
            'nip' => $request->nip,
            'position' => $request->position,
        ]);

        return redirect()->route('employee.users')->with('success', 'User has been created');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(User $user)
    {
        $departments = Department::orderBy('department_name', 'asc')->get();

        return view('employee.pages.user.edit', compact('user', 'departments'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, User $user)
    {
        if ($user->role === 'admin') {
            $user->update([
                'name' => $request->name,
                'email' => $request->email,
            ]);

            $user->employee->update([
                'user_id' => $user->id,
                'nip' => $request->nip,
                'position' => $request->position,
            ]);
        } elseif ($user->role === 'student') {
            $user->update([
                'name' => $request->name,
                'email' => $request->email,
            ]);

            $user->student->update([
                'user_id' => $user->id,
                'nim' => $request->nim,
                'faculty' => $request->faculty,
                'department_id' => $request->department_id,
            ]);
        }
        // dd($request->all());

        return redirect()->route('employee.users')->with('success', 'User has been updated');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
