<?php

namespace App\Http\Controllers\Admin\Users;

use App\Enums\StudentStatus;
use App\Exports\StudentsExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UpdateStatusRequest;
use App\Http\Requests\Admin\Users\Student\StudentRequest;
use App\Http\Requests\Admin\Users\Student\StudentStatusRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\User;
use App\Services\Admin\Master\DepartmentService;
use App\Services\Admin\Users\LecturerService;
use App\Services\Admin\Users\StudentService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

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

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

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

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

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

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

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

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

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

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

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

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

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Mahasiswa 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 updateStatus(StudentStatusRequest $request, User $user): RedirectResponse
    {
        $this->service->updateStatus($user, $request->validated('status'));

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Status mahasiswa berhasil diperbarui.'])->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 StudentsExport(
            search: $request->validated('search') ?? '',
            gender: $request->validated('gender'),
            departmentId: $request->validated('department_id'),
            status: $request->validated('status'),
            enrollmentYear: $request->validated('enrollment_year'),
        ), 'mahasiswa.xlsx');
    }
}
