<?php

namespace Kylin\Licensing\Http\Controllers;

use Illuminate\Routing\Controller;
use Kylin\Core\ApiResponse;
use Kylin\Licensing\Requests\ActivateLicenseRequest;
use Kylin\Licensing\Services\LicenseService;
use Kylin\Licensing\Services\LicenseVerifier;

/**
 * 许可证控制器
 */
class LicenseController extends Controller
{
    public function __construct(
        private readonly LicenseService $licenseService,
        private readonly LicenseVerifier $verifier,
    ) {}

    public function status(): \Illuminate\Http\JsonResponse
    {
        $summary = $this->licenseService->getStatusSummary();
        return ApiResponse::success($summary);
    }

    public function activate(ActivateLicenseRequest $request): \Illuminate\Http\JsonResponse
    {
        $data = $this->licenseService->activate(
            $request->validated('license_key'),
            $request->validated('domain'),
            $request->validated('ip'),
        );
        return ApiResponse::created($data, '许可证激活成功');
    }

    public function deactivate(): \Illuminate\Http\JsonResponse
    {
        $result = $this->licenseService->deactivate();
        if (! $result) {
            return ApiResponse::error('LCS-NOTFOUND-001', '未找到许可证', 404);
        }
        return ApiResponse::success(null, '许可证已停用');
    }
}
