<?php

namespace Tests\Services\StationTaskMaterialBoxService;
use App\Batch;
use App\MaterialBox;
use App\Order;
use App\OrderCommodity;
use App\Services\StationTaskMaterialBoxService;
use App\StationTask;
use Tests\TestCase;
use App\StationTaskMaterialBox;
use App\Traits\TestMockSubServices;

class CreateByBatchesTest extends TestCase
{
    use TestMockSubServices;
    /** @var StationTaskMaterialBoxService $service */
    public $service;
    private $data;
    private $batchAmount=2;
    private $orderAmount=4;
    private $materialBoxAmount=8;
    function setUp(): void
    {
        parent::setUp();
        $this->service = app('StationTaskMaterialBoxService');
        $this->data['batches']
            = factory(Batch::class, $this->batchAmount)
            ->create();
        $this->data['orders']
            = factory(Order::class)
            ->createMany($this->makeArray($this->orderAmount,[
                'batch_id' => function(){return $this->getTargetFieldCirculately($this->data['batches']);}
            ]));
        $this->data['materialBoxes']
            = factory(MaterialBox::class, $this->materialBoxAmount)
            ->create();
        $this->data['orderCommodities']
            = factory(OrderCommodity::class)
            ->createMany($this->makeArray($this->materialBoxAmount,[
                'order_id' => function(){return $this->getTargetFieldCirculately($this->data['orders']);},
                'location' => function(){return $this->getTargetFieldCirculately($this->data['materialBoxes'],'','code');},
            ]));
        $this->data['stationTask']
            = factory(StationTask::class, $this->batchAmount)
            ->create();
    }

    public function testReturned()
    {
        $this->data['stationTaskMaterialBoxes']=$this->service->createByBatches($this->data['batches'],$this->data['stationTask'] );
        $this->assertTrue($this->data['stationTaskMaterialBoxes']->isNotEmpty());
        $this->assertEquals(StationTaskMaterialBox::class, get_class($this->data['stationTaskMaterialBoxes']->first()));
        $this->assertEquals($this->materialBoxAmount, $this->data['stationTaskMaterialBoxes']->count());
        $this->assertEquals($this->materialBoxAmount, (function(){
            $taskBoxes=collect();
            foreach ($this->data['stationTask'] as $stationTask){
                $stationTask->loadMissing('stationTaskMaterialBoxes');
                $taskBoxes=$taskBoxes->merge($stationTask['stationTaskMaterialBoxes']);
            }
            return $taskBoxes->count();
        })());
    }

    function tearDown(): void
    {
        StationTaskMaterialBox::query()
            ->whereIn('id',data_get($this->data['batches'],'*.id')??[])
            ->delete();
        Order::query()
            ->whereIn('id',data_get($this->data['orders'],'*.id')??[])
            ->delete();
        MaterialBox::query()
            ->whereIn('id',data_get($this->data['materialBoxes'],'*.id')??[])
            ->delete();
        OrderCommodity::query()
            ->whereIn('id',data_get($this->data['orderCommodities'],'*.id')??[])
            ->delete();
        StationTask::query()
            ->whereIn('id',data_get($this->data['stationTask'],'*.id')??[])
            ->delete();
        parent::tearDown();
    }
}
