<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Studentdetails;

class StudentController extends Controller
{
 
    public function store(Request $request)
{
    $validatedData = $request->validate([
        'student_name' => 'required|string',
        'student_age' => 'required|numeric',
        'date_of_birth' => 'required|date',
        'gender' => 'required|in:male,female',
        'guardian_name' => 'required|string',
        'communication_address' => 'required|string',
        'contact_phone' => 'required|string',
        'student_class' => 'required|string',
        'student_std' => 'required|string',
        'last_exam_marks' => 'required|string',
        'school_details' => 'required|string',
        'student_image' => 'required', // Assuming maximum size is 2MB
    ]);
    
    if ($request->hasFile('student_image')) {
        $image = $request->file('student_image');
        $imageName = time().'.'.$image->extension();
        $image->move(public_path('images'), $imageName);
        $validatedData['student_image'] = 'images/'.$imageName;
    }

    $student = Studentdetails::create($validatedData);
    
    return response()->json(['message' => 'Student details saved successfully.', 'student' => $student], 201);
}

    public function studenttable()
    {
        $students = StudentDetails::all();

        return response()->json(['students' => $students]);
    }

    public function show($id)
    {
        $student = StudentDetails::findOrFail($id);

        return response()->json(['student' => $student]);
    }

    public function update(Request $request, $id)
{
    $validatedData = $request->validate([
        'student_name' => 'required|string',
        'student_age' => 'required|numeric',
        'date_of_birth' => 'required|date',
        'gender' => 'required|in:male,female',
        'guardian_name' => 'required|string',
        'communication_address' => 'required|string',
        'contact_phone' => 'required|string',
        'student_class' => 'required|string',
        'student_std' => 'required|string',
        'last_exam_marks' => 'required|string',
        'school_details' => 'required|string',
        'student_image' => 'nullable', // Image is optional for update
    ]);

    $student = Studentdetails::findOrFail($id);

    if ($request->hasFile('student_image')) {
        $image = $request->file('student_image');

        $imageName = time() . '.' . $image->extension();
        $image->move(public_path('images'), $imageName);
        $validatedData['student_image'] = 'images/' . $imageName;
    }

    $student->update($validatedData);

    return response()->json(['message' => 'Student details updated successfully.', 'student' => $student], 200);
}


    public function destroy($id)
    {
        $student = StudentDetails::findOrFail($id);

        // Your logic for deleting the student here

        return response()->json(['message' => 'Student deleted successfully.']);
    }
}
