<?php

use App\Filament\Admin\Resources\QuoteResource\Pages\CreateQuote;
use App\Filament\Admin\Resources\QuoteResource\Pages\ListQuotes;
use App\Models\Customer;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;

use function Pest\Livewire\livewire;

uses(RefreshDatabase::class);

it('can render the list page', function () {
    $this->actingAs(User::factory(['is_admin' => true])->create());
    livewire(ListQuotes::class)->assertSuccessful();
});

it('cannot render the list page if user isn\'t an admin', function () {
    $this->actingAs(User::factory()->create());
    livewire(ListQuotes::class)->assertForbidden();
});

it('can create a quote using the form', function () {
    $this->actingAs(User::factory(['is_admin' => true])->create());

    $customer = Customer::factory()->create();

    $formData = [
        'customer_id' => $customer->id,
        'date'        => today(),
        'notes'       => 'Some note',
    ];

    $this->livewire(CreateQuote::class)
        ->fillForm($formData)
        ->call('create')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('quotes', $formData);
});

it('can add an embroidery entry to the quote using the form', function () {
    $this->actingAs(User::factory(['is_admin' => true])->create());

    $customer = Customer::factory()->create();

    $formData = [
        'customer_id' => $customer->id,
        'date'        => today(),
        'notes'       => 'Some note',

        'embroideryEntries' => [
            [
                'logo'            => 'logo name',
                'placement'       => 'Right sleeve',
                'quantity'        => 5,
                'width'           => 1.5,
                'height'          => 2.5,
                'stitch_count'    => '3k - 4k',
                'digitizing_cost' => 10.5,
                'run_charge'      => 12,
            ],
        ],
    ];

    $this->livewire(CreateQuote::class)
        ->fillForm($formData)
        ->call('create')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('embroidery_entries', $formData['embroideryEntries'][0]);
});

it('can add a screen printing entry to the quote using the form', function () {
    $this->actingAs(User::factory(['is_admin' => true])->create());

    $customer = Customer::factory()->create();

    $formData = [
        'customer_id' => $customer->id,
        'date'        => today(),
        'notes'       => 'Some note',

        'screenPrintEntries' => [
            [
                'logo'         => 'logo name',
                'quantity'     => 5,
                'width'        => 1.5,
                'height'       => 2.5,
                'color_amount' => 2,
                'setup_amount' => 2,
                'run_charge'   => 10,
                'color_match'  => 5.10,
                'color_change' => 5.15,
                'flash'        => 5.20,
                'fleece'       => 5.30,
                'poly_ink'     => 5.40,
                'artwork_fee'  => 5.50,
            ],
        ],
    ];

    $this->livewire(CreateQuote::class)
        ->fillForm($formData)
        ->call('create')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('screen_print_entries', $formData['screenPrintEntries'][0]);
});

it('can add a heat transfer entry to the quote using the form', function () {
    $this->actingAs(User::factory(['is_admin' => true])->create());

    $customer = Customer::factory()->create();

    $formData = [
        'customer_id' => $customer->id,
        'date'        => today(),
        'notes'       => 'Some note',

        'heatTransferEntries' => [
            [
                'logo'     => 'logo name',
                'quantity' => 5,
                'width'    => 1.5,
                'height'   => 2.5,
                'price'    => 2,
            ],
        ],
    ];

    $this->livewire(CreateQuote::class)
        ->fillForm($formData)
        ->call('create')
        ->assertHasNoErrors();

    $this->assertDatabaseHas('heat_transfer_entries', $formData['heatTransferEntries'][0]);
});