<?php

namespace App\Http\Controllers\Admin\Users;

use App\Exports\LecturersExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UpdateStatusRequest;
use App\Http\Requests\Admin\Users\LecturerRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\User;
use App\Services\Admin\Master\DepartmentService;
use App\Services\Admin\Users\LecturerService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class LecturerController extends Controller
{
    public function __construct(
        private readonly LecturerService $service,
        private readonly DepartmentService $departmentService,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/users/lecturers/index', [
            'lecturers' => $this->service->paginated(
                ...$request->validatedWithDefaults(),
                gender: $request->validated('gender'),
                departmentId: $request->validated('department_id'),
            ),
            'departments' => $this->departmentService->getAllForSelect(),
            'filters' => $request->only(['gender', 'department_id']),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/users/lecturers/create', [
            'departments' => $this->departmentService->getAllForSelect(),
        ]);
    }

    public function store(LecturerRequest $request): RedirectResponse
    {
        $this->service->create($request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Dosen berhasil ditambahkan.']);

        return to_route('admin.users.lecturers.index');
    }

    public function edit(User $user): Response
    {
        $user->load(['profile', 'lecturer.departments']);

        return Inertia::render('admin/users/lecturers/edit', [
            'user' => $user,
            'departments' => $this->departmentService->getAllForSelect(),
        ]);
    }

    public function update(LecturerRequest $request, User $user): RedirectResponse
    {
        $this->service->update($user, $request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Dosen berhasil diperbarui.']);

        return to_route('admin.users.lecturers.index');
    }

    public function destroy(User $user): RedirectResponse
    {
        $this->service->delete($user);

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Dosen berhasil dihapus.'])->back();
    }

    public function resetPassword(User $user): RedirectResponse
    {
        $this->service->resetPassword($user);

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Kata sandi berhasil direset.'])->back();
    }

    public function updateUserStatus(UpdateStatusRequest $request, User $user): RedirectResponse
    {
        $this->service->updateUserStatus($user, $request->boolean('is_active'));

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Status akun berhasil diperbarui.'])->back();
    }

    public function export(PaginatedRequest $request): BinaryFileResponse
    {
        return Excel::download(new LecturersExport(
            search: $request->validated('search') ?? '',
            gender: $request->validated('gender'),
            departmentId: $request->validated('department_id'),
        ), 'dosen.xlsx');
    }
}
