<?php

namespace Tests\Feature\Console\CMMS;

use App\Models\CMMSMachine;
use App\Services\CMMS\GetMachinesInfoListService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Mockery\MockInterface;
use Tests\TestCase;

class GetMachinesInfoListTest extends TestCase
{
    use RefreshDatabase, WithFaker;
    /**
     * A basic feature test example.
     */
    protected function setUp(): void
    {
        parent::setUp();

        CMMSMachine::factory()->create();


    }

    public function test_get_machines_info_list_console_command_work_properly(): void
    {
        $this->withoutExceptionHandling();

        $data = [
            "Id" => $this->faker->numberBetween(1, 10),
            "PlakNumber" => $this->faker->numerify,
            "CarName" => $this->faker->name,
            "CarGroup" => $this->faker->name,
            "MachineCode" => $this->faker->numerify,
            "UnitName" => $this->faker->name,
            "Status" => $this->faker->numberBetween(1, 10),
            "UnitGroup" => $this->faker->name,
        ];

        $cmmsMachine = CMMSMachine::factory()->create([
            "Id" => $data['Id'],
        ]);

        $serviceResult = [
            'vw_MachBankFull' => [
                [
                    "Id" => $data['Id'],
                    "PlakNumber" => $data['PlakNumber'],
                    "CarName" => $data['CarName'],
                    "CarGroup" => $data['CarGroup'],
                    "MachineCode" => $data['MachineCode'],
                    "UnitName" => $data['UnitName'],
                    "Status" => $data['Status'],
                    "UnitGroup" => $data['UnitGroup'],
                ],
                [
                    "Id" => 3232,
                    "PlakNumber" => $data['PlakNumber'],
                    "CarName" => $data['CarName'],
                    "CarGroup" => $data['CarGroup'],
                    "MachineCode" => $data['MachineCode'],
                    "UnitName" => $data['UnitName'],
                    "Status" => $data['Status'],
                    "UnitGroup" => $data['UnitGroup'],
                ],
            ]
        ];

        $this->mock(GetMachinesInfoListService::class, function (MockInterface $mock) use($serviceResult) {
            $mock->shouldReceive('run')
                ->andReturn($serviceResult);
        });

        $this->artisan('cmms_machines_info:get_list');

        $this->assertDatabaseHas('cmms_machines', [
            'car_name' => $data['CarName'],
            'car_group' => $data['CarGroup'],
            'machine_code' => $data['MachineCode'],
            'unit_name' => $data['UnitName'],
            'car_status' => $data['Status'],
            'car_id' => $data['Id'],
            'unit_group' => $data['UnitGroup'],
            'plak_number' => $data['PlakNumber'],
        ]);
        $this->assertDatabaseHas('cmms_machines', [
            'car_name' => $data['CarName'],
            'car_group' => $data['CarGroup'],
            'machine_code' => $data['MachineCode'],
            'unit_name' => $data['UnitName'],
            'car_status' => $data['Status'],
            'car_id' => 3232,
            'unit_group' => $data['UnitGroup'],
            'plak_number' => $data['PlakNumber'],
        ]);
    }
}
