<?php


namespace Tests\Services\OwnerPriceDirectLogisticService;

use App\CarType;
use App\Owner;
use App\OwnerPriceDirectLogistic;
use App\OwnerPriceDirectLogisticCar;
use App\Services\OwnerPriceDirectLogisticService;
use Ramsey\Uuid\Uuid;
use Tests\TestCase;

class OwnerPriceDirectLogisticServiceTest extends  TestCase
{
    /** @var OwnerPriceDirectLogisticService */
    public $service;
    public $data;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = app(OwnerPriceDirectLogisticService::class);

        $this->data["models"] = factory(OwnerPriceDirectLogistic::class,3)->create()->toArray();
    }

    /**
     * @group customer
     */
    public function testPaginate()
    {
        $models = $this->service->paginate();
        $this->assertGreaterThanOrEqual(3,count($models));
        $this->assertLessThanOrEqual(50,count($models));
        $models = $this->service->paginate($this->data["models"][0]["id"]);
        $this->assertCount(1,$models);
    }
    /**
     * @group customer
     */
    public function testCreate()
    {
        $model = $this->service->create([
            "name" => Uuid::uuid1(),
            "base_km" => 3
        ]);
        $this->assertNotNull($model);
        $this->data["models"][] = $model->toArray();
    }
    /**
     * @group customer
     */
    public function testDestroy()
    {
        $result = $this->service->destroy(array_column($this->data["models"],"id"));
        $this->assertEquals(3,$result);
        $this->data["models"] = [];
    }
    /**
     * @group customer
     */
    public function testFind()
    {
        $model = $this->service->find($this->data["models"][0]["id"]);
        $this->assertNotNull($model);
    }
    /**
     * @group customer
     */
    public function testUpdate()
    {
        $id = $this->data["models"][0]["id"];
        $result = $this->service->update(["id"=>$id],[
            "base_km" => 3
        ]);
        $this->assertEquals(1,$result);
        $model = OwnerPriceDirectLogistic::query()->find($id);
        $this->assertEquals(3,$model->base_km);
    }
    /**
     * @group customer
     */
    public function testUpdateDetail()
    {
        $car = factory(\App\CarType::class)->create();
        $model = factory(OwnerPriceDirectLogisticCar::class)->create([
            "owner_price_direct_logistic_id" => $this->data["models"][0]["id"],   //直发车计费ID
            "car_type_id" => $car->id,                      //车型ID
        ]);
        $result = $this->service->updateDetail(["id"=>$model->id],["base_fee"=>0.01]);
        $this->assertEquals(1,$result);
        OwnerPriceDirectLogisticCar::destroy($model->id);
        CarType::destroy($car->id);
    }
    /**
     * @group customer
     */
    public function testIsExistDetail()
    {
        $car = factory(\App\CarType::class)->create();
        $model = factory(OwnerPriceDirectLogisticCar::class)->create([
            "owner_price_direct_logistic_id" => $this->data["models"][0]["id"],   //直发车计费ID
            "car_type_id" => $car->id,                      //车型ID
        ]);
        $result = $this->service->isExistDetail(["owner_price_direct_logistic_id"=>$this->data["models"][0]["id"]]);
        $this->assertGreaterThan(0,$result);
        OwnerPriceDirectLogisticCar::destroy($model->id);
        CarType::destroy($car->id);
    }
    /**
     * @group customer
     */
    public function testCreatedDetail()
    {
        $model = $this->service->createDetail([
            "owner_price_direct_logistic_id" => $this->data["models"][0]["id"],   //直发车计费ID
            "car_type_id" => 1,
            "base_fee" => 1,
            "additional_fee" => 1,
        ]);
        $this->assertNotNull($model);
        OwnerPriceDirectLogisticCar::destroy($model->id);
    }
    /**
     * @group customer
     */
    public function testDestroyDetail()
    {
        $car = factory(\App\CarType::class)->create();
        $model = factory(OwnerPriceDirectLogisticCar::class)->create([
            "owner_price_direct_logistic_id" => $this->data["models"][0]["id"],   //直发车计费ID
            "car_type_id" => $car->id,                      //车型ID
        ]);
        $result = $this->service->destroyDetail($model->id);
        $this->assertEquals(1,$result);
        CarType::destroy($car->id);
    }
    /**
     * @group customer
     */
    public function testGetExistOwnerName()
    {
        $owner = factory(Owner::class)->create([
            "user_owner_group_id" => 1,
        ]);
        /** @var OwnerPriceDirectLogistic $model */
        $model = OwnerPriceDirectLogistic::query()->find($this->data["models"][0]["id"]);
        $model->owners()->sync([$owner->id]);
        $result = $this->service->getExistOwnerName($owner->id,$this->data["models"][0]["id"]);
        $this->assertCount(0,$result);
        $result = $this->service->getExistOwnerName($owner->id,null);
        $this->assertCount(1,$result);
        $this->assertEquals($owner->name,$result[0]);
        Owner::destroy($owner->id);
        $model->owners()->sync([]);
    }
    /**
     * @group customer
     */
    public function testMatching()
    {
        $model = factory(OwnerPriceDirectLogistic::class)->create([
            "base_km" => 2,
        ]);
        $this->data["models"][] = $model->toArray();
        $owner = factory(Owner::class)->create([
            "user_owner_group_id" => 1,
        ]);
        $model->owners()->sync([$owner->id]);
        $cars = factory(\App\CarType::class,2)->create();
        $item1 = factory(OwnerPriceDirectLogisticCar::class)->create([
            "owner_price_direct_logistic_id" => $model->id,   //直发车计费ID
            "car_type_id" => $cars[0]->id,                      //车型ID
            "base_fee" => 1.5,                         //起步费
            "additional_fee" =>2.5,                   //续费(元/KM)
        ]);
        $item2 = factory(OwnerPriceDirectLogisticCar::class)->create([
            "owner_price_direct_logistic_id" => $model->id,   //直发车计费ID
            "car_type_id" => $cars[1]->id,                      //车型ID
            "base_fee" =>1.3,                         //起步费
            "additional_fee" =>1.6,                   //续费(元/KM)
        ]);
        $result = $this->service->matching(10,$owner->id,$cars[0]->id);
        $this->assertEquals([21.5,null],$result);
        $result = $this->service->matching(10,$owner->id,$cars[1]->id);
        $this->assertEquals([14.1,null],$result);
        $model->owners()->sync([]);
        Owner::destroy($owner->id);
        CarType::destroy(array_column($cars->toArray(),"id"));
        OwnerPriceDirectLogisticCar::destroy([$item1->id,$item2->id]);
    }

    public function tearDown(): void
    {
        OwnerPriceDirectLogistic::destroy(array_column($this->data["models"],"id"));
        parent::tearDown();
    }
}
