<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\NamaPos;
use App\Models\SoftSettings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ConfigParameter extends Controller
{
    public function index(Request $request)
    {
        // $query = $request->query('gate');
        $query = $_GET['gate'] ?? null;

        Log::info('Query Params:', $request->query());
        Log::info('Gate Param:', [$request->query('gate')]);



        $getLocation = SoftSettings::first();
        
        $location = [
            'namaSoftware' => $getLocation->namasoft ?? 'Lokasi belum di set',
            'namaSystem' => $getLocation->namasystem ?? 'Alamat belum di set',
            'LocatioName' => $getLocation->namalokasi ?? 'Alamat belum di set',
            'locationAddress' => $getLocation->alamatlokasi ?? 'location Undefined',
            'CompanyName' => $getLocation->namaperusahaan ?? 'Nama perusahaan belum di set',
            'merchantID' => $getLocation->cabang_lokasi ?? 'Merchant belum di set',
        ];

        if (!array_key_exists('gate', $_GET)) {
            return response()->json([
                'status' => 'success',
                // 'desc' => 'Parameter undefined',
                'details' => $location
            ],400);

        }

        if ($query === '') {
            return response()->json([
                'status' => 'failed',
                'desc' => 'Parameter gate tidak boleh kosong',
            ], 400);
        }


        if($query) {
            $getPos = NamaPos::where('id', $request->query('gate'))->with('posParams','posHardware')->first();
            $location['gateDetails'] = $getPos;
            // Log::info($getPos->toSql());
            return response()->json([
                'status' => 'success',
                'details' => $location
            ]);
        }

        return response()->json([
            'status' => 'success',
            'details' => $location,
        ]);
    }
}
