#include "..\script_component.hpp"

/*
 * File: fnc_initVGStore.sqf
 * Author: IDSolutions
 * Date: 2025-12-17
 * Last Update: 2026-01-30
 * Public: No
 *
 * Description:
 * Initializes the Virtual Garage store for managing player vehicle unlocks.
 * Provides methods for syncing, saving, and applying virtual vehicles to BIS Garage.
 *
 * Arguments:
 * None
 *
 * Return Value:
 * VG store object [HASHMAP OBJECT]
 *
 * Example:
 * call forge_server_garage_fnc_initVGStore
 */

#pragma hemtt ignore_variables ["_self"]
GVAR(VGarageModel) = compileFinal createHashMapObject [[
    ["#type", "VGarageModel"],
    ["defaults", {
        private _vGarage = createHashMap;

        _vGarage set ["armor", []];
        _vGarage set ["cars", []];
        _vGarage set ["helis", []];
        _vGarage set ["naval", []];
        _vGarage set ["other", []];
        _vGarage set ["planes", []];

        _vGarage
    }]
]];

GVAR(VGarageRepository) = compileFinal createHashMapFromArray [
    ["#base", EGVAR(common,BaseStore)],
    ["#type", "VGarageRepository"],
    ["#create", {
        GVAR(VGarageRegistry) = createHashMap;
        ["INFO", "VGarage Repository Initialized!"] call EFUNC(common,log);
    }],
    ["get", {
        params [["_uid", "", [""]]];

        private _cached = GVAR(VGarageRegistry) getOrDefault [_uid, nil];
        if !(isNil { _cached }) exitWith { _cached };

        ["owned:garage:exists", [_uid]] call EFUNC(extension,extCall) params ["_result", "_isSuccess"];
        if !(_isSuccess) exitWith {
            ["ERROR", format ["Failed to check if virtual garage %1 exists!", _uid]] call EFUNC(common,log);
            createHashMap
        };

        private _finalVGarage = createHashMap;

        if (_result == "true") then {
            _finalVGarage =  _self call ["fetch", ["owned:garage:fetch", _uid]];
            ["INFO", format ["Found virtual garage for %1", _uid]] call EFUNC(common,log);
        } else {
            _finalVGarage = GVAR(VGarageModel) call ["defaults", []];

            ["owned:garage:create", [_uid]] call EFUNC(extension,extCall) params ["_result", "_isSuccess"];
            if !(_isSuccess) exitWith {
                ["ERROR", format ["Failed to create virtual garage for %1!", _uid]] call EFUNC(common,log);
                createHashMap
            };

            ["INFO", format ["Created new virtual garage for %1", _uid]] call EFUNC(common,log);
        };

        GVAR(VGarageRegistry) set [_uid, _finalVGarage];
        _finalVGarage
    }]
];

GVAR(VGarageStore) = createHashMapObject [[
    ["#base", GVAR(VGarageRepository)],
    ["#type", "VGarageStore"],
    ["#create", {
        ["INFO", "VGarage Store Initialized!"] call EFUNC(common,log);
    }],
    ["init", {
        params [["_uid", "", [""]]];

        private _vGarage = _self call ["get", [_uid]];
        private _player = [_uid] call EFUNC(common,getPlayer);

        [CRPC(garage,responseInitVG), [_vGarage], _player] call CFUNC(targetEvent);
    }]
]];

GVAR(VGarageStore)
