<?php

namespace App\Http\Controllers;

use App\Models\Fasilitas;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class FasilitasController extends Controller
{

    /**
     * Display a listing of the resource.
     */
    
    public function index()
    {
        try {
            $fasilitas = Fasilitas::all();
            return response()->json([
                'status' => 'success',
                'data' => $fasilitas
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve facilities: ' . $e->getMessage()
            ], 500);
        }
    }

    public function indexAll()
    {
        try {
            $fasilitas = Fasilitas::withTrashed()->get();
            return response()->json([
                'status' => 'success',
                'data' => $fasilitas
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve facilities: ' . $e->getMessage()
            ], 500);
        }
    }

    

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'facility_name' => 'required|string|max:255|unique:facilities',
                'icon' => 'nullable|string|max:255'
            ]);

            if ($validator->fails()) {
                return response()->json([
                    'status' => 'error',
                    'message' => 'Validation failed',
                    'errors' => $validator->errors()
                ], 422);
            }

            $fasilitas = Fasilitas::create($request->all());

            return response()->json([
                'status' => 'success',
                'message' => 'Facility created successfully',
                'data' => $fasilitas
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to create facility: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(Fasilitas $fasilitas)
    {
        try {
            return response()->json([
                'status' => 'success',
                'data' => $fasilitas
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve facility: ' . $e->getMessage()
            ], 500);
        }
    }

    
    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        try {
            $fasilitas = Fasilitas::findOrFail($id);

            $validator = Validator::make($request->all(), [
                'facility_name' => 'sometimes|string|max:255',
                'icon' => 'nullable|string|max:255'
            ]);

            if ($validator->fails()) {
                return response()->json([
                    'status' => 'error',
                    'message' => 'Validation failed',
                    'errors' => $validator->errors()
                ], 422);
            }

            $fasilitas->update($request->all());

            return response()->json([
                'status' => 'success',
                'message' => 'Facility updated successfully',
                'data' => $fasilitas
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to update facility: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        try {
            $fasilitas = Fasilitas::findOrFail($id);
            
            $fasilitas->delete();

            return response()->json([
                'status' => 'success',
                'message' => 'Facility deleted successfully'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to delete facility: ' . $e->getMessage()
            ], 500);
        }
    }


    public function trashed()
    {
        try {
            $trashedFasility = Fasilitas::onlyTrashed()->get();
            
            if ($trashedFasility->isEmpty()) {
                return response()->json([
                    'success' => true,
                    'message' => 'Tidak ada data fasilitas yang dihapus.',
                    'data' => []
                ], 200);
            }

            return response()->json([
                'success' => true,
                'message' => 'Data fasilitas yang dihapus berhasil diambil.',
                'data' => $trashedFasility
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal mengambil data fasilitas yang dihapus.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function restore(string $id)
    {
        try {
            $type = Fasilitas::onlyTrashed()->findOrFail($id);
            $type->restore();

            return response()->json([
                'success' => true,
                'message' => 'User Fasilitas restored successfully.',
                'data' => $type
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to restore user Fasilitas.',
                'error' => $e->getMessage()
            ], 500);
        }
    }

    public function forceDestroy(string $id)
    {
        try {
            $type = Fasilitas::onlyTrashed()->findOrFail($id);
            $type->forceDelete();

            return response()->json([
                'success' => true,
                'message' => 'Fasilitas permanently deleted successfully.'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to permanently delete Fasilitas.',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}
