<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

use App\Models\User;
use App\Models\ImageNode;

class MakeImageDBCommandTest extends TestCase
{
    use RefreshDatabase;

    protected string $imageDbPath;

    protected function setUp(): void {
        parent::setUp();

        // Set up fake imagedb directory
        $this->imageDbPath = public_path('test-imagedb');
        File::deleteDirectory($this->imageDbPath);
        File::makeDirectory($this->imageDbPath . '/subfolder', 0755, true);

        // Create a sample image file
        File::put($this->imageDbPath . '/subfolder/example.jpg', 'fake image content');
    }

    protected function tearDown(): void {
        File::deleteDirectory($this->imageDbPath);
        parent::tearDown();
    }
    
    /** @test */
    public function it_indexes_folders_and_images_in_the_imagedb_directory() {
        $user = User::factory()->create([
            'name' => 'Test User',
            'email' => 'test@example.com',
        ]);

        $this->artisan('pagebuilder:make-imagedb', ['--testmode' => true])
            ->expectsQuestion('Select a user to act as:', "{$user->name} ({$user->email})")
            ->expectsOutput('Clearing existing image nodes...')
            ->expectsOutput('Scanning and indexing image directory...')
            ->expectsOutput('Image DB creation complete.')
            ->assertExitCode(0);

        $this->assertDatabaseCount('image_nodes', 2); // 1 folder + 1 image

        $folder = ImageNode::where('fs_type', 'folder')->first();
        $image = ImageNode::where('fs_type', 'file')->first();

        $this->assertNotNull($folder);
        $this->assertEquals('subfolder', $folder->name);

        $this->assertNotNull($image);
        $this->assertEquals('example.jpg', $image->name);
        $this->assertEquals($folder->id, $image->parent_id);
    }

    /** @test */
    public function it_errors_when_imagedb_directory_does_not_exist() {
        // Ensure directory does not exist
        File::deleteDirectory($this->imageDbPath);

        $this->artisan('pagebuilder:make-imagedb', ['--testmode' => true])
            ->expectsOutput("The 'public/test-imagedb' directory does not exist.")
            ->assertExitCode(1);
    }

    /** @test */
    public function it_errors_when_imagedb_directory_is_empty() {
        // Delete existing dir
        File::deleteDirectory($this->imageDbPath);
        // Create an empty directory
        File::makeDirectory($this->imageDbPath, 0755, true);

        $this->artisan('pagebuilder:make-imagedb', ['--testmode' => true])
            ->expectsOutput("The 'public/test-imagedb' directory is empty. Nothing to index.")
            ->assertExitCode(1);
    }

    /** @test */
    public function it_errors_when_no_users_exist() {
        $this->artisan('pagebuilder:make-imagedb', ['--testmode' => true])
            ->expectsOutput("No users found in the database. Cannot continue!")
            ->expectsOutput('(Use artisan serve and npm run dev to spool up a local instance, and register a user from the web interface)')
            ->assertExitCode(1);
    }
}
