<?php


namespace Tests\Services\OrderPackageCommoditySerialNumberService;

use App\Commodity;
use App\OracleActAllocationDetails;
use App\OracleDOCOrderHeader;
use App\OracleDocOrderSerialNo;
use App\Order;
use App\OrderPackage;
use App\OrderPackageCommodities;
use App\Services\OrderPackageCommoditySerialNumberService;
use \Tests\TestCase;

class GetCreateModelBy extends TestCase
{
    /** @var OrderPackageCommoditySerialNumberService $service */
    protected $service ;
    public $data = [];

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app(OrderPackageCommoditySerialNumberService::class);
        $orderHeaders = factory(OracleDOCOrderHeader::class)->times(3)->make();
        $count= 0;

        foreach ($orderHeaders as $orderHeader) {
            $actAllocationDetails =  factory(OracleActAllocationDetails::class)->times(3)->make(['orderno'=>$orderHeader['orderno']]);
            foreach ($actAllocationDetails as $actAllocationDetail) {
                $oracleDocOrderSerialNos = factory(OracleDocOrderSerialNo::class)->times(3)
                    ->make(['allocationdetailsid'=>$actAllocationDetail['allocationdetailsid'],'sku'=>$actAllocationDetail['sku']]);
                $count+=3;
                $actAllocationDetail->setRelation('oracleDocOrderSerialNos',$oracleDocOrderSerialNos);
            }
            $orderHeader->setRelation('actAllocationDetails',$actAllocationDetails);
        }
        $orders = [];
        $orderPackages = [];
        $commodities = [];
        $orderPackageCommodities = [];
        foreach ($orderHeaders as $orderHeader) {
            $order = factory(Order::class)->create(['code'=>$orderHeader['orderno']]);
            $orders[] = $order;
            foreach ($orderHeader['actAllocationDetails'] as $actAllocationDetail) {
                $commodity = factory(Commodity::class)->create(['sku'=>$actAllocationDetail['sku']]);
                $orderPackage =  factory(OrderPackage::class)->create(['order_id' => $order['id'],'logistic_number'=> $actAllocationDetail['picktotraceid']]);
                $orderPackageCommodity = factory(OrderPackageCommodities::class)
                    ->create(['order_package_id'=>$orderPackage['id'],'commodity_id'=>$commodity['id'],'amount'=>$actAllocationDetail['qty_each']]);
                $orderPackageCommodities[] = $orderPackageCommodity;
                $orderPackages[] =$orderPackage;
                $commodities[] = $commodity;
            }
        }
        $this->data['count'] = $count;
        $this->data['orders'] = $orders;
        $this->data['orderPackages'] = $orderPackages;
        $this->data['orderPackages'] = $orderPackages;
        $this->data['commodities'] = $commodities;
        $this->data['orderPackageCommodities'] = $orderPackageCommodities;
        $this->data['orderHeaders'] = $orderHeaders;
    }

    public function testGetInnerParams()
    {
        $orderQuery = Order::query()->selectRaw('id')->whereIn('code', data_get($this->data['orderHeaders'], '*.orderno'));
        $orderPackages = OrderPackage::query()->with(['commodities' => function ($query) {
            $query->with('commodity', 'serialNumbers');
        }])->whereIn('Order_Packages.Order_id', $orderQuery)->get();
//        $this->tearDown();
        $innerParams = $this->service->getCreateModelBy($this->data['orderHeaders'],$orderPackages);
        foreach ($this->data['orderHeaders'] as $orderHeader){
            foreach ($orderHeader['actAllocationDetails'] as $actAllocationDetail) {
                $logisticNumber = $actAllocationDetail['picktotraceid'];
                $sku = $actAllocationDetail['sku'];
                foreach ($actAllocationDetail['oracleDocOrderSerialNos'] as $oracleDocOrderSerialNo){
                    $serialno = $oracleDocOrderSerialNo['serialno'];
                    $this->assertNotEmpty($innerParams[$logisticNumber][$sku][$serialno]);
                }
            }
        }
        $this->assertNotEmpty($innerParams);
    }

    protected function tearDown():void
    {
        Order::query()->whereIn('id',data_get($this->data['orders'],'*.id'))->delete();
        OrderPackage::query()->whereIn('logistic_number',data_get($this->data['orderPackages'],'*.logistic_number'))->delete();
        OrderPackageCommodities::query()->whereIn('id',data_get($this->data['orderPackageCommodities'],'*.id'))->delete();
        Commodity::query()->whereIn('id',data_get($this->data['commodities'],'*.id'))->delete();
        parent::tearDown();
    }
}
