drop table if exists jardin;
drop table if exists saison;
drop table if exists fermeture;
drop table if exists ferie;

drop table if exists profil;
drop table if exists cotisation;
drop table if exists adherent;
drop table if exists adhesion;

drop table if exists produit;
drop table if exists panier;

drop table if exists depot;
drop table if exists frequence;
drop table if exists preparation;
drop table if exists calendrier;
drop table if exists tournee;
drop table if exists distribution;
drop table if exists planning;

drop table if exists abonnement;
drop table if exists livraison;


select load_extension('mod_spatialite');
select InitSpatialMetaData();

--pragma

create table jardin
(
  id           integer primary key,
  jardin       text not null,
  tva          text,
  adresse      text,
  code_postal  text,
  ville        text,
  contact      text,
  telephone    text,
  email        text
);

select AddGeometryColumn('jardin', 'geom', 4326, 'POINT', 'XY');

insert into jardin (id, jardin, tva, adresse, code_postal, ville, geom, contact, telephone, email) values
  (1,'jardin de Cocagne Thaon les Vosges','FR42400245775',
     'Prairie Claudel','88150','Thaon-les-Vosges', MakePoint(6.427672, 48.2531016, 4326),
     'Elvire Helle','0329316498','compta@jdc-thaon.fr');

/* Calendrier */

create table saison
(
  id         integer primary key,
  jardin_id  integer references jardin(id),
  saison     text   not null,
  date_debut date   not null,
  date_fin   date   not null
);

insert into saison (id,jardin_id,saison,date_debut,date_fin) values
  (2009,1,'2009','2009-01-01','2009-12-31'),
  (2010,1,'2010','2010-01-01','2010-12-31'),
  (2011,1,'2011','2011-01-01','2011-12-31'),
  (2012,1,'2012','2012-01-01','2012-12-31'),
  (2013,1,'2013','2013-01-01','2013-12-31'),
  (2014,1,'2014','2014-01-01','2014-12-31'),
  (2015,1,'2015','2015-01-01','2015-12-31'),
  (2016,1,'2016','2016-01-01','2016-12-31'),
  (2017,1,'2017','2017-01-01','2017-12-31'),
  (2018,1,'2018','2018-01-01','2018-12-31'),
  (2019,1,'2019','2019-01-01','2019-12-31'),
  (2020,1,'2020','2020-01-01','2020-12-31'),
  (2021,1,'2021','2021-01-01','2021-12-31'),
  (2022,1,'2022','2022-01-01','2022-12-31'),
  (2023,1,'2023','2023-01-01','2023-12-31'),
  (2024,1,'2024','2024-01-01','2024-12-31'),
  (2025,1,'2025','2025-01-01','2025-12-31');

create table fermeture
(
  id        integer primary key,
  saison_id integer   not null references saison(id),
  semaine   integer not null
);

insert into fermeture (id,saison_id,semaine) values
  (1,2020,52),
  (2,2021,1),
  (3,2021,52),
  (4,2022,51),
  (5,2022,52),
  (6,2023,52),
  (7,2024,1),
  (8,2024,52),
  (9,2025,1),
  (10,2025,52);

create table ferie
(
  id         integer primary key,
  saison_id  integer not null references saison(id),
  ferie      text   not null,
  jour       date   not null
);

insert into ferie (id,saison_id,ferie,jour) values
  (1,2025,'Lundi de Pâques','2025-04-21'),
  (2,2025,'Fête du Travail','2025-05-01'),
  (3,2025,'Victoire 1945','2025-05-08'),
  (4,2025,'Ascension','2025-05-29'),
  (5,2025,'Pentecôte','2025-06-09'),
  (6,2025,'Fête nationale','2025-07-14'),
  (7,2025,'Assomption','2025-08-15'),
  (8,2025,'Toussaint','2025-11-01'),
  (9,2025,'Armistice 1918','2025-11-11');



create table profil
(
  id        integer primary key,
  profil    text   not null
);

insert into profil (id, profil) values
  (1, 'adhérent'),
  (2, 'non adhérent'),
  (3, 'professionnel'),
  (4, 'salarié');


create table cotisation
(
  id        integer       primary key,
  saison_id integer        not null references saison(id),
  profil_id integer        not null references profil(id),
  montant   numeric(8, 2) not null
);

