<?php
namespace Lycoreco\Apps\Recipes\Models;

use Lycoreco\Includes\Model\BaseModel;

class IngredientInRecipeModel extends BaseModel
{
    public $field_ingredient_id;
    public $field_recipe_id;
    public $field_amount;
    
    static protected $search_fields = ['tb1.name'];
    public $ingredient_name;
    public $ingredient_unit;
    static protected $additional_fields = array(
        [
            'field' => [
                'tb1.name AS ingredient_name',
                'tb1.unit_name AS ingredient_unit'
            ],
            'join_table' => 'ingredients tb1 ON tb1.id = obj.ingredient_id'
        ]
    );

    static protected $table_name = 'recipe_ingredients';
    static protected $table_fields = [
        'id'            => 'int',
        'ingredient_id' => 'int',
        'recipe_id'    => 'int',
        'amount'        => 'int'
    ];
    public static function init_table()
    {
        $result = db_query('CREATE TABLE ' . static::$table_name . ' (
            id INT AUTO_INCREMENT PRIMARY KEY,

            ingredient_id INT NOT NULL,
            recipe_id INT NOT NULL,
            amount INT NOT NULL,

            FOREIGN KEY (ingredient_id) REFERENCES ingredients(id) ON DELETE CASCADE,
            FOREIGN KEY (recipe_id) REFERENCES recipes(id) ON DELETE CASCADE
        );');
        return $result;
    }
    public function get_count()
    {
        return $this->field_amount . ' ' . $this->ingredient_unit;
    }
}