<?php

declare(strict_types=1);

use App\Models\Artwork;
use App\Models\Tag;
use App\Models\User;
use App\Models\UserNegativeSignal;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('stores hidden artwork signals', function () {
    $user = User::factory()->create();
    $artwork = Artwork::factory()->create();

    $response = $this->actingAs($user)->postJson('/api/discovery/feedback/hide-artwork', [
        'artwork_id' => $artwork->id,
        'source' => 'feed-test',
    ]);

    $response->assertStatus(202)->assertJsonPath('stored', true);

    expect(UserNegativeSignal::query()
        ->where('user_id', $user->id)
        ->where('signal_type', 'hide_artwork')
        ->where('artwork_id', $artwork->id)
        ->exists())->toBeTrue();
});

it('revokes hidden artwork signals', function () {
    $user = User::factory()->create();
    $artwork = Artwork::factory()->create();

    UserNegativeSignal::query()->create([
        'user_id' => $user->id,
        'signal_type' => 'hide_artwork',
        'artwork_id' => $artwork->id,
        'source' => 'feed-test',
    ]);

    $response = $this->actingAs($user)->deleteJson('/api/discovery/feedback/hide-artwork', [
        'artwork_id' => $artwork->id,
    ]);

    $response->assertOk()->assertJsonPath('revoked', true);

    expect(UserNegativeSignal::query()
        ->where('user_id', $user->id)
        ->where('signal_type', 'hide_artwork')
        ->where('artwork_id', $artwork->id)
        ->exists())->toBeFalse();
});

it('stores disliked tag signals by slug', function () {
    $user = User::factory()->create();
    $tag = Tag::query()->create(['name' => 'Sci-Fi', 'slug' => 'sci-fi']);

    $response = $this->actingAs($user)->postJson('/api/discovery/feedback/dislike-tag', [
        'tag_slug' => 'sci-fi',
        'source' => 'feed-test',
    ]);

    $response->assertStatus(202)->assertJsonPath('stored', true);

    expect(UserNegativeSignal::query()
        ->where('user_id', $user->id)
        ->where('signal_type', 'dislike_tag')
        ->where('tag_id', $tag->id)
        ->exists())->toBeTrue();
});

it('revokes disliked tag signals by slug', function () {
    $user = User::factory()->create();
    $tag = Tag::query()->create(['name' => 'Sci-Fi', 'slug' => 'sci-fi']);

    UserNegativeSignal::query()->create([
        'user_id' => $user->id,
        'signal_type' => 'dislike_tag',
        'tag_id' => $tag->id,
        'source' => 'feed-test',
    ]);

    $response = $this->actingAs($user)->deleteJson('/api/discovery/feedback/dislike-tag', [
        'tag_slug' => 'sci-fi',
    ]);

    $response->assertOk()->assertJsonPath('revoked', true);

    expect(UserNegativeSignal::query()
        ->where('user_id', $user->id)
        ->where('signal_type', 'dislike_tag')
        ->where('tag_id', $tag->id)
        ->exists())->toBeFalse();
});
