<?php

namespace Tests\Feature;

use App\Models\Episode;
use App\Models\Gallery;
use App\Models\Hentai;
use App\Models\Studios;
use App\Services\GalleryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class GalleryServiceTest extends TestCase
{
    use RefreshDatabase;

    protected $galleryService;

    protected function setUp(): void
    {
        parent::setUp();
        $this->galleryService = new GalleryService;
    }

    public function test_create_or_update_gallery()
    {
        // Fake the storage disk
        Storage::fake('public');

        // Create a Hentai and Episode instance
        $hentai = Hentai::factory()->create();
        $studio = Studios::factory()->create();
        $episode = Episode::factory()->create(['hentai_id' => $hentai->id, 'studios_id' => $studio->id]);

        // Create a fake image file
        $file = UploadedFile::fake()->image('test_image.jpg');

        // Create a request with the fake file
        $request = new Request;
        $request->files->add(['episodegallery1' => [$file]]);

        // Call the method to test
        $this->galleryService->createOrUpdateGallery($request, $hentai, $episode, $episode->episode, true);

        $imageURL = "/images/hentai/{$hentai->slug}/gallery-ep-{$episode->episode}-0.webp";
        $thumbnailURL = "/images/hentai/{$hentai->slug}/gallery-ep-{$episode->episode}-0-thumbnail.webp";

        // Assert that the gallery was created
        $this->assertDatabaseHas('gallery', [
            'hentai_id' => $hentai->id,
            'episode_id' => $episode->id,
            'image_url' => $imageURL,
            'thumbnail_url' => $thumbnailURL,
        ]);

        // Assert that the image and thumbnail were saved
        $this->assertTrue(Storage::disk('public')->exists($imageURL));
        $this->assertTrue(Storage::disk('public')->exists($thumbnailURL));
    }
}
