<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Extension;
use Illuminate\Http\Request;

class ExtensionController extends Controller
{
    public function index()
    {
        $pageTitle  = 'Extensions';
        $extensions = Extension::orderBy('name')->get();
        return view('admin.extension.index', compact('pageTitle', 'extensions'));
    }

    public function update(Request $request, $id)
    {
        $extension       = Extension::findOrFail($id);
        $validationRule = [];

        foreach ($extension->shortcode as $key => $val) {
            $validationRule = array_merge($validationRule, [$key => 'required']);
        }

        $request->validate($validationRule);

        $shortcode = json_decode(json_encode($extension->shortcode), true);

        foreach ($shortcode as $key => $value) {
            $shortcode[$key]['value'] = $request->$key;
        }

        $extension->shortcode = $shortcode;
        $extension->save();
        $notify[] = ['success', $extension->name . ' updated successfully'];
        return back()->withNotify($notify);
    }

    public function status($id)
    {
        return Extension::changeStatus($id);
    }
}
