<?php

declare(strict_types=1);

use App\Constants\FeatureConstants;
use App\Events\Wiki\Song\SongCreated;
use App\Events\Wiki\Song\SongDeleted;
use App\Events\Wiki\Song\SongRestored;
use App\Events\Wiki\Song\SongUpdated;
use App\Jobs\SendDiscordNotificationJob;
use App\Models\Wiki\Song;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Event;
use Laravel\Pennant\Feature;

test('song created sends discord notification', function () {
    Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS);
    Bus::fake(SendDiscordNotificationJob::class);
    Event::fakeExcept(SongCreated::class);

    Song::factory()->createOne();

    Bus::assertDispatched(SendDiscordNotificationJob::class);
});

test('song deleted sends discord notification', function () {
    $song = Song::factory()->createOne();

    Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS);
    Bus::fake(SendDiscordNotificationJob::class);
    Event::fakeExcept(SongDeleted::class);

    $song->delete();

    Bus::assertDispatched(SendDiscordNotificationJob::class);
});

test('song restored sends discord notification', function () {
    $song = Song::factory()->createOne();

    Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS);
    Bus::fake(SendDiscordNotificationJob::class);
    Event::fakeExcept(SongRestored::class);

    $song->restore();

    Bus::assertDispatched(SendDiscordNotificationJob::class);
});

test('song updated sends discord notification', function () {
    $song = Song::factory()->createOne();

    Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS);
    Bus::fake(SendDiscordNotificationJob::class);
    Event::fakeExcept(SongUpdated::class);

    $changes = Song::factory()->makeOne();

    $song->fill($changes->getAttributes());
    $song->save();

    Bus::assertDispatched(SendDiscordNotificationJob::class);
});
