\c postgres;

drop database tetastock;
drop role tetastock;

create role tetastock with LOGIN ENCRYPTED PASSWORD 'tetastock';

create database tetastock;

\c tetastock;

CREATE TABLE stock_users (
  id serial primary key,
  mail text not NULL,
  password text not NULL,
  name text not NULL
);

CREATE TABLE stock_kits (
  id serial primary key,
  name text not NULL unique,
  designation text not NULL
);

CREATE TABLE stock_providers (
  id serial primary key,
  name text unique not NULL,
  address text not NULL,
  mail text not NULL,
  url text not NULL,
  comment text not NULL
);

CREATE TABLE stock_componants (
  id serial primary key,
  reference varchar(20) unique not NULL,
  designation varchar(50) not NULL,
  last_price NUMERIC not NULL default 0,
  mean_price NUMERIC not NULL default 0,
  quantity NUMERIC not NULL default 0,
  min_quantity NUMERIC not NULL default 0,
  place varchar(15) not NULL,
  provider_id integer REFERENCES stock_providers(id)
);

CREATE TABLE stock_kit_compositions (
  id serial primary key,
  kit_id integer REFERENCES Stock_kits(id),
  componant_id integer REFERENCES stock_componants(id),
  quantity integer not NULL
);

CREATE TABLE stock_orders (
  id serial primary key,
  componant_id integer REFERENCES stock_componants(id),
  quantity integer not NULL,
  price NUMERIC not NULL default 0,
  date timestamp not NULL
);

alter table stock_users owner to tetastock;
alter table stock_kits owner to tetastock;
alter table stock_providers owner to tetastock;
alter table stock_componants owner to tetastock;
alter table stock_kit_compositions owner to tetastock;
alter table stock_orders owner to tetastock;
alter database tetastock owner to tetastock;

insert into stock_users (mail, password, name) values
('doug.letough@free.fr', '$2a$08$578910202124252729313uTyggq4ANEjMljcClFriOqcsttB2fnAW', 'Doug Le Tough');
insert into stock_users (mail, password, name) values
('doug@redatomik.org', '$2a$08$578910202124252729313uTyggq4ANEjMljcClFriOqcsttB2fnAW', 'Doug Le Tough');

insert into stock_providers (name, address, mail, url, comment)
  values ('Tous', 'N/A', 'N/A', 'N/A', 'N/A');
insert into stock_providers (name, address, mail, url, comment)
  values ('Aucun', 'N/A', 'N/A', 'N/A', 'N/A');
insert into stock_providers (name, address, mail, url, comment)
  values ('China Elec Co', 'Beijing', 'sales@chinaelecco.cc', 'https://chinaelecco.cc', 'Pas cher mais délai excessif et composants pourris');

insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-01', 'Resistance 10KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Lor-01', '1 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-02', 'Resistance 11KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-03', 'Resistance 12KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-04', 'Resistance 13KΩ', 13.34, 12.42, 73, 0, 'B 43', 2);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-05', 'Resistance 14KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Lor-02', '2 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-06', 'Resistance 15KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-07', 'Resistance 16KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-08', 'Resistance 17KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-09', 'Resistance 18KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Lor-03', '3 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-10', 'Resistance 19KΩ', 13.34, 12.42, 73, 0, 'B 43', 2);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-11', 'Resistance 20KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-12', 'Resistance 21KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-13', 'Resistance 22KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Lor-04', '4 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-14', 'Resistance 23KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-15', 'Resistance 24KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-16', 'Resistance 25KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-17', 'Resistance 26KΩ', 13.34, 12.42, 73, 0, 'B 43', 2);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Lor-05', '5 Lorem ipsum dolor sit ametconsectetur adipiscing', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-18', 'Resistance 27KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-19', 'Resistance 28KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);
insert into stock_componants (reference, designation, last_price, mean_price, quantity, min_quantity, place, provider_id)
  values ('Res-20', 'Resistance 29KΩ', 13.34, 12.42, 73, 0, 'B 43', 3);

insert into stock_kits (name, designation) values ('Kit_test', 'Kit de test');
insert into stock_kits (name, designation) values ('Kit2_test2', 'Kit de test 2');
insert into stock_kits (name, designation) values ('MF_K', 'Mother fucking kit');

insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 5, 1);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 8, 2);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 10, 2);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (1, 12, 7);

insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 5, 2);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 4, 1);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 9, 3);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 22, 2);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 1, 5);
insert into stock_kit_compositions (kit_id, componant_id, quantity) values (2, 14, 8);
