-- Options

	-- get all options

	PROCEDURE getAll(
		IN language_id INT,
		IN start INT,
		IN limit INT,
		IN cursor INT, -- current cursor (option_id) for cursor pagination
		OUT fetch_all, 
		OUT fetch_one,
	)
	BEGIN
		-- option
		SELECT `option`.*, option_content.name
			FROM `option`
			INNER JOIN option_content 
				ON option_content.option_id = `option`.option_id AND option_content.language_id = :language_id
		
		WHERE 1 = 1
	
		-- cursor pagination
	
		@IF isset(:cursor)
	
		THEN
	
			AND `option`.option_id > :cursor
	
		END @IF
	
		ORDER BY option.option_id DESC
		
		-- limit
		@IF isset(:limit)
		THEN		
			LIMIT :limit OFFSET :start
		END @IF;
		
		SELECT count(*) FROM (
			
			@SQL_COUNT(`option`.option_id, `option`) -- this takes previous query removes limit and replaces select columns with parameter product_id
			
		) as count;		
			
	END	
	
	-- get option

	PROCEDURE get(
		IN option_id INT,
		IN language_id INT,
		OUT fetch_row, 
	)
	BEGIN
		-- option
		SELECT *
			FROM `option` as _ 
			INNER JOIN option_content 
				ON option_content.option_id = _.option_id AND option_content.language_id = :language_id

		WHERE _.option_id = :option_id;
	END
	
	-- add option

	PROCEDURE add(
		IN option ARRAY,
		IN language_id INT,
		OUT insert_id
	)
	BEGIN
		
		-- allow only table fields and set defaults for missing values
		:option_data  = @FILTER(:option, option)
		
		
		INSERT INTO `option` 
			
			( @KEYS(:option_data) )
			
	  	VALUES ( :option_data );		
		
		
		:option_content  = @FILTER(:option, option_content)
	  	
		INSERT INTO option_content 
			
			( @KEYS(:option_content), language_id, option_id )
			
	  	VALUES ( :option_content, :language_id, @result.option);


	END
	
	-- edit option
	CREATE PROCEDURE edit(
		IN option ARRAY,
		IN option_id INT,
		OUT affected_rows,
		OUT affected_rows
	)
	BEGIN

		-- allow only table fields and set defaults for missing values
		:option_data = @FILTER(:option, option)

		UPDATE `option`
			
			SET @LIST(:option_data) 
			
		WHERE option_id = :option_id;
		
		-- allow only table fields and set defaults for missing values
		:option_content = @FILTER(:option, option_content)

		UPDATE option_content
			
			SET @LIST(:option_content) 
			
		WHERE option_id = :option_id

	END
	
	
	-- delete option

	PROCEDURE delete(
		IN option_id ARRAY,
		OUT affected_rows, 
		OUT affected_rows, 
	)
	BEGIN
		-- option value
		DELETE FROM option_value_content WHERE option_value_id IN (:option_value_id);
		DELETE FROM option_value WHERE option_value_id IN (:option_id);

		-- option	
		DELETE FROM option_content WHERE option_id IN (:option_id);
		DELETE FROM `option` WHERE option_id IN (:option_id);
	END
