<?php

namespace App\Http\Controllers;

use App\Models\Plant;
use App\Models\User;
// use Carbon\Carbon;
use Hash;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    // show(string $id)
    public function get_user_data(Request $request)
    {
        $expectedUser = env('API_AUTH_USER');
        $expectedPw = env('API_AUTH_PW');
        $header_auth = $request->header('Authorization');
        $header_user = $request->header('User-Name');
        $header_pass = $request->header('User-Pass');
        $expectedToken = $expectedUser.':'.$expectedPw;

        if ('Bearer '.$expectedToken != $header_auth) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Invalid authorization token!',
            ], 403);
        }

        if (! $header_user) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Invalid user name found!',
            ], 400);
        } elseif (! $header_pass) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Invalid password found!',
            ], 400);
        }

        $existUser = User::where('name', $header_user)->first();
        $existPlant = 'All Plants';

        if (! $existUser) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Unknown user name found!',
            ], 400);
        } else {
            $codeExist = Plant::where('id', $existUser->plant_id)->first();
            if ($codeExist) {
                $existPlant = $codeExist->code;
            }
        }

        // Retrieve the user by email
        // $user = User::where('email', $email)->first();
        if (Hash::check($header_pass, $existUser->password)) {
            return response()->json([
                'created_at' => $existUser->created_at->format('Y-m-d H:i:s') ?? '',
                'updated_at' => $existUser->updated_at->format('Y-m-d H:i:s') ?? '',
                'requested_at' => now()->format('Y-m-d H:i:s') ?? '', // Carbon::now(config('app.timezone'))->format('Y-m-d H:i:s') ?? "",
                'plant' => (string) $existPlant ?? '',
                'email' => $existUser->email ?? '',
                'roles' => $existUser->roles()->pluck('name')->toArray(),
            ], 200);
        } else {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Password does not match!',
            ], 400);
        }

        // $machines = User::with('plant')->with('line')->orderBy('plant_id')->get();
        // $machinesData = $machines->map(function($machine) {
        //     return [
        //         'plant_code'          => $machine->plant ? (String)$machine->plant->code : "",
        //         'group_work_center'   => $machine->line->group_work_center ?? "",
        //         'work_center'         => $machine->work_center ?? "",
        //     ];
        // });

        // return response()->json([
        //     'machines' => $machinesData
        // ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