insert into cotisation (id, saison_id, profil_id, montant) values
  (1,2025,1,15.0),
  (2,2025,2,0.0),
  (3,2025,3,100.0),
  (4,2025,4,2.0);

create table adherent
(
  id          integer primary key,
  adherent    text    not null,
  profil_id   integer not null references profil(id),
  depot_id    integer not null references depot(id),
  email       text,
  telephone   text,
  date_sortie date
);

create table adhesion
(
  id            integer primary key,
  adherent_id   integer not null references adherent(id),
  date_adhesion date   not null,
  montant       numeric(8, 2) not null,
  saison_id     integer not null references saison(id)
);

create table depot
(
  id               integer   not null primary key,
  depot            text      not null,
  capacite         integer   not null default 10,
  adresse          text,
  code_postal      text,
  ville            text
);

select AddGeometryColumn('depot', 'geom', 4326, 'POINT', 'XY');

insert into depot (id,depot,capacite,adresse,code_postal,ville,geom) values
  (0,'Livraison à domicile',100, NULL, NULL, NULL, NULL),
  (1,'Jardins de Cocagne',100,'Prairie Claudel','88150','Thaon-les-Vosges',MakePoint(6.427672, 48.2531016, 4326)),
  (2,'Asso Étudiant Universitaire',20,'9 rue de la Louvière','88000','Épinal',MakePoint(6.4531588, 48.1723212, 4326)),
  (3,'Conseil Départemental des Vosges',20,'8 rue de la Préfecture','88000','Épinal',MakePoint(6.4445154, 48.1721724, 4326)),
  (4,'Asso Rhyzome',20,'15 rue des Jardiniers','88000','Épinal',MakePoint(6.452224, 48.1706099, 4326)),
  (6,'Pharmacie Saint Nabord',20,'24 rue du Gal de Gaulle','88200','St Nabord',MakePoint(6.5807814, 48.0510352, 4326)),
  (8,'Denninger',20,'36 bis rue de la Plaine','88190','Golbey',MakePoint(6.4426982, 48.1929337, 4326)),
  (9,'3e Rive (Café Associatif)',20,'15 rue du Maréchal Lyautey','88000','Épinal',MakePoint(6.4457306, 48.177777, 4326)),
  (10,'Crédit Agricole',20,'Allée des Érables','88000','Épinal',MakePoint(6.454908, 48.203990, 4326)),
  (12,'Centre Léo Lagrange',60,'6 av. Salvador Allende','88000','Épinal',MakePoint(6.4599403, 48.1938105, 4326)),
  (13,'Boulassel Docelles',20,'1 rue Moncey','88460','Docelles',MakePoint(6.6162166, 48.1460719, 4326)),
  (14,'Ligue de l’enseignement',40,'15 rue Général de Reffye','88000','Épinal',MakePoint(6.4323215, 48.1819469, 4326)),
  (15,'Garage Renault- Station Service',20,'664 rue de la Gare','88550', 'Pouxeux',MakePoint(6.5760129, 48.1051197, 4326)),
  (16,'Adinolfi',40,'7 allée des Primevères','88390','Les Forges',MakePoint(6.397633, 48.171791, 4326)),
  (17,'Lecompte François',40,'24 route du Noirpré','88530','Le Tholy',MakePoint(6.7477787, 48.0812967, 4326)),
  (18,'Papeterie Norske Skog',20,'ZI Route Charles Pellerin','88190','Golbey',MakePoint(6.423976, 48.208795, 4326)),
  (19,'Botanic',20,'9 av. des Terres St Jean','88000','Épinal',MakePoint(6.4692286, 48.1891998, 4326)),
  (20,'Pro & Cie',40,'7 rue de la République','88400','Gérardmer',MakePoint(6.877433, 48.074172, 4326)),
  (21,'Mme Pierot Charmes',40,'15 rue Ste Barbe','88130','Charmes',MakePoint(6.2951122, 48.3777043, 4326)),
  (25,'DVIS Epinal',20,'1 Rue de la Préfecture','88000','Épinal',MakePoint(6.44875, 48.172438, 4326)),
  (26,'Peridon',20,'7 rue du Savron','88220','Raon-aux-Bois',MakePoint(6.5036466, 48.0504027, 4326)),
  (31,'Chambre d’Agriculture',20,'17 rue André Vitu','88000','Épinal',MakePoint(6.465403, 48.1775685, 4326)),
  (34,'Biocoop',20,'7 rue du Boudiou','88000','Épinal',MakePoint(6.447245, 48.174228)),
  (36,'Moustaches Bikes',20,'5 rue du Ruisseau','88150','Thaon-les-Vosges',MakePoint(6.4005773, 48.2576491)),
  (38,'Vosgelis Remiremont',20,'4 place de l’Abbaye','88200','Remiremont',MakePoint(6.592068, 48.015964)),
  (42,'Église Saint Antoine',60,'12 rue Armand Colle','88000','Épinal',MakePoint(6.4489619, 48.1604568)),
  (46,'Maison de l’Environnement',20,'12 rue  Raymond Poincaré','88000','Épinal',MakePoint(6.449693, 48.175374, 4326)),
  (48,'Vosgelis',20,'8 quai Barbier','88000','Épinal',MakePoint(6.445190, 48.171198, 4326)),
  (50,'Association GACI',20,'26 rue de la Joncherie','88200','Remiremont',MakePoint(6.5934293, 48.0189339, 4326)),
  (55,'Brico Marché',20,'2 rue de Fraisne','88600','Bruyères',MakePoint(6.7196903, 48.2050495, 4326)),
  (57,'Office du tourisme',20,'6 place C. Poncelet','88200','Remiremont',MakePoint(6.5917178, 48.0159918, 4326)),
  (60,'Point Vert Mafra',20,'5 rue des Résistants Zac Barbazan','88600','Bruyères',MakePoint(6.7208371, 48.2032056, 4326)),
  (61,'La quarterelle',20,'3 rue Carterelle','88200','Épinal',MakePoint(6.7208371, 48.2032056, 4326)),
  (62,'Léo Lagrange',20,'Chemin du Tambour Major','88000','Épinal',MakePoint(6.7208371, 48.2032056, 4326)),
  (65,'Bouvier Emmanuel',20,'557 rue du Chêne','88220','Hadol',MakePoint(6.484004, 48.108499, 4326)),
  (75,'APF - Local extérieur – ESAT',20,'rue de la papeterie','88000','Dinozé',MakePoint(6.4738942, 48.1383687, 4326)),
  (77,'La tête à Toto',20,'26 quai des Bons Enfants','88000','Épinal',MakePoint(6.4409549, 48.1748623, 4326)),
  (78,'UIMM',20,'Label Initiative','88150','Thaon les Vosges',MakePoint(6.4409549, 48.1748623, 4326)),
  (80,'Résidence du Monsey',20,'Ruelle de Monsey', '88450', 'Vincey', MakePoint(6.330850, 48.337907, 4326)),
  (81,'Complexe Sportif',40,'Bld Georges Clemenceau','88130','Charmes',MakePoint(6.298452, 48.375298, 4326)),
  (83,'Fives',20,'2 rue des Amériques','88190','Golbey',MakePoint(6.428831, 48.200150, 4326)),
  (84,'Nomexy Secours Catholique',20,'1 place de Verdun', '88440', 'Nomexy', MakePoint(6.386527, 48.305704, 4326));

