<?php

use App\Models\ServiceSong;
use App\Models\Song;
use App\Models\SongArrangement;
use App\Models\User;

/*
|--------------------------------------------------------------------------
| ServiceSongController — confirm/unconfirm + Reset-Logik
|--------------------------------------------------------------------------
*/

test('POST /api/service-songs/{id}/confirm setzt confirmed_at', function () {
    $user = User::factory()->create();
    $song = Song::factory()->create();
    $serviceSong = ServiceSong::factory()->create([
        'song_id' => $song->id,
        'confirmed_at' => null,
    ]);

    $response = $this->actingAs($user)
        ->postJson("/api/service-songs/{$serviceSong->id}/confirm");

    $response->assertOk()
        ->assertJson(['success' => true]);

    $serviceSong->refresh();
    expect($serviceSong->confirmed_at)->not->toBeNull();
});

test('POST /api/service-songs/{id}/unconfirm setzt confirmed_at auf null', function () {
    $user = User::factory()->create();
    $song = Song::factory()->create();
    $serviceSong = ServiceSong::factory()->create([
        'song_id' => $song->id,
        'confirmed_at' => now(),
    ]);

    $response = $this->actingAs($user)
        ->postJson("/api/service-songs/{$serviceSong->id}/unconfirm");

    $response->assertOk()
        ->assertJson(['success' => true]);

    $serviceSong->refresh();
    expect($serviceSong->confirmed_at)->toBeNull();
});

test('confirm/unconfirm erfordern Authentifizierung', function () {
    $serviceSong = ServiceSong::factory()->create();

    $this->postJson("/api/service-songs/{$serviceSong->id}/confirm")
        ->assertUnauthorized();

    $this->postJson("/api/service-songs/{$serviceSong->id}/unconfirm")
        ->assertUnauthorized();
});

test('assign setzt confirmed_at zurueck', function () {
    $user = User::factory()->create();
    $oldSong = Song::factory()->create();
    $newSong = Song::factory()->create();
    $serviceSong = ServiceSong::factory()->create([
        'song_id' => $oldSong->id,
        'confirmed_at' => now(),
    ]);

    $this->actingAs($user)
        ->postJson("/api/service-songs/{$serviceSong->id}/assign", [
            'song_id' => $newSong->id,
        ])
        ->assertOk();

    $serviceSong->refresh();
    expect($serviceSong->song_id)->toBe($newSong->id);
    expect($serviceSong->confirmed_at)->toBeNull();
});

test('unassign setzt confirmed_at zurueck', function () {
    $user = User::factory()->create();
    $song = Song::factory()->create();
    $serviceSong = ServiceSong::factory()->create([
        'song_id' => $song->id,
        'confirmed_at' => now(),
    ]);

    $this->actingAs($user)
        ->postJson("/api/service-songs/{$serviceSong->id}/unassign")
        ->assertOk();

    $serviceSong->refresh();
    expect($serviceSong->song_id)->toBeNull();
    expect($serviceSong->confirmed_at)->toBeNull();
});

test('Aenderung des Arrangements setzt confirmed_at zurueck', function () {
    $user = User::factory()->create();
    $song = Song::factory()->create();
    $arrA = SongArrangement::factory()->create(['song_id' => $song->id, 'name' => 'A']);
    $arrB = SongArrangement::factory()->create(['song_id' => $song->id, 'name' => 'B']);
    $serviceSong = ServiceSong::factory()->create([
        'song_id' => $song->id,
        'song_arrangement_id' => $arrA->id,
        'confirmed_at' => now(),
    ]);

    $this->actingAs($user)
        ->patchJson("/api/service-songs/{$serviceSong->id}", [
            'song_arrangement_id' => $arrB->id,
        ])
        ->assertOk();

    $serviceSong->refresh();
    expect($serviceSong->song_arrangement_id)->toBe($arrB->id);
    expect($serviceSong->confirmed_at)->toBeNull();
});

test('reine use_translation Aenderung setzt confirmed_at NICHT zurueck', function () {
    $user = User::factory()->create();
    $song = Song::factory()->create(['has_translation' => true]);
    $arr = SongArrangement::factory()->create(['song_id' => $song->id, 'name' => 'A']);
    $confirmedAt = now()->subHour();
    $serviceSong = ServiceSong::factory()->create([
        'song_id' => $song->id,
        'song_arrangement_id' => $arr->id,
        'use_translation' => false,
        'confirmed_at' => $confirmedAt,
    ]);

    $this->actingAs($user)
        ->patchJson("/api/service-songs/{$serviceSong->id}", [
            'use_translation' => true,
        ])
        ->assertOk();

    $serviceSong->refresh();
    expect($serviceSong->use_translation)->toBeTrue();
    expect($serviceSong->confirmed_at)->not->toBeNull();
});
