<?php

namespace Tests\Feature;

use App\Models\Gallery;
use App\Models\Image;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class SparkboothUploadTest extends TestCase
{
    use RefreshDatabase;

    public function test_json_upload_with_credentials_succeeds(): void
    {
        Storage::fake('public');

        $gallery = Gallery::factory()->create([
            'upload_enabled' => true,
            'images_path' => 'uploads/test',
            'sparkbooth_username' => 'spark-user',
            'sparkbooth_password' => 'secret-123',
            'sparkbooth_response_format' => 'json',
        ]);

        $file = UploadedFile::fake()->image('photo.jpg', 800, 600);

        $response = $this->postJson(route('api.sparkbooth.upload'), [
            'username' => 'spark-user',
            'password' => 'secret-123',
            'media' => $file,
            'name' => 'Guest',
            'message' => 'Hallo',
        ]);

        $response->assertOk()
            ->assertJson([
                'status' => true,
                'error' => null,
            ])
            ->assertJsonStructure(['url']);

        $image = Image::first();
        $this->assertNotNull($image);
        $this->assertSame($gallery->id, $image->gallery_id);
        $this->assertStringStartsWith('uploads/test/sparkbooth_', $image->path);

        Storage::disk('public')->assertExists($image->path);
    }

    public function test_xml_response_for_invalid_credentials(): void
    {
        Storage::fake('public');

        Gallery::factory()->create([
            'upload_enabled' => true,
            'images_path' => 'uploads/test',
            'sparkbooth_username' => 'spark-user',
            'sparkbooth_password' => 'secret-123',
            'sparkbooth_response_format' => 'xml',
        ]);

        $file = UploadedFile::fake()->image('photo.jpg', 800, 600);

        $response = $this->post(route('api.sparkbooth.upload'), [
            'username' => 'spark-user',
            'password' => 'wrong',
            'media' => $file,
            'response_format' => 'xml',
        ]);

        $response->assertStatus(403);
        $response->assertHeader('Content-Type', 'application/xml');
        $this->assertStringContainsString('<rsp status="fail">', $response->getContent());
    }

    public function test_base64_upload_with_token_succeeds(): void
    {
        Storage::fake('public');

        $gallery = Gallery::factory()->create([
            'upload_enabled' => true,
            'images_path' => 'uploads/base64',
            'sparkbooth_response_format' => 'json',
        ]);

        $token = 'tokentest123';
        $gallery->setUploadToken($token);
        $gallery->save();

        $fake = UploadedFile::fake()->image('inline.png', 20, 20);
        $binary = base64_encode(file_get_contents($fake->getRealPath()));
        $dataUri = 'data:image/png;base64,'.$binary;

        $response = $this->postJson(route('api.sparkbooth.upload'), [
            'token' => $token,
            'media' => $dataUri,
            'filename' => 'custom.png',
        ]);

        $response->assertOk()
            ->assertJson([
                'status' => true,
                'error' => null,
            ])
            ->assertJsonStructure(['url']);

        Storage::disk('public')->assertExists('uploads/base64/custom.png');

        $this->assertDatabaseHas('images', [
            'gallery_id' => $gallery->id,
            'path' => 'uploads/base64/custom.png',
        ]);
    }
}
