<?php

namespace App\Http\Controllers;

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

class CommitmentController extends Controller
{
    public function index()
    {
        $commitments = Commitment::where('type', 1)->get();
        $type = 1;
        return view('admin.commitments.commitment', compact('commitments', 'type'));
    }

    public function create()
    {
        return view('admin.commitments.form');
    }
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'type' => 'required|in:1,2,3,4',
            'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
        ]);

        $data = $request->only(['name', 'description', 'type']);

        // Handle image upload
        if ($request->hasFile('image')) {
            $data['image'] = $this->uploadImage($request->file('image'));
        }

        Commitment::create($data);

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

    public function edit($id)
    {
        $Commitment = Commitment::findOrFail($id);
        return view('admin.commitments.form', compact('Commitment'));
    }

    public function update(Request $request, $id)
    {
        $commitment = Commitment::findOrFail($id);

        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'type' => 'required|in:1,2,3,4',
            'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
        ]);

        $data = $request->only(['name', 'description', 'type']);

        // Handle new image upload
        if ($request->hasFile('image')) {
            // Optionally delete old image
            if ($commitment->image && file_exists(public_path($commitment->image))) {
                unlink(public_path($commitment->image));
            }

            $data['image'] = $this->uploadImage($request->file('image'));
        }

        $commitment->update($data);

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

    private function uploadImage($image)
    {
        $imageName = time() . '.' . $image->extension();
        $image->move(public_path('images/commitments'), $imageName);
        return 'images/commitments/' . $imageName;
    }


    public function destroy($id)
    {
        $commitment = Commitment::findOrFail($id);

        // Optionally delete the image file
        if ($commitment->image && file_exists(public_path($commitment->image))) {
            unlink(public_path($commitment->image));
        }

        $commitment->delete();

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





    public function indexValue()
    {
        $commitments = Commitment::where('type', 4)->get();
        $type = 4;
        return view('admin.commitments.commitment', compact('commitments', 'type'));
    }

    public function createValue()
    {
        return view('admin.commitments.valueform');
    }
    public function storeValue(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'type' => 'required|in:1,2,3,4',
            'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
        ]);

        $data = $request->only(['name', 'description', 'type']);

        // Handle image upload
        if ($request->hasFile('image')) {
            $data['image'] = $this->uploadImageValue($request->file('image'));
        }

        Commitment::create($data);

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

    public function editValue($id)
    {
        $Value = Commitment::findOrFail($id);

        return view('admin.commitments.valueform', compact('Value'));
    }

    public function updateValue(Request $request, $id)
    {
        $commitment = Commitment::findOrFail($id);

        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'type' => 'required|in:1,2,3,4',
            'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
        ]);

        $data = $request->only(['name', 'description', 'type']);

        // Handle new image upload
        if ($request->hasFile('image')) {
            // Optionally delete old image
            if ($commitment->image && file_exists(public_path($commitment->image))) {
                unlink(public_path($commitment->image));
            }

            $data['image'] = $this->uploadImageValue($request->file('image'));
        }

        $commitment->update($data);

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

    private function uploadImageValue($image)
    {
        $imageName = time() . '.' . $image->extension();
        $image->move(public_path('images/values'), $imageName);
        return 'images/values/' . $imageName;
    }


    public function destroyValue($id)
    {
        $commitment = Commitment::findOrFail($id);

        // Optionally delete the image file
        if ($commitment->image && file_exists(public_path($commitment->image))) {
            unlink(public_path($commitment->image));
        }

        $commitment->delete();

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


    public function mission()
    {
        $mission = Commitment::where('type', 3)->first();
        return view('admin.commitments.missionform', compact('mission'));
    }


    public function updateMission(Request $request, $id)
    {
        $mission = Commitment::findorFail($id);

        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        $data = $request->only(['name', 'description']);


        // Update existing mission
        $mission->update($data);
        $message = 'Mission updated successfully.';


        return back()->with('success', $message);
    }


    public function vision()
    {
        $vision = Commitment::where('type', 2)->first();
        return view('admin.commitments.visionform', compact('vision'));
    }


    public function updateVision(Request $request, $id)
    {
        $vision = Commitment::findorFail($id);

        $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
        ]);

        $data = $request->only(['name', 'description']);

        // Handle new image upload
        if ($request->hasFile('image')) {
            // Optionally delete old image
            if ($vision->image && file_exists(public_path($vision->image))) {
                unlink(public_path($vision->image));
            }

            $image = $request->file('image');
            $imageName = time() . '.' . $image->extension();
            $image->move(public_path('images/vision'), $imageName);
            $data['image'] = 'images/vision/' . $imageName;
        }

        $vision->update($data);
        $message = 'Vision updated successfully.';

        return back()->with('success', $message);
    }
}
