<?php

namespace App\Http\Controllers\people;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Helpers\HelperController;
use App\Http\Controllers\UsersController;
use App\Http\Resources\ControllersResource;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class PlantControllersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('people.plant_controller');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            //validate
            $validator = Validator::make($request->all(), [
                'name' => 'required',
                'email' => 'required|email|unique:users,email',
                'phone' => 'required|unique:users,phone',
            ]);
            if ($validator->fails()) {
                $result = [
                    'status' => 400,
                    'errors' => $validator->errors()
                ];

                return response()->json($result, 400);
            }
            $plants = [];
            //cast plant codes to json
            foreach ($request->plant_code as $key => $plant) {
                $plants[] = $plant['code'];
            };
            // check if active user exists
            //UsersController::validations($request->notation, $request->plant_code, $request->company_code);
            $name = $request->name;
            $email = $request->email;
            $phone = $request->phone;
            $password = Hash::make('Password');
            $role = $request->role;
            $notation = $request->notation;
            $plant_code = json_encode($plants);
            $company_code = $request->company_code;
            HelperController::save_plant_level($name, $email, $phone, $password, $role, $plant_code, $notation, $company_code);
            $res = [
                'status' => 200,
                'message' => 'User created successfully'
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'message' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function fetch_controllers()
    {
        // dd("Am in the right place");
        try {
            $users = User::where([['role', 'plant_controller'], ['status', 'active']])->latest()->get();
            $res = [
                'status' => 200,
                'users' => ControllersResource::collection($users)
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            return $th->getMessage();
            $res = [
                'status' => 500,
                'users' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    public function test()
    {
        $mystring = '["1100","1200","1300"]';
        if (str_contains($mystring, '!')) {
            return "yes";
        } else {
            return "no";
        }
        // $remove[] = "'";
        // $remove[] = '"';
        // $remove[] = '[';
        // $remove[] = ']';
        // $test = substr($mystring, 1, -1);
        // $test_a = "'" . $test . "'";

        // $myarray = str_replace($remove, "", $mystring);
        // $data = explode(",", $myarray);
        // foreach ($data as $key => $value) {
        //     return $value;
        // };
    }
}