create extension if not exists ltree;
create extension if not exists pgtap;
create extension if not exists postgis;
create extension if not exists pgrouting;
create extension if not exists vector;

create table adherent (
  id int primary key,
  nom text,
  prenom text,
  genre smallint,
  naissance date,
  codepostal text
);

create table famille (
  code text primary key,
  famille text,
  code_parent text, -- references famille(code)
  arborescence ltree
);

create table article (
  code text primary key,
  article text,
  famille_code text, -- references famille(code)
  factpoids boolean,
  unitevente int,
  prix decimal,
  suivistock int
);

create table ticket (
  id int primary key,
  date_ticket timestamp,
  adherent_id int,  -- references adherent(id)
  mode_rglt int
);

create table ligne (
  id int primary key,
  ticket_id int, --references ticket (id),
  article_code text, -- references article (code)
  prix_unitaire decimal,
  quantite decimal
);


CREATE TABLE route (
  id int primary key,
  source int,
  target int,
  cost double precision,
  reverse_cost double precision
);

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]');
