<?php

use App\Livewire\Studio\Information\Faq;
use App\Models\Faq as FaqModel;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

beforeEach(function () {
    $this->setupUser();

    $role = Role::firstOrCreate(['name' => 'Owner']);
    Permission::firstOrCreate(['name' => 'view faq']);
    Permission::firstOrCreate(['name' => 'create faq']);
    Permission::firstOrCreate(['name' => 'update faq']);
    Permission::firstOrCreate(['name' => 'delete faq']);
    $role->givePermissionTo(['view faq', 'create faq', 'update faq', 'delete faq']);
    $this->user->assignRole($role);
});

it('renders the faq page correctly', function () {
    $this->actingAs($this->user)
        ->get(route('studio.information.faq.index'))
        ->assertOk()
        ->assertSeeLivewire(Faq::class);
});

it('can open create modal', function () {
    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->dispatch('modal:open', method: 'create', modalTitle: 'Tambah FAQ')
        ->assertSet('method', 'create')
        ->assertSet('modalTitle', 'Tambah FAQ');
});

it('validates required fields on create', function () {
    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('create')
        ->assertHasErrors([
            'form.question' => 'required',
            'form.answer' => 'required',
        ]);
});

it('validates question max length', function () {
    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->set('form.question', str_repeat('a', 101))
        ->set('form.answer', 'Jawaban')
        ->call('create')
        ->assertHasErrors(['form.question' => 'max']);
});

it('can store a new faq', function () {
    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->set('form.question', 'Apa itu FAQ?')
        ->set('form.answer', 'FAQ adalah Frequently Asked Questions')
        ->call('create')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('faqs', [
        'question' => 'Apa itu FAQ?',
        'answer' => 'FAQ adalah Frequently Asked Questions',
        'sort_order' => 1,
    ]);
});

it('increments sort order when creating new faq', function () {
    // Create first FAQ
    FaqModel::factory()->create(['sort_order' => 1]);
    FaqModel::factory()->create(['sort_order' => 2]);

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->set('form.question', 'Pertanyaan Baru?')
        ->set('form.answer', 'Jawaban Baru')
        ->call('create')
        ->assertHasNoErrors();

    // New FAQ should have sort_order 1, and existing ones should be incremented
    $newFaq = FaqModel::where('question', 'Pertanyaan Baru?')->first();
    expect($newFaq->sort_order)->toBe(1);

    // Check that existing FAQs were incremented
    expect(FaqModel::where('sort_order', '>', 1)->count())->toBe(2);
});

it('can open edit modal and load data', function () {
    $faq = FaqModel::factory()->create([
        'question' => 'Pertanyaan Edit?',
        'answer' => 'Jawaban Edit',
    ]);

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('openModal', 'update', 'Ubah FAQ', $faq->hash_id)
        ->assertSet('method', 'update')
        ->assertSet('modalTitle', 'Ubah FAQ');
});

it('can update a faq', function () {
    $faq = FaqModel::factory()->create([
        'question' => 'Pertanyaan Lama?',
        'answer' => 'Jawaban Lama',
    ]);

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->set('form.faq', $faq)
        ->set('form.question', 'Pertanyaan Diperbarui?')
        ->set('form.answer', 'Jawaban Diperbarui')
        ->call('update')
        ->assertHasNoErrors()
        ->assertDispatched('refreshDatatable');

    $this->assertDatabaseHas('faqs', [
        'id' => $faq->id,
        'question' => 'Pertanyaan Diperbarui?',
        'answer' => 'Jawaban Diperbarui',
    ]);
});

it('can delete a faq', function () {
    $faq = FaqModel::factory()->create(['sort_order' => 2]);

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('delete', $faq)
        ->assertDispatched('refreshDatatable');

    $this->assertSoftDeleted('faqs', ['id' => $faq->id]);
});

it('can move faq sort order up', function () {
    $faq1 = FaqModel::factory()->create(['sort_order' => 1]);
    $faq2 = FaqModel::factory()->create(['sort_order' => 2]);
    $faq3 = FaqModel::factory()->create(['sort_order' => 3]);

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('sortOrder', $faq2, 'up')
        ->assertDispatched('refreshDatatable');

    $faq1->refresh();
    $faq2->refresh();

    expect($faq2->sort_order)->toBe(1);
    expect($faq1->sort_order)->toBe(2);
});

it('can move faq sort order down', function () {
    $faq1 = FaqModel::factory()->create(['sort_order' => 1]);
    $faq2 = FaqModel::factory()->create(['sort_order' => 2]);
    $faq3 = FaqModel::factory()->create(['sort_order' => 3]);

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('sortOrder', $faq2, 'down')
        ->assertDispatched('refreshDatatable');

    $faq2->refresh();
    $faq3->refresh();

    expect($faq2->sort_order)->toBe(3);
    expect($faq3->sort_order)->toBe(2);
});

it('cannot access faq page without permission', function () {
    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    $this->actingAs($this->user)
        ->get(route('studio.information.faq.index'))
        ->assertForbidden();
});

it('cannot create faq without permission', function () {
    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->set('form.question', 'Test?')
        ->set('form.answer', 'Test')
        ->call('create')
        ->assertForbidden();
});

it('cannot update faq without permission', function () {
    $faq = FaqModel::factory()->create();

    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('openModal', 'update', 'Ubah FAQ', $faq->hash_id)
        ->set('form.question', 'Updated?')
        ->call('update')
        ->assertForbidden();
});

it('cannot delete faq without permission', function () {
    $faq = FaqModel::factory()->create();

    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('delete', $faq)
        ->assertForbidden();
});

it('cannot sort faq without permission', function () {
    $faq = FaqModel::factory()->create(['sort_order' => 2]);

    $this->user->roles()->detach();
    $this->user->permissions()->detach();

    Livewire::actingAs($this->user)
        ->test(Faq::class)
        ->call('sortOrder', $faq, 'up')
        ->assertForbidden();
});
