<?php

namespace Tests\Services\OrderCommodityService;

use App\Commodity;
use App\OracleActAllocationDetails;
use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderCommodity;
use App\Owner;
use App\Services\CommodityService;
use App\Services\OrderCommodityService;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class SyncOrderCommodityTest extends TestCase
{
    /** @var OrderCommodityService $service */
    private $service;
    private $data = [];
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->service = app('OrderCommodityService');

        $owners = factory(Owner::class)->times(5)->create();
        $this->data['owners'] = $owners;
        $commodities = collect();
        foreach ($owners as $owner) {
            $commodities = $commodities->concat(factory(Commodity::class)->times(rand(5,20))->create(['owner_id'=>$owner->id]));
        }
        $this->data['commodities'] = $commodities;

        $orderHeaders = collect();$orders = collect();
        for ($i=0;$i<2000;$i++) {
            $owner = $owners->random();
            $commodity_list = $commodities->where('owner_id',$owner->id);
            $order = factory(Order::class)->create(['owner_id'=>$owner->id]);
            $orderHeader = factory(OracleDOCOrderHeader::class)->make(['orderno' => $order->code, 'customerid' => $owner->code]);
            $oracleActAllocationDetails = collect();
            for($count=1;$count<=4;$count++){
                $commodity = $commodity_list->random();
                $oracleActAllocationDetail  = factory(OracleActAllocationDetails::class)->make([
                    'orderno'=>$orderHeader['orderno'],
                    'customerid' => $orderHeader['customerid'],
                    'sku' => $commodity->sku
                    ]);
                $oracleActAllocationDetails->push($oracleActAllocationDetail);
            }
            $orderHeader->setRelation('actAllocationDetails',$oracleActAllocationDetails);
            $orderHeaders->push($orderHeader);
            $orders->push($order);
        }
        $this->data['orders'] = $orders;
        $this->data['orderHeaders'] = $orderHeaders;

        $this->mock(CommodityService::class,function($mock)use($commodities){
            $mock->shouldReceive('get_')->andReturn($commodities);
        });
    }

    /**
     * @test
     */
    public function syncOrderCommodity()
    {
        for ($i = 0 ;$i<4; $i++) {
            $this->service->syncOrderCommodity($this->data['orderHeaders']);
        }
        $counts = OrderCommodity::query()->whereIn('order_id',data_get($this->data['orders'],'*.id'))->get()->count();
        $actAllocationDetailsCounts  = 0;
         foreach ($this->data['orderHeaders'] as $orderHeader) {
            $actAllocationDetails = $orderHeader->actAllocationDetails;
            $actAllocationDetailsCounts  += $actAllocationDetails->count();
        }
        $this->assertEquals($counts,$actAllocationDetailsCounts);
     }

    public function tearDown(): void
    {
        cache()->flush();
        Order::query()->whereIn('id',data_get($this->data['orders'],'*.id'))->delete();
        Owner::query()->whereIn('id',data_get($this->data['owners'],'*.id'))->delete();
        Commodity::query()->whereIn('id',data_get($this->data['commodities'],'*.id'))->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
