<?php

namespace Tests\Feature;

use App\Enums\OrderAttributes;
use App\Enums\OrderStatus;
use App\Enums\OrderType;
use App\Filament\Resources\OrderResource\Pages\CreateOrder;
use App\Models\Contact;
use App\Models\Customer;
use App\Models\Order;
use App\Models\ServiceType;
use App\Models\User;

use function Pest\Livewire\livewire;

it('can create an order and save it to the database', function () {
    $type       = fake()->randomElement(OrderType::cases());
    $status     = fake()->randomElement(OrderStatus::cases());
    $customer   = Customer::factory()->create();
    $contact    = Contact::factory()->for($customer)->create();
    $attributes = array_map(fn ($case) => $case->value, OrderAttributes::cases());

    $serviceTypes = ServiceType::factory()->count(2)->create();

    $user = User::factory()->create();
    $this->actingAs($user);

    $formData = [
        'order_type'       => $type->value,
        'customer_id'      => $customer->id,
        'contact_id'       => $contact->id,
        'customer_po'      => 'Customer PO name here',
        'order_date'       => today(),
        'due_date'         => today()->addDays(10),
        'notes'            => 'Notes go here! Here\'s the notes!',
        'pre_production'   => '1',
        'printed'          => '1',
        'status'           => $status->value,
        'order_attributes' => $attributes,
        'order_products'   => [
            [
                'sku'          => 'sku 1',
                'product_name' => 'test',
                'color'        => 'black',
                'xs'           => '1',
                's'            => '2',
                'm'            => '3',
                'l'            => '4',
                'xl'           => '5',
                '2xl'          => '6',
                '3xl'          => '7',
                'osfa'         => '8',
            ],
            [
                'sku'          => 'sku 2',
                'product_name' => 'alsotest',
                'color'        => 'white',
                'xs'           => '9',
                's'            => '10',
                'm'            => '11',
                'l'            => '12',
                'xl'           => '13',
                '2xl'          => '14',
                '3xl'          => '15',
                'osfa'         => '16',
            ],
        ],
        'services' => [
            [
                'serviceType'            => $serviceTypes[0]->id,
                'placement'              => 'c/f',
                'serviceFileName'        => 'logo name 1',
                'serviceFileSetupNumber' => '1',
                'serviceFileWidth'       => '1',
                'serviceFileHeight'      => '2',
                'amount'                 => '3',
                'amount_price'           => '4',
                'serviceFileCode'        => 'A1234',
                'notes'                  => 'Here\'s some notes, all handwritten by me.',
            ],
            [
                'serviceType'            => $serviceTypes[1]->id,
                'placement'              => 'f/b',
                'serviceFileName'        => 'logo name 2',
                'serviceFileSetupNumber' => '5',
                'serviceFileWidth'       => '6',
                'serviceFileHeight'      => '7',
                'amount'                 => '8',
                'amount_price'           => '9',
                'serviceFileCode'        => 'B5678',
                'notes'                  => 'Here\'s even more notes, still handwritten by me.',
            ],
        ],
    ];

    livewire(CreateOrder::class)
        ->set('data.order_products', [])
        ->set('data.services', [])
        ->fillForm($formData)
        ->call('create')
        ->assertHasNoErrors();

    $order = Order::first();

    $this->assertNotNull($order);

    $this->assertSame($order->customer_id, $formData['customer_id']);

});
