<?php


namespace Tests\Services\OwnerAreaReportService;


use App\Owner;
use App\OwnerAreaReport;
use App\OwnerStoragePriceModel;
use App\Services\OwnerAreaReportService;
use App\Unit;
use App\UserOwnerGroup;
use Tests\TestCase;

class OwnerAreaReportServiceTest extends  TestCase
{
    /** @var OwnerAreaReportService */
    public $service;
    public $data;

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

        $userOwnerGroup = factory(\App\UserOwnerGroup::class)->create();
        $owner = factory(\App\Owner::class)->create([
            "user_owner_group_id"=>$userOwnerGroup->id,
        ]);
        $unit = factory(Unit::class)->create();
        $ownerStoragePriceModel = factory(\App\OwnerStoragePriceModel::class)->create([
            "unit_id" => $unit->id,
        ]);

        $this->data["owners"] = [$owner->toArray()];
        $this->data["units"] = [$unit->toArray()];
        $this->data["ownerStoragePriceModels"] = [$ownerStoragePriceModel->toArray()];
        $this->data["userOwnerGroups"] = [$userOwnerGroup->toArray()];
        $this->data["models"] = factory(OwnerAreaReport::class,3)->create([
            "owner_id" => $owner->id,
            "owner_storage_price_model_id" => $ownerStoragePriceModel->id,
            "user_owner_group_id" => $userOwnerGroup->id,
        ])->toArray();
    }


    /**
     * @group customer
     */
    public function testUpdate(){
        $model = $this->data["models"][0];
        $result = $this->service->update(["id"=>$model["id"]],[
            "accounting_area" => 1000,
        ]);
        $this->assertEquals(true,$result);
        $area = OwnerAreaReport::query()->find($model["id"]);
        $this->assertNotNull($area);
        $this->assertEquals(1000,$area->accounting_area);
    }
    /**
     * @group customer
     */
    public function testGet(){
        $ids = implode(",",array_column($this->data["models"],"owner_id"));
        $models = $this->service->get(["owner_id"=>$ids]);
        $this->assertGreaterThanOrEqual(3,count($models));
    }

    protected function tearDown(): void
    {
        Owner::destroy(array_column($this->data["owners"],"id"));
        Unit::destroy(array_column($this->data["units"],"id"));
        OwnerStoragePriceModel::destroy(array_column($this->data["ownerStoragePriceModels"],"id"));
        UserOwnerGroup::destroy(array_column($this->data["userOwnerGroups"],"id"));
        OwnerAreaReport::destroy(array_column($this->data["models"],"id"));
        parent::tearDown();
    }
}
