<?php

namespace Tests\Services\OrderPackageSerivce\GetInnerParamsTest;

use App\Commodity;
use App\OracleActAllocationDetails;
use App\OracleBasSKU;
use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderPackage;
use App\Owner;
use App\Services\common\DataHandlerService;
use App\Services\OrderPackageService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class GetInnerParamsTest extends TestCase
{
    /**
     * @var OrderPackageService $service
     */
    public $service;
    private $data = [];
    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderPackageService');
        $orderHeader = factory(OracleDOCOrderHeader::class)->make(['userdefine1' =>'TEST_OW' ]);
        $this->data['orderHeader'] = $orderHeader;
        $order = factory(Order::class)->create([
            'code'=>$orderHeader['orderno'],
            ]);

        $basSku = factory(OracleBasSKU::class)->make(['customerid' => 'TEST_OW']);
        $this->data['sku'] = $basSku;
        $OracleActAllocationDetail1 = factory(OracleActAllocationDetails::class)->make(['orderno'=>$orderHeader->orderno,'customerid'=>'TEST_OW','sku'=>$basSku->sku]);
        $OracleActAllocationDetail2 = factory(OracleActAllocationDetails::class)->make(['orderno'=>$orderHeader->orderno,'customerid'=>'TEST_OW','sku'=>$basSku->sku]);
        $OracleActAllocationDetail3 = factory(OracleActAllocationDetails::class)->make(['orderno'=>$orderHeader->orderno,'customerid'=>'TEST_OW','sku'=>$basSku->sku]);
        $OracleActAllocationDetails = collect([$OracleActAllocationDetail1,$OracleActAllocationDetail2,$OracleActAllocationDetail3]);
        $orderHeader->setRelation('actAllocationDetails',$OracleActAllocationDetails);
        $this->data['actAllocationDetails'] = $OracleActAllocationDetails;
        $owner = factory(Owner::class)->create(['code'=>'TEST_OW']);
        $commodity = factory(Commodity::class)->create(['sku'=>$basSku['sku'],'owner_id' => $owner]);
        $this->data['owner'] = $owner;
        $this->data['commodity'] = $commodity;
        $this->data['order'] = $order;
    }

    /**
     * @test
     */
    public function getInnerParamsTest()
    {
        /** @var DataHandlerService $dataHandlerService */
        $dataHandlerService = app(DataHandlerService::class);
        $order  = $this->data['order'];
        $orderPackage = OrderPackage::query()->whereHas('order',function($query)use($order){
            $query->where('code',$order['code']);
        })->get();
        $map = $dataHandlerService->dataHeader(['logistic_number'],$orderPackage);
        $params = $this->service->getInnerParams($this->data['orderHeader'],$this->data['order'],$map);
        $orderPackages = collect($params);
        dump($params,$this->data['actAllocationDetails']->toArray());
        foreach ($this->data['actAllocationDetails'] as $actAllocationDetail) {
            $orderPackage = $orderPackages->where('logistic_number',$actAllocationDetail['picktotraceid'])->first();
            $this->assertNotNull($orderPackage);
        }
    }

    public function tearDown(): void
    {
        $this->data['owner']->delete();
        $this->data['commodity']->delete();
        $this->data['order']->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
