<?php

namespace domain\Order\OrderStatusHistory;

use common\components\ActiveRecordCommon;
use domain\Order\Order\Order;
use domain\Order\OrderStatus\OrderStatus;
use domain\User\User\User;

class OrderStatusHistory extends ActiveRecordCommon
{
    protected OrderStatus $orderStatusObject;

    public function getOrder(): Order
    {
        return $this->orderRelation;
    }

    public function setOrder(Order $order): self
    {
        $this->populateFieldObject('id_order', 'orderRelation', $order);
        return $this;
    }

    public function getUser(): User
    {
        return $this->userRelation;
    }

    public function setUser(User $user): self
    {
        $this->populateFieldObject('id_user', 'userRelation', $user);
        return $this;
    }

    public function getDate(): \DateTime
    {
        return new \DateTime($this->date);
    }

    public function setDate(\DateTime $date): self
    {
        $this->date = $date->format('Y-m-d H:i:s');
        return $this;
    }

    public function getOrderStatusAlias(): string
    {
        return $this->order_status_alias;
    }

    public function setOrderStatusAlias(string $orderStatusAlias): self
    {
        $this->order_status_alias = $orderStatusAlias;
        return $this;
    }

    public function getOrderStatus(): OrderStatus
    {
        return $this->orderStatusObject;
    }

    public function setOrderStatus(OrderStatus $orderStatus): self
    {
        $this->orderStatusObject = $orderStatus;
        $this->setOrderStatusAlias($orderStatus->getAlias());
        return $this;
    }

    public static function tableName()
    {
        return 'order_status_history';
    }

    public function rules()
    {
        return [
            [['id_order', 'order_status_alias', 'date', 'id_user'], 'required'],
            [['id_order', 'id_user'], 'integer'],
            [['order_status_alias'], 'string'],
            [['date'], 'safe'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'id_order' => 'Commande',
            'order_status_alias' => 'Statut',
            'date' => 'Date',
            'id_user' => 'Utilisateur'
        ];
    }

    /* Relations */

    protected function getOrderRelation()
    {
        return $this->hasOne(Order::class, ['id' => 'id_order']);
    }

    protected function getUserRelation()
    {
        return $this->hasOne(User::class, ['id' => 'id_user']);
    }
}