<?php

namespace App\Http\Controllers\Admin;

use App\Exports\StudentFormatExport;
use App\Models\Student;
use App\Models\Classroom;
use Illuminate\Http\Request;
use App\Imports\StudentsImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //get students
        $students = Student::when(request()->q, function($students) {
            $students = $students->where('name', 'like', '%'. request()->q . '%');
        })->with('classroom')->latest()->paginate(5);

        //append query string to pagination links
        $students->appends(['q' => request()->q]);

        //render with inertia
        return inertia('Admin/Students/Index', [
            'students' => $students,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //get classrooms
        $classrooms = Classroom::all();

        //render with inertia
        return inertia('Admin/Students/Create', [
            'classrooms' => $classrooms,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //validate request
        $request->validate([
            'name'          => 'required|string|max:255',
            'nisn'          => 'required|string|unique:students',
            'gender'        => 'required|string',
            'password'      => 'required|confirmed',
            'classroom_id'  => 'required'
        ]);

        //create student
        Student::create([
            'name'          => $request->name,
            'nisn'          => $request->nisn,
            'gender'        => $request->gender,
            'password'      => $request->password,
            'classroom_id'  => $request->classroom_id
        ]);

        //redirect
        return redirect()->route('admin.students.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //get student
        $student = Student::findOrFail($id);

        //get classrooms
        $classrooms = Classroom::all();

        //render with inertia
        return inertia('Admin/Students/Edit', [
            'student' => $student,
            'classrooms' => $classrooms,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Student $student)
    {
        //validate request
        $request->validate([
            'name'          => 'required|string|max:255',
            'nisn'          => 'required|string|unique:students,nisn,'.$student->id,
            'gender'        => 'required|string',
            'classroom_id'  => 'required',
            'password'      => 'confirmed'
        ]);

        //check passwordy
        if($request->password == "") {

            //update student without password
            $student->update([
                'name'          => $request->name,
                'nisn'          => $request->nisn,
                'gender'        => $request->gender,
                'classroom_id'  => $request->classroom_id
            ]);

        } else {

            //update student with password
            $student->update([
                'name'          => $request->name,
                'nisn'          => $request->nisn,
                'gender'        => $request->gender,
                'password'      => $request->password,
                'classroom_id'  => $request->classroom_id
            ]);

        }

        //redirect
        return redirect()->route('admin.students.index');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //get student
        $student = Student::findOrFail($id);

        //delete student
        $student->delete();

        //redirect
        return redirect()->route('admin.students.index');
    }

    /**
     * import
     *
     * @return void
     */
    public function import()
    {
        return inertia('Admin/Students/Import');
    }

    /**
     * Download format import siswa.
     *
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function exportFormat()
    {
        $classroomTitle = Classroom::query()->value('title') ?? 'X IPA 1';

        return Excel::download(new StudentFormatExport($classroomTitle), 'format-import-siswa.xlsx');
    }
    
    /**
     * storeImport
     *
     * @param  mixed $request
     * @return void
     */
    public function storeImport(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:csv,xls,xlsx'
        ]);

        $import = new StudentsImport();
        Excel::import($import, $request->file('file'));

        $failures     = $import->failures();
        $rowsImported = $import->getRowsImported();

        if ($failures->isNotEmpty()) {
            $errors = [];
            foreach ($failures as $failure) {
                foreach ($failure->errors() as $error) {
                    $errors[] = "Baris {$failure->row()}: {$error}";
                }
            }

            if ($rowsImported > 0) {
                // Sebagian berhasil — redirect ke index dengan notif sukses + daftar error
                return redirect()->route('admin.students.index')
                    ->with('success', "{$rowsImported} siswa berhasil diimport.")
                    ->withErrors($errors);
            }

            // Semua gagal — kembali ke halaman import dengan daftar error
            return redirect()->route('admin.students.import')
                ->withErrors($errors);
        }

        if ($rowsImported === 0) {
            return redirect()->route('admin.students.import')
                ->with('error', 'Tidak ada siswa yang diimport. Pastikan file tidak kosong dan sesuai format.');
        }

        return redirect()->route('admin.students.index')
            ->with('success', "{$rowsImported} siswa berhasil diimport!");
    }

}
