<?php

namespace domain\Product\Accessory;

use common\components\ActiveRecordCommon;
use domain\Producer\Producer\Producer;
use domain\Product\ProductAccessory\ProductAccessory;
use yii\db\ActiveQuery;

class Accessory extends ActiveRecordCommon
{
    // Produits sélectionnés dans le formulaire d'édition
    public $selected_products_ids;

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

    public function rules()
    {
        return [
            [['name', 'id_producer'], 'required'],
            [['name'], 'string', 'max' => 255],
            [['quantity', 'id_producer'], 'integer'],
            [['selected_products_ids'], 'safe'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Nom',
            'quantity' => 'Quantité',
        ];
    }

    /* Getters / Setters */

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getProducer(): Producer
    {
        return $this->producerRelation;
    }

    public function setProducer(Producer $producer): self
    {
        $this->populateFieldObject('id_producer', 'producerRelation', $producer);
        return $this;
    }

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

    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function getQuantity(): ?int
    {
        return $this->quantity;
    }

    public function setQuantity(?int $quantity): self
    {
        $this->quantity = $quantity;
        return $this;
    }

    public function getProductAccessories(): array
    {
        return $this->productAccessoriesRelation;
    }

    public function getSelectedProductsIds()
    {
        return $this->selected_products_ids;
    }

    public function setSelectedProductsIds(array $selectedProductsIdsArray): self
    {
        $this->selected_products_ids = $selectedProductsIdsArray;
        return $this;
    }

    /* Relations */

    public function getProducerRelation(): ActiveQuery
    {
        return $this->hasOne(Producer::class, ['id' => 'id_producer']);
    }

    public function getProductAccessoriesRelation(): ActiveQuery
    {
        return $this->hasMany(ProductAccessory::class, ['id_accessory' => 'id']);
    }
}