DROP TABLE IF EXISTS "printproducts_defaults";
DROP TYPE IF EXISTS border_types;

CREATE TYPE border_types AS ENUM ('white', 'black', 'none');

CREATE TABLE printproducts_defaults (
  print_id INTEGER PRIMARY KEY,
  width_mm numeric NOT NULL,
  height_mm numeric NOT NULL,
  crop_x numeric NOT NULL DEFAULT 0,
  crop_y numeric NOT NULL DEFAULT 0,
  border border_types NOT NULL
);

ALTER TABLE printproducts_defaults
    ADD CONSTRAINT printid_fkey
    FOREIGN KEY (print_id)
    REFERENCES "product-printproducts"(printid) MATCH SIMPLE;

------------------------------------

INSERT INTO printproducts_defaults (print_id, width_mm, height_mm, crop_x, crop_y, border)
SELECT
  printid as print_id,

	(CASE 
		WHEN width_height_ratio >= 1.2 THEN 700 -- landscape in landscape 
		ELSE 500 -- else square in landscape, square in square or square in portrait or portrait in portrait
	END) as width_mm, 

	(CASE 
		WHEN width_height_ratio <= 0.8571428571 THEN 700 -- portrait in portrait
		ELSE 500 -- else square in portrait, square in square, square in landscape or landscape in landscape
	END) as height_mm, 

	(CASE 
		WHEN width_height_ratio >= 1.4 THEN ( -- landscape in landscape
			LEAST(
				width - (height * 1.4 / 2.0), -- max right side
				GREATEST(
					height * 1.4 / 2.0, -- max left side = 0
					COALESCE("focusXpoint2", 50.0) / 100.0 * width -- focuspoint
				)
			) - (height * 1.4 / 2.0) -- convert to left edge
		) / CAST(width AS FLOAT) -- convert to percent
		--------------------
		WHEN width_height_ratio >= 1.2 AND width_height_ratio < 1.4 THEN 0 -- landscape in landscape with lesser ratio
		--------------------
		WHEN width_height_ratio > 1 AND width_height_ratio < 1.2 THEN ( -- square in landscape
			LEAST(
				width - (height / 2.0), -- max right side
				GREATEST(
					height / 2.0, -- max left side = 0
					COALESCE("focusXpoint2", 50.0) / 100.0 * width -- focuspoint
				)
			) - (height / 2.0) -- convert to left edge
		) / CAST(width AS FLOAT) -- convert to percent
		--------------------
		WHEN width_height_ratio < 1 AND width_height_ratio > 0.8571428571 THEN 0 -- square in portrait
		--------------------
		WHEN width_height_ratio <= 0.8571428571 AND width_height_ratio > 0.7142857143 THEN ( -- portrait in portrait with higher ratio
			LEAST(
				width - (height * 0.7142857143 / 2.0), -- max right side
				GREATEST(
					height * 0.7142857143 / 2.0, -- max left side = 0
					COALESCE("focusXpoint2", 50.0) / 100.0 * width -- focuspoint
				)
			) - (height * 0.7142857143 / 2.0) -- convert to left edge
		) / CAST(width AS FLOAT) -- convert to percent	
		--------------------
		WHEN width_height_ratio <= 0.7142857143 THEN 0 -- portrait in portrait
		--------------------
		ELSE 0 -- square in square
	END) as crop_x,

	(CASE 
		WHEN width_height_ratio >= 1.4 THEN 0 -- landscape in landscape
		--------------------
		WHEN width_height_ratio >= 1.2 AND width_height_ratio < 1.4 THEN ( -- landscape in landscape with lesser ratio
			LEAST(
				height - ((width / 1.4) / 2.0), -- max bottom side
				GREATEST(
					(width / 1.4) / 2.0, -- max top side = 0
					COALESCE("focusYpoint2", 50.0) / 100.0 * height -- focuspoint
				)
			) - (width / 1.4) / 2.0  -- convert to top edge
		) / CAST(height AS FLOAT) -- convert to percent
		--------------------
		WHEN width_height_ratio > 1 AND width_height_ratio < 1.2 THEN 0 -- square in landscape
		--------------------
		WHEN width_height_ratio < 1 AND width_height_ratio > 0.8571428571 THEN ( -- square in portrait
			LEAST(
				height - ((width) / 2.0), -- max bottom side
				GREATEST(
					(width) / 2.0, -- max top side = 0
					COALESCE("focusYpoint2", 50.0) / 100.0 * height -- focuspoint
				)
			) - (width) / 2.0  -- convert to top edge
		) / CAST(height AS FLOAT) -- convert to percent
		--------------------
		WHEN width_height_ratio <= 0.8571428571 AND width_height_ratio > 0.7142857143 THEN 0 -- portrait in portrait with higher ratio
		--------------------
		WHEN width_height_ratio <= 0.7142857143 THEN ( -- portrait in portrait
			LEAST(
				height - ((width / 0.7142857143) / 2.0), -- max bottom side
				GREATEST(
					(width / 0.7142857143) / 2.0, -- max top side = 0
					COALESCE("focusYpoint2", 50.0) / 100.0 * height -- focuspoint
				)
			) - (width / 0.7142857143) / 2.0  -- convert to top edge
		) / CAST(height AS FLOAT) -- convert to percent
		--------------------
		ELSE 0 -- square in square
	END) as crop_y,

	'none' as border
FROM
	"product-printproducts" ppp
	JOIN v_product vp ON ppp.productid = vp.id
	WHERE groupid = 7;
