-- Subscription plans

	-- get all subscription plan

	CREATE PROCEDURE getAll(
		IN language_id INT,
		IN subscription_plan_id ARRAY,
		IN start INT,
		IN limit INT,
		IN cursor INT, -- current cursor (subscription_plan_id) for cursor pagination
		OUT fetch_all, 
		OUT fetch_one
	)
	BEGIN
		-- subscription_plan
		SELECT subscription_plan.*,subscription_plan_content.name,subscription_plan_content.content,subscription_plan.subscription_plan_id as array_key
			FROM subscription_plan 
			INNER JOIN subscription_plan_content ON subscription_plan_content.subscription_plan_id = subscription_plan.subscription_plan_id 
												 AND subscription_plan_content.language_id = :language_id
		WHERE 1 = 1

		@IF isset(:subscription_plan_id)
		THEN		
			AND subscription_plan.subscription_plan_id IN (:subscription_plan_id)
		END @IF

		-- cursor pagination

		@IF isset(:cursor)

		THEN

		    AND subscription_plan.subscription_plan_id > :cursor

		END @IF

		-- limit
		@IF isset(:limit)
		THEN		
			LIMIT :limit OFFSET :start
		END @IF;
		
		SELECT count(*) FROM (
			
			@SQL_COUNT(subscription_plan.subscription_plan_id, subscription_plan) -- this takes previous query removes limit and replaces select columns with parameter product_id
			
		) as count;		
			
	END	
	
	-- get subscription plan

	CREATE PROCEDURE get(
		IN subscription_plan_id INT,
		OUT fetch_row 
	)
	BEGIN
		-- subscription plan
		SELECT _.*,subscription_plan_content.name,subscription_plan_content.content
			FROM subscription_plan as _ 
			INNER JOIN subscription_plan_content ON subscription_plan_content.subscription_plan_id = _.subscription_plan_id 
												AND subscription_plan_content.language_id = :language_id			
			
			WHERE _.subscription_plan_id = :subscription_plan_id;
	END
	
	-- add subscription plan

	CREATE PROCEDURE add(
		IN subscription_plan ARRAY,
		OUT insert_id
	)
	BEGIN
		
		-- allow only table fields and set defaults for missing values
		:subscription_plan_data  = @FILTER(:subscription_plan, subscription_plan)
		
		INSERT INTO subscription_plan 
			
			( @KEYS(:subscription_plan_data) )
			
	  	VALUES ( :subscription_plan_data );

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

		INSERT INTO subscription_plan_content 
			
			( @KEYS(:subscription_plan_content), language_id, subscription_plan_id )
			
	  	VALUES ( :subscription_plan_content, :language_id, @result.subscription_plan );

	END
	
	-- edit subscription plan
	
	CREATE PROCEDURE edit(
		IN subscription_plan ARRAY,
		IN subscription_plan_id INT,
		IN language_id INT,
		OUT affected_rows
	)
	BEGIN

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

		UPDATE subscription_plan
			
			SET @LIST(:subscription_plan_data) 
			
		WHERE subscription_plan_id = :subscription_plan_id;

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

		UPDATE subscription_plan_content
			
			SET @LIST(:subscription_plan_content) 
			
		WHERE subscription_plan_id = :subscription_plan_id AND language_id = :language_id

	END
	
	
	-- delete subscriptionplan

	CREATE PROCEDURE delete(
		IN subscription_plan_id ARRAY,
		OUT affected_rows, 
		OUT affected_rows 
	)
	BEGIN
		-- user_group
		DELETE FROM subscription_plan WHERE subscription_plan_id IN (:subscription_plan_id);
	END
