<?php

namespace Tests\Feature;

use App\Models\ClientAccount;
use App\Models\License;
use App\Models\Product;
use App\Models\Release;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ApiCenterTest extends TestCase
{
    use RefreshDatabase;

    public function test_health_endpoint_returns_service_status(): void
    {
        $this->getJson('/api/health')
            ->assertOk()
            ->assertJsonPath('status', 'ok')
            ->assertJsonPath('domain', 'api.nusaclouds.com');
    }

    public function test_admin_endpoint_requires_api_key(): void
    {
        config(['services.nusaclouds.admin_api_key' => 'secret']);

        $this->getJson('/api/v1/products')
            ->assertUnauthorized();
    }

    public function test_license_can_be_verified_for_allowed_domain(): void
    {
        $product = Product::query()->create([
            'slug' => 'wordpress-plugin',
            'name' => 'NusaClouds WordPress Plugin',
            'status' => 'active',
        ]);
        $client = ClientAccount::query()->create([
            'name' => 'PT Contoh Digital',
            'domains' => ['example.com'],
            'status' => 'active',
        ]);
        $license = License::query()->create([
            'product_id' => $product->id,
            'client_account_id' => $client->id,
            'domains' => ['example.com'],
            'status' => 'active',
        ]);

        $this->postJson('/api/v1/licenses/verify', [
            'license_key' => $license->key,
            'product_slug' => 'wordpress-plugin',
            'domain' => 'example.com',
        ])
            ->assertOk()
            ->assertJsonPath('data.valid', true)
            ->assertJsonPath('data.reason', 'ok');
    }

    public function test_update_check_returns_signed_download_when_newer_release_exists(): void
    {
        config(['services.nusaclouds.download_signing_key' => 'download-secret']);

        $product = Product::query()->create([
            'slug' => 'wordpress-plugin',
            'name' => 'NusaClouds WordPress Plugin',
            'status' => 'active',
        ]);
        $client = ClientAccount::query()->create([
            'name' => 'PT Contoh Digital',
            'domains' => ['example.com'],
            'status' => 'active',
        ]);
        $license = License::query()->create([
            'product_id' => $product->id,
            'client_account_id' => $client->id,
            'domains' => ['example.com'],
            'status' => 'active',
        ]);
        Release::query()->create([
            'product_id' => $product->id,
            'version' => '1.1.0',
            'channel' => 'stable',
            'file_url' => 'https://downloads.nusaclouds.com/wordpress-plugin/1.1.0.zip',
            'published_at' => now(),
        ]);

        $this->postJson('/api/v1/updates/check', [
            'license_key' => $license->key,
            'product_slug' => 'wordpress-plugin',
            'current_version' => '1.0.0',
            'domain' => 'example.com',
        ])
            ->assertOk()
            ->assertJsonPath('data.valid', true)
            ->assertJsonPath('data.update_available', true)
            ->assertJsonPath('data.latest_version', '1.1.0')
            ->assertJsonPath('data.download_url', fn (?string $url) => str_contains($url ?? '', '/api/v1/downloads/'));
    }

    public function test_signed_download_returns_release_url(): void
    {
        config(['services.nusaclouds.download_signing_key' => 'download-secret']);

        $product = Product::query()->create([
            'slug' => 'wordpress-plugin',
            'name' => 'NusaClouds WordPress Plugin',
            'status' => 'active',
        ]);
        $client = ClientAccount::query()->create([
            'name' => 'PT Contoh Digital',
            'status' => 'active',
        ]);
        $license = License::query()->create([
            'product_id' => $product->id,
            'client_account_id' => $client->id,
            'status' => 'active',
        ]);
        $release = Release::query()->create([
            'product_id' => $product->id,
            'version' => '1.1.0',
            'channel' => 'stable',
            'file_url' => 'https://downloads.nusaclouds.com/wordpress-plugin/1.1.0.zip',
            'published_at' => now(),
        ]);

        $token = app(\App\Services\SignedDownloadService::class)->sign([
            'release_id' => $release->id,
            'license_key' => $license->key,
            'expires_at' => now()->addMinutes(15)->timestamp,
        ]);

        $this->getJson('/api/v1/downloads/'.rawurlencode($token))
            ->assertOk()
            ->assertJsonPath('data.download_url', 'https://downloads.nusaclouds.com/wordpress-plugin/1.1.0.zip');
    }
}
