<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Feature;
use Illuminate\Support\Facades\Validator;
use Exception;

class FeatureController extends Controller
{
    /**
     * Display All Features withoutTrashed
    */
    public function index()
    {
        try {
            $features = Feature::latest()->get();
            if ($features->isEmpty()) {
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!.']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['features' => $features];
            return response()->json([
                'data' => $data,
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Create Feature
    */
    public function store(Request $request)
    {
        try {
            $validation = Validator::make($request->all(), [
                'name' => 'required|string',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            $feature = Feature::create([
                'name' => $request->name,
            ]);

            $data = ['feature' => $feature];
            return response([
                'response_status' => 200,
                'response_message' => ['success' => ['Feature Created Successfully!']],
                'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Display Single Feature
    */
    public function show($id)
    {
        try {
            $feature = Feature::find($id);
            if(!$feature){
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }
            $data = ['feature' => $feature];
            return response()->json([
                'data' => $data,
                'response_status' => 200
        ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Update Feature.
    */
    public function update(Request $request, string $id)
    {
        try {
            $validation = Validator::make($request->all(), [
                'name' => 'required|string',
            ]);

            if ($validation->fails()) {
                return response()->json([
                    'validation_errors' => $validation->messages(),
                    'response_status' => 400,
                ], 200);
            }

            $feature = Feature::find($id);
            if(!$feature){
                return response()->json([
                //   'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }

            $feature->name = $request->name;
            $feature->save();

            $data = ['feature' => $feature];
            return response([
                'response_status' => 200,
                'response_message' => ['success' => ['Feature Updated Successfully!']],
                'data' => $data
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }

    /**
     * Permanantly Remove Feature.
    */
    public function destroy(string $id)
    {
        try {
            $feature = Feature::find($id);
            if(!$feature){
                return response()->json([
                    // 'validation_errors' => ['not_found' => ['No Data Found!']],
                    'response_status' => 400
                ], 200);
            }

            $feature->delete();

            return response()->json([
                'response_message' => ['success' => ['Feature Deleted Successfully!']],
                'response_status' => 200
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'validation_errors' => $e->getMessage(),
                'response_status' => 400,
            ], 200);
        }
    }
}
