<?php

namespace App\Controllers\Api;

use App\Models\SettingModel;

class VersionController extends BaseApiController
{
    /**
     * GET api/version
     * Public endpoint (no auth). Returns minimum required app version and store URLs.
     * App compares current version with min_version; only show update options for non-empty URLs.
     */
    public function index()
    {
        $settings = model(SettingModel::class);

        $minVersion = $settings->getValue('app_min_version');
        $minVersion = $minVersion !== null && $minVersion !== '' ? $minVersion : null;

        $google   = $settings->getValue('app_store_url_google');
        $apple    = $settings->getValue('app_store_url_apple');
        $huawei   = $settings->getValue('app_store_url_huawei');
        $directApk = $settings->getValue('app_direct_apk_url');

        $storeUrls = [
            'google_play'       => $google !== null && trim($google) !== '' ? trim($google) : null,
            'app_store'         => $apple !== null && trim($apple) !== '' ? trim($apple) : null,
            'huawei_app_gallery' => $huawei !== null && trim($huawei) !== '' ? trim($huawei) : null,
            'direct_apk'        => $directApk !== null && trim($directApk) !== '' ? trim($directApk) : null,
        ];

        return $this->success([
            'min_version' => $minVersion,
            'store_urls'  => $storeUrls,
        ]);
    }
}
