<?php

use App\Livewire\Studio\Master\Formula as FormulaComponent;
use App\Models\Bottle;
use App\Models\Formula;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;

uses(RefreshDatabase::class);

beforeEach(function () {
    $this->user = User::factory()->create();
    Bottle::factory()->count(3)->create();
    Formula::factory()->count(3)->create();
});

function mountFormulaComponent(User $user)
{
    return Livewire::actingAs($user)->test(FormulaComponent::class);
}

it('renders formula page successfully', function () {
    mountFormulaComponent($this->user)
        ->assertViewIs('livewire.studio.master.formulas')
        ->assertViewHas('pageTitle', 'Rumus');
});

it('mounts formulas and sizes correctly', function () {
    $component = mountFormulaComponent($this->user);

    expect($component->formulas)->toHaveCount(3);
    expect($component->sizes)->toHaveCount(Bottle::count());
});

it('prevents creating formula when unauthorized', function () {
    Formula::factory()->raw();
    mountFormulaComponent($this->user)
        ->call('create')
        ->assertForbidden();
});

it('creates formula successfully when authorized', function () {
    $permission = Permission::create(['name' => 'create formula']);
    $this->user->givePermissionTo($permission);

    $data = Formula::factory()->raw();

    $component = mountFormulaComponent($this->user);

    $component
        ->set('form.quality', $data['quality'])
        ->set('form.size', $data['size'])
        ->set('form.volume', $data['volume'])
        ->call('create')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('formulas', [
        'quality' => $data['quality'],
        'size' => $data['size'],
        'volume' => $data['volume'],
    ]);

    expect($component->formulas[$data['size']][0]['quality'])->toBe($data['quality']);
});

it('updates formula successfully when authorized', function () {
    $permission = Permission::create(['name' => 'update formula']);
    $this->user->givePermissionTo($permission);

    $formulaToEdit = Formula::factory()->create();

    $data = Formula::factory()->raw();

    $component = mountFormulaComponent($this->user);

    $component
        ->set('form.quality', $data['quality'])
        ->set('form.size', $data['size'])
        ->set('form.volume', $data['volume'])
        ->call('update')
        ->assertHasNoErrors();

    $formulaToEdit->refresh();

    $this->assertEquals($data['quality'], $formulaToEdit->quality);

    expect($component->formulas[$formulaToEdit->size][0]['quality'])->toBe('Updated Quality');
});

it('deletes formula successfully', function () {
    $formula = Formula::factory()->create();
    $component = mountFormulaComponent($this->user);

    $component->call('delete', $formula)
        ->assertHasNoErrors();

    $this->assertSoftDeleted('formulas', ['id' => $formula->id]);

    expect($component->formulas[$formula->size] ?? [])->toBeEmpty();
});
