<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Attributes\Test;

class BuildOrderControllerTest extends TestCase
{
    use RefreshDatabase;

    private array $sampleRequestData = [
        'package_name' => 'com.example.app',
        'app_name' => 'Example App',
        'domain' => 'example.com',
        'base_suffix' => 'example',
        'base_url' => 'https://example.com',
        'build_number' => '1.0.0',
        'icon_url' => 'https://example.com/icon.png',
        'jks_url' => 'https://example.com/jks.jks',
        'key_properties_url' => 'https://example.com/key_properties.json',
        'issuer_id' => '123456789',
        'key_id' => '123456789',
        'api_key_url' => 'https://sample-url.com/something.p8',
        'team_id' => '123456789',
        'app_identifier' => 'com.lazycoders.halerfashion'
    ];

    #[Test]
    public function cannot_create_build_request_without_token(): void
    {
        $responseUnauthorized = $this->postJson('/api/build/ios', $this->sampleRequestData);
        $responseUnauthorized->assertStatus(401);

        $token = $this->makeValidToken();
        $responseOk = $this
            ->withHeaders($this->makeRequestHeaders($token))
            ->postJson('/api/build/ios', $this->sampleRequestData);

        $responseOk->assertStatus(200);
    }

    #[Test]
    public function ios_fields_are_required_unless_android_only_build(): void
    {
        $token = $this->makeValidToken();

        $iOSFields = ['issuer_id', 'key_id', 'api_key_url', 'team_id', 'app_identifier'];
        $dataWithoutIOS = Arr::except($this->sampleRequestData, $iOSFields);

        $responseInvalidForIOS = $this
            ->withHeaders($this->makeRequestHeaders($token))
            ->postJson('/api/build/ios', $dataWithoutIOS);

        $responseInvalidForIOS->assertStatus(422);
        $responseInvalidForIOS->assertJsonValidationErrors($iOSFields);


    }

    #[Test]
    public function android_fields_are_required_unless_ios_only_build(): void
    {
        $token = $this->makeValidToken();

        $androidFields = ['jks_url', 'key_properties_url'];
        $dataWithoutAndroid = Arr::except($this->sampleRequestData, $androidFields);

        $responseInvalidForAndroid = $this
            ->withHeaders($this->makeRequestHeaders($token))
            ->postJson('/api/build/android', $dataWithoutAndroid);

        $responseInvalidForAndroid->assertStatus(422);
        $responseInvalidForAndroid->assertJsonValidationErrors($androidFields);
    }

    private function makeValidToken(): string
    {
        $token = fake()->uuid();

        User::factory()->create()
            ->tokens()->create([
                'name' => 'test_token',
                'token' => hash('sha256', $token),
                'abilities' => ['build:make'],
            ]);

        return $token;
    }

    private function makeRequestHeaders(string $token): array
    {
        return [
            'Authorization' => 'Bearer ' . $token,
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ];
    }
}