create table produit
(
  id      integer       primary key,
  produit text          not null,
  prix    numeric(8, 2) not null,
  marge   numeric(8, 2) not null
);

insert into produit values
(1,'Panier simple',13.80,40.0),
(2,'Panier familial',23.70,40.0),
(3,'Panier fruité 1',14.00,70.0),
(4,'Panier fruité 3',23.00,70.0),
(5,'Panier fruité 2',17.00,70.0),
(6,'Oeufs x6',3.05,50.0),
(7,'Panier fruité entreprise',23.50,70.0);

create table frequence
(
  id           integer primary key,
  frequence    text   not null
);

insert into frequence (id,frequence) values
  (1,'hebdomadaire'),
  (2,'15 jours'),
  (3,'libre');

create table panier
(
  id           integer primary key,
  produit_id   integer   not null references produit(id),
  panier       text     not null,
  frequence_id integer   not null references frequence(id),
  quantite     integer not null,
  prix         numeric(8, 2) not null
);

insert into panier (id,produit_id,panier,frequence_id,quantite,prix) values
  (1,1,'Panier simple hebdomadaire',1,50,690.0),
  (2,1,'Panier simple 15 jours',2,25,404.0),
  (3,2,'Panier familial hebdomadaire',1,50,1185.0),
  (4,2,'Panier familial 15 jours',2,25,658.0),
  (5,1,'Panier simple unité',3,1,13.8),
  (6,2,'Panier familial unité',3,1,23.7),
  (7,1,'Panier solidaire',3,1,2.5),
  (8,3,'Panier fruité 1',3,1,14.0),
  (9,4,'Panier fruité 2',3,1,17.0),
  (10,5,'Panier fruité 3',3,1,23.0),
  (11,6,'Oeufs x6',3,1,3.05),
  (12,7,'Panier fruité 4',3,1,23.5);

