<?php

namespace App\Apps\Versionmgr\Controllers;

use App\Apps\Versionmgr\Services\UpgradeService;
use App\Apps\Versionmgr\Services\VersionCheckService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class VersionController extends Controller
{
    protected VersionCheckService $versionCheckService;

    protected UpgradeService $upgradeService;

    public function __construct(
        VersionCheckService $versionCheckService,
        UpgradeService $upgradeService
    ) {
        $this->versionCheckService = $versionCheckService;
        $this->upgradeService = $upgradeService;
    }

    public function current()
    {
        return response()->json($this->versionCheckService->getCurrentVersion());
    }

    public function check()
    {
        return response()->json($this->versionCheckService->checkUpdate());
    }

    public function skip(Request $request)
    {
        $request->validate([
            'version' => 'required|string',
            'build' => 'required|string',
        ]);

        return response()->json(
            $this->versionCheckService->skipVersion($request->version, $request->build)
        );
    }

    public function upgrade(Request $request)
    {
        $targetVersion = $request->input('target_version', '');

        return response()->json($this->upgradeService->startUpgrade($targetVersion));
    }

    public function upgradeStatus()
    {
        return response()->json($this->upgradeService->getUpgradeStatus());
    }

    public function rollback()
    {
        return response()->json($this->upgradeService->rollback());
    }

    public function upgradePath(Request $request)
    {
        $target = $request->input('target', '');

        return response()->json(
            $this->versionCheckService->getUpgradePathDetail($target)
        );
    }
}
