<?php

namespace Tests\Services\OrderService;

use App\Batch;
use App\Logistic;
use App\OracleBasCode;
use App\OracleDOCOrderHeader;
use App\Order;
use App\Owner;
use App\Services\common\DataHandlerService;
use App\Services\OrderService;
use App\Shop;
use App\Warehouse;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GetCreateOrderModelsTest extends TestCase
{

    /**
     * @var OrderService $service
     * @var DataHandlerService $dataHandlerService
     */
    private $service;
    private $dataHandlerService;
    private $data = [];

    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderService');
        $this->dataHandlerService = app('DataHandlerService');

        $owners = factory(Owner::class)->times(20)->create();
        $this->data['owners'] = $owners;
        $logistics = factory(Logistic::class)->times(30)->create();
        $wareHouses = factory(Warehouse::class)->times(2)->create();
        $batches = factory(Batch::class)->times(10)->create();
        $this->data['logistics'] = $logistics;
        $this->data['wareHouses'] = $wareHouses;
        $shops = collect();
        foreach ($owners as $owner) {
            $shops = $shops->concat(factory(Shop::class)->times(rand(1,11))->create(['owner_id'=>$owner->id]));
        }
        $this->data['shops'] = $shops;
        $orderHeaders = collect();

        $basBack = factory(OracleBasCode::class)->times(5)->make();

        for ($i=0;$i<= 30;$i++) {
            $owner = $owners->random();
            $shop = $shops->where('owner_id',$owner->id)->random();
            $logistic = $logistics->random();
            $wareHouse = $wareHouses->random();
            $batch = $batches->random();
            $orderHeader = factory(OracleDOCOrderHeader::class)->make();
            $orderHeader->customerid = $owner->code;
            $orderHeader->waveno = $batch->code;
            $orderHeader->warehouseid = $wareHouse->code;
            $orderHeader->userdefine1 = $logistic->code;
            $orderHeader->issuepartyname =  $shop->code;
            $orderType = $basBack->random();
            $oracleBASCode = $basBack->random();
            $orderHeader->setRelation('orderType',$orderType);
            $orderHeader->setRelation('oracleBASCode',$oracleBASCode);
            $orderHeaders->push($orderHeader);
        }
        $this->data = [
            'owners'  => $owners,
            'logistics' => $logistics,
            'wareHouses' => $wareHouses,
            'shops' => $shops,
            'orderHeaders' => $orderHeaders,
            'batches' => $batches,
        ];
    }

    /**
     * @test
     */
    public function getCreateOrderModels()
    {
        // is progressing
        // Mission in progress
        $orderHeader_map =$this->data['orderHeaders'];
        $warehouse_map = $this->dataHandlerService->dataHeader(['code'],$this->data['wareHouses']);
        $owner_map = $this->dataHandlerService->dataHeader(['code'],$this->data['owners']);
        $logistic_map = $this->dataHandlerService->dataHeader(['code'],$this->data['logistics']);
        $shop_map = $this->dataHandlerService->dataHeader(['name','owner_id'],$this->data['shops']);
        $batch_map =  $this->dataHandlerService->dataHeader(['code'],$this->data['batches']);
        $date = (string)Carbon::now();
        foreach ($orderHeader_map as $orderHeader) {
            $param = $this->service->getCreateOrderModels($orderHeader,$warehouse_map,$owner_map,$logistic_map,$shop_map,$batch_map,$date);
            $warehouse = $this->dataHandlerService->getKeyValue(['code'=>$orderHeader->warehouseid],$warehouse_map);
            $owner = $this->dataHandlerService->getKeyValue(['code'=>$orderHeader->customerid],$owner_map);
            $logistic = $this->dataHandlerService->getKeyValue(['code'=>$orderHeader->userdefine1],$logistic_map);
            $batch = $this->dataHandlerService->getKeyValue(['code'=>$orderHeader->waveno],$batch_map);
            $shop['id'] = null;
            if($orderHeader->issuepartyname != null && $orderHeader->issuepartyname != '' ){
                $shop = $this->dataHandlerService->getKeyValue(['name'=>$orderHeader->issuepartyname ?? '','owner_id'=>$owner->id??''],$shop_map);
            }

            $this->assertEquals($param['code'],$orderHeader['orderno']);
            $this->assertEquals($param['batch_id'],$batch['id'] ?? null);
            $this->assertEquals($param['warehouse_id'],$warehouse['id'] ?? null);
            $this->assertEquals($param['owner_id'],$owner['id'] ?? null);
            $this->assertEquals($param['shop_id'],$shop['id'] ?? null);
            $this->assertEquals($param['logistic_id'],$logistic['id'] ?? null);
            $this->assertEquals($param['consignee_name'],  $orderHeader['c_contact']);
            $this->assertEquals($param['consignee_phone'],$orderHeader['c_tel2'] ?? $orderHeader['c_tel1']);
            $this->assertEquals($param['city'],$orderHeader['c_city']);
            $this->assertEquals($param['district'],$orderHeader['c_district']);
            $this->assertEquals($param['address'],$orderHeader['c_address1']);
            $this->assertEquals($param['client_code'],$orderHeader['soreference1']);
            $this->assertEquals($param['wms_edittime'], $orderHeader['edittime']);
            $this->assertEquals($param['wms_status'],$orderHeader->oracleBASCode->codename_c??'');
            $this->assertEquals($param['order_type'],$orderHeader->orderType->codename_c??'');
        }
    }

    public function tearDown(): void
    {
        Order::query()->whereIn('code',data_get($this->data['orderHeaders'],'*.orderno'));

        Warehouse::query()->whereIn('id',data_get($this->data['wareHouses'],'*.id'));
        Owner::query()->whereIn('id',data_get($this->data['owners'],'*.id'));
        Logistic::query()->whereIn('id',data_get($this->data['logistics'],'*.id'));
        Shop::query()->whereIn('id',data_get($this->data['shops'],'*.id'));
        Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id'));

        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