insert into panier (id,produit_id,panier,frequence_id,quantite,prix) values
  (13,1,'Panier simple hebdomadaire à domicile',1,50,894.0),
  (14,1,'Panier simple 15 jours à domicile',2,25,497.0),
  (15,1,'Panier familial hebdomadaire à domicile',1,50,894.0),
  (16,1,'Panier familial 15 jours à domicile',2,25,497.0),
  (17,1,'Panier simple soutien x12',1,12,196.0),
  (18,1,'Panier simple soutien x6',2,6,98.0),
  (19,1,'Panier familial soutien x12',1,12,196.0),
  (20,1,'Panier familial soutien x6',2,6,98.0);

create table preparation
(
  id             integer primary key,
  preparation    text     not null,
  jour           integer not null
);

insert into preparation (id,preparation,jour) values
  (1,'Mardi',1),
  (2,'Jeudi',3);

create table calendrier
(
    id             integer primary key,
    saison_id      integer not null references saison(id),
    calendrier     text   not null
);

insert into calendrier (id,saison_id,calendrier) values
  (1,2025,'Livraisons du mardi'),
  (2,2025,'Livraisons du mercredi'),
  (3,2025,'Livraisons du jeudi'),
  (4,2025,'Livraisons du vendredi');

create table tournee
(
    id             integer primary key,
    tournee        text,
    preparation_id integer   not null references preparation(id),
    calendrier_id  integer   not null references calendrier(id),
    ordre          integer   not null,
    couleur        text      not null
);

insert into tournee values
(1,'Mardi (Épinal)',1,1,1,'yellow'),
(2,'Mardi (Jardins)',1,1,2,'orange'),
(3,'Mercredi matin',1,2,3,'red'),
(4,'Mercredi après-midi',1,2,4,'pink'),
(5,'Mercredi (Jardins)',1,2,6,'purple'),
(6,'Jeudi (Salariés)',2,3,8,'indigo'),
(7,'Vendredi (Épinal)',2,4,9,'azure'),
(8,'Vendredi (Jardins)',2,4,12,'lime'),
(9,'Jeudi (Charmes)',2,3,7,'green'),
(10,'Vendredi (Gérardmer)',2,4,10,'maroon'),
(11,'Mercredi (Ent.)',1,2,5,'gray-700'),
(12,'Vendredi (Ent.)',2,4,11,'khaki');

create table distribution
(
    id             integer  primary key,
    distribution   text     not null,
    tournee_id     integer  not null references tournee(id),
    depot_id       integer,
    adherent_id    integer,
    ordre          integer  not null
);

create table planning
(
  id            integer primary key,
  calendrier_id integer not null references calendrier(id),
  jour          date    not null
);

create table abonnement
(
    id               integer primary key,
    adherent_id      integer        not null references adherent(id),
    panier_id        integer,
    date_debut       date          not null,
    nombre           integer       not null,
    montant          numeric(8, 2) not null,
    mode_paiement_id integer       null,
    saison_id        integer       null references saison(id)
);

create table livraison
(
  id              integer primary key,
  abonnement_id   integer         not null references abonnement(id),
  distribution_id integer         not null references distribution(id),
  produit_id      integer         not null references produit(id),
  qte             integer         not null default 1,
  livre           etat_livraison  not null default 'à livrer',
  planning_id     integer         not null references planning(id)
);
