<?php

namespace Tests\Services\OrderPackageService\SyncOrderPackageTest;

use App\Commodity;
use App\OracleActAllocationDetails;
use App\OracleBasSKU;
use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderPackage;
use App\Owner;
use App\Services\OracleDOCOrderHeaderService;
use App\Services\OrderPackageService;
use App\Services\OrderService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;
use Tests\TestCase;

class SyncOrderPackageTest extends TestCase
{

    /**
     * @var OrderPackageService $service
     * @var OracleDOCOrderHeaderService $orderHeaderService
     * @var OrderService $orderService
     */
    public $service;
    public $orderService;
    public $orderHeaderService;
    private $data = [];

    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('OrderPackageService');
        $this->orderService = app('OrderService');
        $this->orderHeaderService = app('OracleDocOrderHeaderService');

        $orderHeader = factory(OracleDOCOrderHeader::class)->make(['userdefine1' =>'TEST_OW' ]);
        $order = factory(Order::class)->create(['code'=>$orderHeader['orderno'],]);
        $basSku = factory(OracleBasSKU::class)->make(['customerid' => 'TEST_OW']);
        $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);

        $owner = factory(Owner::class)->create(['code'=>'TEST_OW']);
        $commodity = factory(Commodity::class)->create(['sku'=>$basSku['sku'],'owner_id' => $owner]);
        $this->data = [
            'owner' => $owner,
            'commodity' => $commodity,
            'order' => $order,
            'orderHeader' =>  $orderHeader,
            'sku' => $basSku,
            'actAllocationDetails' => $OracleActAllocationDetails,
        ];

    }

    /**
     * @test
     */
    public function syncOrderPackage()
    {
        $orderHeaders = collect([$this->data['orderHeader']]);
        $this->service->syncOrderPackage($orderHeaders);
        $orderPackages = OrderPackage::query()->where('order_id',$this->data['order']['id'])->get();
        $this->assertEquals($orderPackages->count(),count($this->data['orderHeader']->actAllocationDetails));
    }

    /**
     * @test
     */
    public function orderPackageUnnecessary()
    {
        OrderPackage::query()->create(['order_id'=>$this->data['order']['id'],'logistic_number'=>Str::uuid()]);
        OrderPackage::query()->create(['order_id'=>$this->data['order']['id'],'logistic_number'=>Str::uuid()]);
        $orderHeaders = collect([$this->data['orderHeader']]);
        $this->service->syncOrderPackage($orderHeaders);
        $orderPackages = OrderPackage::query()->where('order_id',$this->data['order']['id'])->get();
        $this->assertEquals($orderPackages->count(),count($this->data['orderHeader']->actAllocationDetails));
    }

    /**
     * @test
     */
    public function orderPackagesLack()
    {
        $orderHeaders = collect([$this->data['orderHeader']]);
        $this->service->syncOrderPackage($orderHeaders);
        $actAllocationDetails = $this->data['actAllocationDetails']->random(2);
        $this->data['orderHeader']->setRelation('actAllocationDetails', $actAllocationDetails);
        $orderHeaders = collect([$this->data['orderHeader']]);
        $this->service->syncOrderPackage($orderHeaders);
        $orderPackages = OrderPackage::query()->where('order_id',$this->data['order']['id'])->get();
        $this->assertEquals($orderPackages->count(),count($this->data['orderHeader']->actAllocationDetails));
        $this->assertOrderPackage($orderHeaders,$orderPackages);
    }

    public function assertOrderPackage($orderHeaders,$orderPackages)
    {
        foreach ($orderHeaders->first()->actAllocationDetails as $actAllocationDetail) {
            $orderPackage = $orderPackages->where('logistic_number',$actAllocationDetail->picktotraceid)->first();
            $this->assertNotEmpty($orderPackage);
        }
    }

    protected function tearDown(): void
    {
        $this->data['owner']->delete();
        $this->data['commodity']->delete();
        $this->data['order']->delete();

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


}
