<?php

namespace App\Http\Controllers\Employee;

use Illuminate\View\View;
use App\Models\Department;
use App\Models\Personality;
use Illuminate\Support\Str;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\Employee\DepartmentRequest;
use App\Http\Requests\Employee\PersonalityRequest;

class DepartmentController extends Controller
{
    public function index(): View
    {
        // * Get all department ordered by department_code ascending
        $departments = Department::orderBy('department_code', 'asc')->get();

        return view('employee.pages.department.index', compact('departments'));
    }

    public function generateDepartmentCode(): string
    {
        // * Get the latest department ordered by department_code descending
        $latestDepartment = Department::orderBy('department_code', 'desc')->first();

        // * If there is no departmment, the next departmment code will be D0001
        if (!$latestDepartment) {
            $nextDepartmentCode = 'D0001';

            // * If there is a department, the next department code will be the latest department code + 1
        } else {
            $latestDepartmentCode = $latestDepartment->department_code;
            $number = (int) substr($latestDepartmentCode, 1);
            $nextNumber = $number + 1;
            $nextDepartmentCode = 'D' . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
        }

        return $nextDepartmentCode;
    }

    public function create(): View
    {
        // * Generate the department code
        $generateDepartmentCode = $this->generateDepartmentCode();

        return view('employee.pages.department.create', compact('generateDepartmentCode'));
    }

    public function store(DepartmentRequest $request): RedirectResponse
    {
        // * Create the department
        $department = Department::create([
            'department_code' => $request->department_code,
            'department_name' => $request->department_name,
        ]);

        // * Notification
        $notification = [
            'message' => 'Jurusan telah berhasil ditambahkan',
            'alert-type' => 'info',
        ];

        return redirect()->route('departments')->with($notification);
    }

    public function edit(Department $departments): View
    {
        return view('employee.pages.department.edit', compact('departments'));
    }

    public function update(DepartmentRequest $request, Department $departments): RedirectResponse
    {
        // * Update the department
        $departments->update([
            'department_code' => $request->department_code,
            'department_name' => $request->department_name,
        ]);

        // * Notification
        $notification = [
            'message' => 'Jurusan telah berhasil diperbarui',
            'alert-type' => 'info',
        ];

        return redirect()->route('departments')->with($notification);
    }

    public function reorderDepartmentCode(): void
    {
        // * Get all departments ordered by department_code ascending
        $departmentCode = Department::orderBy('department_code', 'asc')->get();

        // * Reorder the department code
        foreach ($departmentCode as $index => $department) {
            $department->update([
                'department_code' => 'D' . str_pad($index + 1, 4, '0', STR_PAD_LEFT),
            ]);
        }
    }

    public function destroy(Department $departments): RedirectResponse
    {
        // * Delete the department
        $departments->delete();

        // * Reorder the department code
        $this->reorderDepartmentCode();

        // * Notification
        $notification = [
            'message' => 'Jurusan telah berhasil dihapus',
            'alert-type' => 'info',
        ];

        return redirect()->route('departments')->with($notification);
    }
}
