CREATE TABLE produits (
  id SERIAL PRIMARY KEY,
  nom TEXT,
  caracteristiques VECTOR(6)
);

INSERT INTO produits (nom, caracteristiques) VALUES
('Tomate',      '[0.6, 0.3, 0.3, 1.0, 0.5, 1.0]'),
('Concombre',   '[0.2, 0.1, 0.9, 1.0, 0.0, 0.0]'),
('Courgette',   '[0.3, 0.1, 0.5, 1.0, 0.0, 0.0]'),
('Carotte',     '[0.5, 0.1, 0.8, 0.5, 0.0, 1.0]'),
('Poivron',     '[0.4, 0.2, 0.6, 1.0, 0.0, 1.0]'),
('Aubergine',   '[0.2, 0.1, 0.4, 1.0, 0.0, 0.0]'),
('Pomme',       '[0.8, 0.3, 0.9, 1.0, 1.0, 1.0]'),
('Poire',       '[0.7, 0.2, 0.7, 1.0, 1.0, 0.0]'),
('Fraise',      '[0.9, 0.4, 0.7, 1.0, 1.0, 1.0]'),
('Cerise',      '[0.8, 0.3, 0.6, 1.0, 1.0, 1.0]'),
('Citron',      '[0.2, 1.0, 0.6, 1.0, 1.0, 1.0]'),
('Orange',      '[0.7, 0.6, 0.7, 1.0, 1.0, 1.0]'),
('Banane',      '[0.9, 0.1, 0.2, 1.0, 1.0, 0.0]'),
('Raisin',      '[0.8, 0.3, 0.5, 1.0, 1.0, 1.0]'),
('Pastèque',    '[0.8, 0.2, 0.6, 1.0, 1.0, 1.0]'),
('Melon',       '[0.9, 0.2, 0.5, 1.0, 1.0, 1.0]'),
('Betterave',   '[0.5, 0.1, 0.4, 0.0, 0.0, 1.0]'),
('Radis',       '[0.2, 0.3, 0.9, 0.5, 0.0, 1.0]'),
('Brocoli',     '[0.1, 0.1, 0.6, 0.0, 0.0, 0.0]'),
('Chou-fleur',  '[0.1, 0.1, 0.7, 0.0, 0.0, 0.0]');

alter table produits
add column nutrition vector(10);

UPDATE produits
SET nutrition = ARRAY[energie, proteines, glucides, sucres, graisses, graisses_saturees, sel, fibres, nutriscore, additifs]::vector;


with selection as (
select op.id, product_name, brands, "energy-kcal_100g", fat_100g, "saturated-fat_100g",
  "carbohydrates_100g", "sugars_100g", "proteins_100g", "salt_100g", "fiber_100g",
  "nutriscore_score", "additives_n", string_to_array(additives_en, ','),
  "potassium_100g", "calcium_100g", "vitamin-a_100g", "vitamin-c_100g",
  main_category_en
  from openfoodfacts_cleaned op
  left join produit p on p.id = op.id
where brands like 'Familia%'
    and p.id is null
	and "energy-kcal_100g" is not null
	and proteins_100g  is not null
	and fat_100g  is not null
	and "saturated-fat_100g"   is not null
	and carbohydrates_100g is not null
	and sugars_100g   is not null
	and salt_100g   is not null
	and nutriscore_score is not null
)
insert into produit (id, nom, marque,
    energie, graisse, graisse_saturee,
	glucide, sucre, proteines, sel,
	fibres, nutriscore, additifs, additifs_list,
	potassium, calcium, vitamin_a, vitamin_c, categorie)
select * from selection;
