<?php

namespace Tests\Services\CommodityService;

use App\Commodity;
use App\CommodityBarcode;
use App\Services\CacheService;
use App\Services\CommodityBarcodeService;
use App\Services\CommodityService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class SyncBarcodesTest extends TestCase
{

    /** @var CommodityService $service */
    public $service;
    /** @var CommodityBarcodeService $commodityBarcodeService */
    public $commodityBarcodeService;
    public $data=[];
    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('CommodityService');
        $this->commodityBarcodeService = app('CommodityBarcodeService');
        ($this->data['commodity']=(new Commodity(['owner_id'=>1,'sku'=>md5(microtime(true))])))->save();
        $this->data['barcodes']=['A1','B1','C2'];
        $this->data['commodityBarcodes']=new Collection();
        $inputs=[
            'commodity_id'=>$this->data['commodity']['id'],
            'barcodes'=>$this->data['barcodes']
        ];
        $this->commodityBarcodeService->insertMany_onCommodities([$inputs]);
    }

    public function testLessBarcodesCommodity(){
        $commodityBarcodes = $this->data['commodity']->load('barcodes')->barcodes;
        $this->assertEquals(count($this->data['barcodes']),$commodityBarcodes->count());
        $lastCode=array_pop($this->data['barcodes']);
        $this->data['barcodesStr']=implode(',',$this->data['barcodes']);
        $this->service->syncBarcodes($this->data['barcodesStr'], $this->data['commodity']->owner_id, $this->data['commodity']->sku);
        $commodityBarcodes = $this->data['commodity']->load('barcodes')->barcodes;
        $this->assertEquals(count($this->data['barcodes']),$commodityBarcodes->count());
        $nullCommodityBarcode=CommodityBarcode::query()
            ->where('commodity_id',$this->data['commodity']['id'])
            ->where('code',$lastCode)
            ->first();
        $this->assertEquals(null,$nullCommodityBarcode);

    }
    public function testMoreBarcodesCommodity(){
        $commodityBarcodes = $this->data['commodity']->load('barcodes')->barcodes;
        $this->assertEquals(count($this->data['barcodes']),$commodityBarcodes->count());
        $this->data['barcodes'][] = $newCode='FF';
        $this->data['barcodesStr']=implode(',',$this->data['barcodes']);
        $this->service->syncBarcodes($this->data['barcodesStr'], $this->data['commodity']->owner_id, $this->data['commodity']->sku);
        $commodityBarcodes = $this->data['commodity']->load('barcodes')->barcodes;
        $this->assertEquals(count($this->data['barcodes']),$commodityBarcodes->count());
        $additionalCommodityBarcode=CommodityBarcode::query()
            ->where('commodity_id',$this->data['commodity']['id'])
            ->where('code',$newCode)
            ->get();
        $this->assertEquals(1,$additionalCommodityBarcode->count());

    }
    public function testDiffMoreBarcodesCommodity(){
        $commodityBarcodes = $this->data['commodity']->load('barcodes')->barcodes;
        $this->assertEquals(count($this->data['barcodes']),$commodityBarcodes->count());
        $this->data['barcodes'][] = $newCode='FF';
        $firstCode=array_shift($this->data['barcodes']);
        $this->data['barcodesStr']=implode(',',$this->data['barcodes']);
        $this->service->syncBarcodes($this->data['barcodesStr'], $this->data['commodity']->owner_id, $this->data['commodity']->sku);
        $commodityBarcodes = $this->data['commodity']->load('barcodes')->barcodes;
        $this->assertEquals(count($this->data['barcodes']),$commodityBarcodes->count());
        $additionalCommodityBarcode=CommodityBarcode::query()
            ->where('commodity_id',$this->data['commodity']['id'])
            ->where('code',$newCode)
            ->get();
        $nullCommodityBarcode=CommodityBarcode::query()
            ->where('commodity_id',$this->data['commodity']['id'])
            ->where('code',$firstCode)
            ->first();
        $this->assertEquals(1,$additionalCommodityBarcode->count());
        $this->assertEquals(null,$nullCommodityBarcode);

    }


    public function tearDown(): void
    {
        $this->service->destroyWithOffspring($this->data['commodity']);
        CommodityBarcode::destroy($this->data['commodityBarcodes']->map(function ($commodityBarcode){
            return $commodityBarcode['id'];
        }));
        parent::tearDown();
    }
}
