<?php

namespace App\Http\Controllers;

use App\Models\Department;
use App\Models\Doctor;
use App\Models\Specialty;
use Illuminate\Http\Request;

class DoctorController extends Controller
{
    public function index()
    {
        $doctors = Doctor::latest()->get();
        return view('admin.doctors.index', compact('doctors'));
    }

    public function create()
    {
        $departments = Department::all();
        $specialties = Specialty::all();
        return view('admin.doctors.form', compact('departments', 'specialties'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:doctors,slug',
            'specialty_id' => 'required|exists:specialties,id',
            'department_id' => 'required|exists:departments,id',
            'experience' => 'required|string|max:255',
            'degree' => 'required|string|max:255',
            'designation' => 'required|string|max:255',
            'desc' => 'required|string',
            'show_home' => 'nullable',
            'image' => 'nullable|image|max:2048',
        ]);

        // Handle image upload
        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $destination = public_path('images/doctors');

            if (!file_exists($destination)) {
                mkdir($destination, 0755, true);
            }

            $file->move($destination, $filename);
            $data['image'] = 'images/doctors/' . $filename;
        }
        $data['show_home'] = $request->has('show_home') ? true : false;
        Doctor::create($data);

        return redirect()->route('doctor.index')->with('success', 'Doctor created successfully.');
    }


    public function edit(Doctor $doctor)
    {
        $departments = Department::all();
        $specialties = Specialty::all();
        return view('admin.doctors.form', compact('doctor', 'departments', 'specialties'));
    }


    public function update(Request $request, $id)
    {
        $doctor = Doctor::findOrFail($id);

        $data = $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:doctors,slug,' . $doctor->id,
            'specialty_id' => 'required|exists:specialties,id',
            'department_id' => 'required|exists:departments,id',
            'experience' => 'required|string|max:255',
            'degree' => 'required|string|max:255',
            'designation' => 'required|string|max:255',
            'desc' => 'required|string',
            'show_home' => 'nullable',
            'image' => 'nullable|image|max:2048',
        ]);

        if ($request->hasFile('image')) {
            // Delete old image if exists
            if ($doctor->image && file_exists(public_path($doctor->image))) {
                unlink(public_path($doctor->image));
            }

            // Upload new image
            $file = $request->file('image');
            $filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $destination = public_path('images/doctors');

            if (!file_exists($destination)) {
                mkdir($destination, 0755, true);
            }

            $file->move($destination, $filename);
            $data['image'] = 'images/doctors/' . $filename;
        } else {
            $data['image'] = $doctor->image;
        }
        $data['show_home'] = $request->has('show_home') ? true : false;
        $doctor->update($data);

        return redirect()->route('doctor.index')->with('success', 'Doctor updated successfully.');
    }

    public function destroy($id)
    {
        $doctor = Doctor::findOrFail($id);

        // Delete image from public path
        if ($doctor->image && file_exists(public_path($doctor->image))) {
            unlink(public_path($doctor->image));
        }

        $doctor->delete();

        return redirect()->route('doctor.index')->with('success', 'Doctor deleted successfully.');
    }
}
