<?php

namespace Lycoreco\Apps\Recipes\Models;

use Lycoreco\Includes\Model\BaseModel;

class FavoriteModel extends BaseModel
{
    public $field_recipe_id;
    public $field_user_id;

    static protected $table_name = 'recipe_favorites';
    static protected $table_fields = [
        'id'            => 'int',
        'recipe_id'     => 'int',
        'user_id'       => 'int',
    ];

    public static function init_table()
    {
        $result = db_query('CREATE TABLE ' . static::$table_name . ' (
            id INT AUTO_INCREMENT PRIMARY KEY,

            recipe_id INT NOT NULL,
            user_id INT NOT NULL,

            FOREIGN KEY (recipe_id) REFERENCES recipes(id) ON DELETE CASCADE,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
        );');
        return $result;
    }

    public function valid()
    {
        $recipe = RecipeModel::get(array(
            [
                'name'      => 'obj.id',
                'type'      => '=',
                'value'     => $this->field_recipe_id
            ]
        ));
        if(!$recipe)
            return ['Recipe does not exist'];

        return true;
    }
}