<?php

namespace App\Http\Controllers\APIS\AdminApi;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\DeviceProfileMaster;
use Illuminate\Support\Facades\Log;
use App\Services\AdminDeviceProfileMaster;  // Include the service

use Exception;




class DeviceProfileMasterController extends Controller

{
    protected $AdminDeviceProfileMaster;

    public function __construct(AdminDeviceProfileMaster $AdminDeviceProfileMaster)
    {
        $this->AdminDeviceProfileMaster = $AdminDeviceProfileMaster;
    }

    public function deviceprofileMasterList()
    {
        try {
            $deviceMaster = DeviceProfileMaster::select('id', 'name')->get();

            return jsonResponseWithSuccessMessage('Device profile master fetched successfully', [
                'deviceprofilemaster' => $deviceMaster
            ]);
        } catch (Exception $e) {
            Log::error("An error occurred: " . $e->getMessage());
            return jsonResponseWithErrorMessage($e->getMessage(), 500);
        }
    }

    public function updateDevice(Request $request, $deviceId)
    {
        try {
            $deviceProfileMaster = DeviceProfileMaster::find($deviceId);
            if (!$deviceProfileMaster) {
                return jsonResponseWithErrorMessage('No device found', 404);
            }
            $request->validate([
                'name' => 'required|string|max:255'
            ]);
            $deviceProfileMaster->name = $request->name;
            $deviceProfileMaster->save();
            $updatedDevice = $deviceProfileMaster->only(['id', 'name']);
            return jsonResponseWithSuccessMessage('Device name updated successfully', [
                'device' => $updatedDevice
            ]);
        } catch (Exception $e) {
            return jsonResponseWithErrorMessage('Failed to update device: ' . $e->getMessage(), 500);
        }
    }
}
