<?php

namespace App\Http\Controllers;

use App\Models\Facilities;

use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;

class FacilitiesController extends Controller
{
    public function allFacilities()
    {
       
        $data = [
            'pageTitle' => 'Facilities',
            'facilities' => Facilities::withCount('photos')->latest()->get()
        ];
        return view('back.pages.facility.facilities', $data);
    }

    public function addFacility()
    {
        $data = [
            'pageTitle' => 'Add Facility'
        ];
        return view('back.pages.facility.add_facility', $data);
    }

    public function createFacilities(Request $request)
    {
    $request->validate([
        'facility_name' => 'required|string|max:255',
        'description' => 'nullable|string',
        'cover_photo' => 'nullable|image|mimes:jpg,jpeg,png|max:4096',
    ]);

    $filename = null;

    if ($request->hasFile('cover_photo')) {
        $path = "images/facilities/";
        $file = $request->file('cover_photo');
        $originalName = $file->getClientOriginalName();
        $newFilename = time() . '_' . $originalName;

        $upload = $file->move(public_path($path), $newFilename);

        if (!$upload) {
            return response()->json(['status' => 0, 'message' => 'Failed to upload cover photo.']);
        }

        $filename = $newFilename;
    }

    $facility = new Facilities();
    $facility->name = $request->facility_name;
    $facility->description = $request->description;
    $facility->cover_photo = $filename;
    $facility->status = $request->has('status') ? 1 : 0;

    $saved = $facility->save();

    if ($saved) {
        return response()->json(['status' => 1, 'message' => 'Facility has been successfully created.']);
    } else {
        return response()->json(['status' => 0, 'message' => 'Something went wrong while saving the facility.']);
    }
}
public function editFacility($id)
{
    $facilities = Facilities::findOrFail($id);
    return view('back.pages.facility.edit_facility', compact('facilities'));
}

public function updateFacility(Request $request)
{
    $facilities = Facilities::findOrFail($request->id);

    // Validate fields
    $request->validate([
        'facility_name' => 'required|string|max:255',
        'description' => 'nullable|string',
        'cover_photo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:4096', // 2MB Max
    ]);

    try {
        $facilities->name = $request->facility_name;
        $facilities->description = $request->description;
        $facilities->status = $request->has('status') ? 1 : 0;

        // If new cover photo uploaded
        if ($request->hasFile('cover_photo')) {
            $path = 'images/facilities/';

            // Delete old cover photo if exists
            if ($facilities->cover_photo && File::exists(public_path($path . $facilities->cover_photo))) {
                File::delete(public_path($path . $facilities->cover_photo));
            }

            // Upload new one
            $file = $request->file('cover_photo');
            $fileName = uniqid() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path($path), $fileName);

            $facilities->cover_photo = $fileName;
        }

        $facilities->save();

        return response()->json([
            'status' => 1,
            'message' => 'Album updated successfully!'
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'status' => 0,
            'message' => 'Something went wrong. ' . $e->getMessage()
        ]);
    }
}

}
