<?php

namespace App\Modules\DefinedService\Location\Domain;

use App\Modules\FactUnit\Domain\IdFactUnit;
use App\Modules\Shared\Domain\Aggregate\AggregateAttribute;
use App\Modules\Shared\Domain\Aggregate\AggregateRoot;

class Location extends AggregateRoot
{

    /** @return AgregateAttribute[] */
    public static function attributes(): array
    {
        return [
            new AggregateAttribute('id', IdLocation::class, 'Id_Location', 'primary'),
            new AggregateAttribute('date', LocationDate::class, 'Location_Date'),
            new AggregateAttribute('dateUpdate', LocationDateUpdate::class, 'Location_DateUpdate'),
            new AggregateAttribute('name', LocationName::class, 'Location_Name'),
            new AggregateAttribute('abrv', LocationAbrv::class, 'Location_Abrv'),
            new AggregateAttribute('latitude', LocationLatitude::class, 'Location_Latitude'),
            new AggregateAttribute('longitude', LocationLongitude::class, 'Location_Longitude'),
            new AggregateAttribute('version', LocationVersion::class, 'Location_Version'),
            new AggregateAttribute('status', LocationStatus::class, 'Location_Status'),
        ];
    }

    public static function table(): string
    {
        return 't_location';
    }

    public function __construct(
        protected IdLocation $id,
        protected LocationDate $date,
        protected LocationDateUpdate $dateUpdate,
        protected LocationName $name,
        protected LocationAbrv $abrv,
        protected LocationLatitude $latitude,
        protected LocationLongitude $longitude,
        protected LocationVersion $version,
        protected LocationStatus $status,
    ) {}

    public static function create(
        IdLocation $id,
        LocationDate $date,
        LocationDateUpdate $dateUpdate,
        LocationName $name,
        LocationAbrv $abrv,
        LocationLatitude $latitude,
        LocationLongitude $longitude,
        LocationVersion $version,
        LocationStatus $status
    ) {
        $model = new self($id, $date, $dateUpdate, $name, $abrv, $latitude, $longitude, $version, $status);
        return $model;
    }

    public function getVersion(): int
    {
        return $this->version()->value();
    }

    public function id(): IdLocation
    {
        return $this->id;
    }

    public function date(): LocationDate
    {
        return $this->date;
    }

    public function dateUpdate(): LocationDateUpdate
    {
        return $this->dateUpdate;
    }

    public function name(): LocationName
    {
        return $this->name;
    }

    public function abrv(): LocationAbrv
    {
        return $this->abrv;
    }

    public function latitude(): LocationLatitude
    {
        return $this->latitude;
    }

    public function longitude(): LocationLongitude
    {
        return $this->longitude;
    }

    public function version(): LocationVersion
    {
        return $this->version;
    }

    public function status(): LocationStatus
    {
        return $this->status;
    }

    public function delete()
    {
        $this->status = new LocationStatus(LocationStatus::DELETED);
        $this->dateUpdate = LocationDateUpdate::reduceDatetimeToPeru(LocationDateUpdate::now());
    }

    public function setId(IdLocation $id): void
    {
        $this->id = $id;
    }

    public function setName(LocationName $name): void
    {
        $this->name = $name;
    }

    public function setAbrv(LocationAbrv $abrv): void
    {
        $this->abrv = $abrv;
    }

    public function setLatitude(LocationLatitude $latitude): void
    {
        $this->latitude = $latitude;
    }

    public function setLongitude(LocationLongitude $longitude): void
    {
        $this->longitude = $longitude;
    }

    public function setStatus(LocationStatus $status): void
    {
        $this->status = $status;
    }

    public function updateDateUpdate(): void
    {
        $this->dateUpdate = LocationDateUpdate::reduceDatetimeToPeru(LocationDateUpdate::now());
    }

    public function updateVersion(): void
    {
        $this->version = new LocationVersion($this->version->value() + 1);
    }
}
