<?php

namespace App\Http\Controllers;

use App\Models\Leadership;
use Illuminate\Http\Request;

class LeadershipController extends Controller
{
    public function index()
    {
        $leaders = Leadership::latest()->where('is_ceo', false)->get();
        $ceo = Leadership::where('is_ceo', true)->first();

        return view('admin.leadership.index', compact('leaders', 'ceo'));
    }

    public function create()
    {
        return view('admin.leadership.form');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email',
            'degree' => 'nullable|string|max:255',
            'position' => 'required|string|max:255',
            'image' => 'nullable|image|max:2048',
            'short_description' => 'nullable|string|max:500',
            'description' => 'nullable|string',
        ]);

        $data['is_ceo'] = $request->has('is_ceo');

        // Optional: Ensure only one CEO
        if ($data['is_ceo']) {
            Leadership::where('is_ceo', true)->update(['is_ceo' => false]);
        }

        if ($request->hasFile('image')) {
            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('images/leadership'), $imageName);
            $data['image'] = 'images/leadership/' . $imageName;
        }
        Leadership::create($data);
        return redirect()->route('leadership.index')->with('success', 'Leadership member added successfully
        ');
    }
    public function edit($id)
    {
        $leadership = Leadership::findOrFail($id);
        return view('admin.leadership.form', compact('leadership'));
    }

    public function update(Request $request, $id)
    {
        $leader = Leadership::findOrFail($id);

        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email',
            'degree' => 'required|string|max:255',
            'position' => 'required|string|max:255',
            'image' => 'nullable|image|max:2048',
            'short_description' => 'nullable|string|max:500',
            'description' => 'nullable|string',
        ]);

        $data['is_ceo'] = $request->has('is_ceo');

        if ($data['is_ceo']) {
            Leadership::where('is_ceo', true)
                ->where('id', '!=', $leader->id)
                ->update(['is_ceo' => false]);
        }

        if ($request->hasFile('image')) {
            if ($leader->image && file_exists(public_path($leader->image))) {
                unlink(public_path($leader->image));
            }

            $imageName = time() . '.' . $request->image->extension();
            $request->image->move(public_path('images/leadership'), $imageName);
            $data['image'] = 'images/leadership/' . $imageName;
        } else {
            $data['image'] = $leader->image;
        }

        $leader->update($data);

        return redirect()->route('leadership.index')->with('success', 'Leadership member updated successfully');
    }

    public function destroy($id)
    {
        $leader = Leadership::findOrFail($id);
        if ($leader->image && file_exists(public_path($leader->image))) {
            unlink(public_path($leader->image));
        }
        $leader->delete();
        return redirect()->route('leadership.index')->with('success', 'Leadership member deleted successfully');
    }
}
