<?php

namespace App\Http\Controllers;

use Intervention\Image\Facades\Image;
use App\Models\Faculty;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class FacultyController extends Controller
{
    public function addFaculty(Request $request)
    {

        $data = [
            'user' => Auth::user(),
            'departments' => [
                'Bachelor of Science in Criminology',
                'Bachelor of Science in Information Technology',
                'Bachelor of Science in Nursing',
                'Bachelor of Science in Hospitality Management',
                'Bachelor of Science in Radiologic Technology',
                'Diploma in Hotel and Restaurant Technology'
            ],
            'pageTitle' => 'Add new faculty'
        ];
        return view('back.pages.faculty.add_faculty', $data);
    }

    public function createFaculty(Request $request)
    {
        $request->validate([
            'faculty_name' => 'required|string',
            'faculty_position' => 'required|string',
            'department' => 'required|string',
            'faculty_image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
        ]);
        if ($request->hasFile('faculty_image')) {
            $path = "images/faculty/";
            $file = $request->file('faculty_image');
            $filename = $file->getClientOriginalName();
            $new_filename = time() . '_' . $filename;

            $upload = $file->move(public_path($path), $new_filename);
            if ($upload) {
                $faculty = new Faculty();
                $faculty->faculty_name = $request->faculty_name;
                $faculty->faculty_position = $request->faculty_position;
                $faculty->department = $request->department;
                $faculty->profile_picture = $new_filename;
                $saved = $faculty->save();

                if ($saved) {
                    return response()->json(['status' => 1, 'message' => 'New Faculty has been successfully added.']);
                } else {
                    return response()->json(['status' => 0, 'message' => 'Something went wrong.']);
                }
            } else {
                return response()->json(['status' => 0, 'message' => 'Something went wrong on uploading the faculty picture. ']);
            }
        }
    }

    public function allFaculty(Request $request)
    {
        $data = [
            'pageTitle' => 'Faculties'
        ];
        return view('back.pages.faculty.faculties', $data);
    }

    public function editFaculty(Request $request, $id = null)
    {
        $faculty = Faculty::findOrFail($id);

        $data = [
            'user' => Auth::user(),
            'pageTitle' => 'Edit Faculty',
            'departments' => [
                'Bachelor of Science in Criminology',
                'Bachelor of Science in Information Technology',
                'Bachelor of Science in Nursing',
                'Bachelor of Science in Hospitality Management',
                'Bachelor of Science in Radiologic Technology',
                'Diploma in Hotel and Restaurant Technology'
            ],
            'faculty' => $faculty
        ];
        return view('back.pages.faculty.edit_faculty', $data);
    }
    public function updateFaculty(Request $request)
{
    $faculty = Faculty::findOrFail($request->faculty_id);
    $faculty_image_name = $faculty->profile_picture; // ✅ fix: correct column

    $request->validate([
        'faculty_name' => 'required|string',
        'faculty_position' => 'required|string',
        'department' => 'required|string',
        'faculty_image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
    ]);

    if ($request->hasFile('faculty_image')) {
        $old_faculty_image = $faculty->profile_picture;
        $path = '/images/faculty/';
        $file = $request->file('faculty_image');
        $filename = $file->getClientOriginalName();
        $new_filename = time() . '_' . $filename;
        
        // ✅ Delete old file if it exists
        if ($old_faculty_image && File::exists(public_path($path . $old_faculty_image))) {
            File::delete(public_path($path . $old_faculty_image));
        }
         $file->move(public_path($path), $new_filename);

        $faculty_image_name = $new_filename;
    }

    // ✅ Update fields
    $faculty->faculty_name = $request->faculty_name;
    $faculty->faculty_position = $request->faculty_position;
    $faculty->department = $request->department;
    $faculty->profile_picture = $faculty_image_name; // use the same image if no new one is uploaded

    $saved = $faculty->save();

    return response()->json([
        'status' => $saved ? 1 : 0,
        'message' => $saved ? 'Faculty has been successfully updated.' : 'Something went wrong.'
    ]);
}

}
