<?php

use App\Enums\UserType;
use App\Models\BusinessSetting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

uses(RefreshDatabase::class);

it('updates branding assets using uploaded files', function () {
    Storage::fake('public');

    $admin = User::factory()->create([
        'type' => UserType::SUPER_ADMIN,
    ]);

    $response = $this->actingAs($admin)->post(route('admin.business.update'), [
        'business_name' => 'Acme HRM',
        'tagline' => 'People first',
        'logo_url' => UploadedFile::fake()->image('logo.png', 200, 200),
        'favicon_url' => UploadedFile::fake()->image('favicon.png', 64, 64),
        'cover_url' => UploadedFile::fake()->image('cover.jpg', 1400, 400),
    ]);

    $response->assertRedirect();

    expect(BusinessSetting::where('key', 'business_name')->value('value'))->toBe('Acme HRM');

    $logoPath = BusinessSetting::where('key', 'logo_url')->value('value');
    $faviconPath = BusinessSetting::where('key', 'favicon_url')->value('value');
    $coverPath = BusinessSetting::where('key', 'cover_url')->value('value');

    expect($logoPath)->toStartWith('branding/');
    expect($faviconPath)->toStartWith('branding/');
    expect($coverPath)->toStartWith('branding/');

    Storage::disk('public')->assertExists($logoPath);
    Storage::disk('public')->assertExists($faviconPath);
    Storage::disk('public')->assertExists($coverPath);
});
