create table point (
    id serial primary key,
    nom text not null,
    geom geometry(point, 4326) not null
);

create index idx_point_geom on point using gist (geom);

create table route (
  id serial primary key,
  source int,
  target int,
  cost double precision,
  reverse_cost double precision,
  geom geometry(linestring, 4326) not null
);

create index idx_route_geom on route using gist (geom);

create table tags (
  id serial primary key,
  name text unique
);

create table route_tags (
  route_id int references roads(id) on delete cascade,
  tag_id  int references tags(id) on delete cascade,
  primary key (route_id, tag_id)
);

create table route_costs (
  route_id int references roads(id),
  mode text,
  cost float,
  reverse_cost float,
  primary key (route_id, mode)
);

create table zone (
    id serial primary key,
    name text,
    geom geometry(polygon, 4326) -- on choisit srid 4326 (wgs84)
);

INSERT INTO point (id, nom, geom) VALUES
(1,'Boulangerie', ST_SetSRID(ST_MakePoint(-4.01570310149135423, 48.74486669121691307), 4326)),
(2,'Salle des fêtes', ST_SetSRID(ST_MakePoint(-4.01523914066345888, 48.74492186050463971), 4326)),
(3,'Crêperie', ST_SetSRID(ST_MakePoint(-4.01485656953699177, 48.74436722362130325), 4326)),
(4,'Le port', ST_SetSRID(ST_MakePoint(-4.0146327887195, 48.74421252036642), 4326)),
(5,'Marie', ST_SetSRID(ST_MakePoint(-4.01292331831630555, 48.74446490379569497), 4326)),
(6,'La Poste', ST_SetSRID(ST_MakePoint(-4.01297842611315758, 48.74533129595438652), 4326)),
(7, 'École primaire', ST_SetSRID(ST_MakePoint(-4.01383673054336843, 48.74532080203461248), 4326)),
(8, 'Pharmacie', ST_SetSRID(ST_MakePoint(-4.015872431573694, 48.74436631101314532), 4326)),
(9, 'Bibliothèque', ST_SetSRID(ST_MakePoint(-4.0169922393896265, 48.74407185159144), 4326)),
(10, 'Librairie', ST_SetSRID(ST_MakePoint(-4.016706525682735, 48.74357654268962), 4326)),
(11, 'Poissonerie', ST_SetSRID(ST_MakePoint(-4.016130238694671, 48.74305994900348), 4326)),
(12, 'Sémaphore', ST_SetSRID(ST_MakePoint(-4.01268322695224, 48.74633108784164), 4326)),
(13, 'Boucherie', ST_SetSRID(ST_MakePoint(-4.01149526816046631, 48.74529523306026846), 4326)),
(14, 'Bergerie', ST_SetSRID(ST_MakePoint(-4.011299684064602, 48.7474391861085), 4326)),
(15, 'Décheterie', ST_SetSRID(ST_MakePoint(-4.0092682382493825, 48.747176626882954), 4326)),
(16, 'Coiffeur', ST_SetSRID(ST_MakePoint(-4.01695656484633279, 48.74578944178636419), 4326)),
(17, 'Dentiste', ST_SetSRID(ST_MakePoint(-4.01820013254909014, 48.74479792341951878), 4326)),
(18, 'Barbier', ST_SetSRID(ST_MakePoint(-4.017619213917883, 48.743923429030154), 4326)),
(19, 'Épicerie', ST_SetSRID(ST_MakePoint(-4.01778756237945, 48.74377735868944), 4326)),
(20, 'Bijouterie', ST_SetSRID(ST_MakePoint(-4.018133119765466, 48.74377151586775), 4326)),
(21, 'Écurie', ST_SetSRID(ST_MakePoint(-4.021500089167666, 48.74375398739865), 4326)),
(22, 'Gendarmerie', ST_SetSRID(ST_MakePoint(-4.017636934769236, 48.74298272869709), 4326)),
(23, 'Église', ST_SetSRID(ST_MakePoint(-4.00974023695237225, 48.74437056754450026), 4326)),
(24, 'Collège', ST_SetSRID(ST_MakePoint(-4.01000671584753565, 48.74422424537268483), 4326)),
(25, 'Hôtel', ST_SetSRID(ST_MakePoint(-4.0070587566129525, 48.74134972191262), 4326)),
(26, 'Cabinet médical', ST_SetSRID(ST_MakePoint(-4.006265083289092, 48.74170845222292), 4326)),
(27, 'Pompier', ST_SetSRID(ST_MakePoint(-4.006068894377577, 48.74217891432291), 4326)),
(28, 'Cimetière', ST_SetSRID(ST_MakePoint(-4.006826896990252, 48.74318451229527), 4326)),
(29, 'Auberge de jeunesse', ST_SetSRID(ST_MakePoint(-4.003418425059911, 48.74296605378058), 4326)),
(30, 'Déchèterie', ST_SetSRID(ST_MakePoint(-4.003359497656666, 48.74446857837965), 4326)),
(31, 'Manoir', ST_SetSRID(ST_MakePoint(-4.024275254166002, 48.74298397587782), 4326)),
(32, 'Stade', ST_SetSRID(ST_MakePoint(-4.019140945079908, 48.742638305027356), 4326)),
(33, 'Gare maritime', ST_SetSRID(ST_MakePoint(-4.0182483929763535, 48.742208182126596), 4326));

INSERT INTO route (source, target, cost, reverse_cost, geom)
SELECT
    a.id AS source,
    b.id AS target,
    1 AS cost,
    1 AS reverse_cost,
    ST_MakeLine(a.geom, b.geom) AS geom
FROM point a
JOIN point b ON a.id = 6 and b.id = 7;


INSERT INTO zone (name, geom)
VALUES (
    'Ouest',
    ST_GeomFromText('POLYGON((
        -4.040739030549147 48.745741606853215,
-4.036245459319038 48.74828131566804,
-4.031850647896184 48.747923159378665,
-4.03234444693246 48.74938832805965,
-4.026912657533427 48.75020229220042,
-4.026468238400779 48.74919297470383,
-4.02523374081009 48.74919297470383,
-4.0203945102545875 48.740238464088826,
-4.022567226014201 48.73880559432817,
-4.026122579075386 48.740238464088826,
-4.02745583647333 48.74219231157387,
-4.03397398375217 48.74287614025014,
-4.034122123463052 48.741215397325284,
-4.035455380860997 48.741215397325284,
-4.040739030549147 48.745741606853215,
    ))', 4326)
);


INSERT INTO zone (name, geom)
VALUES (
    'Centre',
    ST_GeomFromText('POLYGON((
        -4.02523374081009 48.74919297470383,
-4.02360420399038 48.753295235684185,
-4.019851331314684 48.75345801690918,
-4.014518301722906 48.75231853725851,
-4.012641865385059 48.75323012304652,
-4.006666897046123 48.7528720020309,
-4.009234652034756 48.74313664396521,
-4.013036904614079 48.744536828300426,
-4.015604659602713 48.74349483436875,
-4.01402450268663 48.741541037517905,
-4.014326361007127 48.74136749047571,
-4.016436329474995 48.74286326394115,
-4.018177053460988 48.74218495358343,
-4.016937446986115 48.740984843601765,
-4.018678170972106 48.73959337586721,
-4.020366145746403 48.74016736097527,
-4.0203945102545875 48.740238464088826,
-4.02523374081009 48.74919297470383
    ))', 4326)
);


INSERT INTO zone (name, geom)
VALUES (
    'Est',
    ST_GeomFromText('POLYGON((
        -4.006666897046123 48.7528720020309,
-4.005870355067274 48.75026102618178,
-4.003629135498721 48.748652881336724,
-3.999937714914126 48.7464361648583,
-3.9950927255526962 48.74493656593118,
-3.9940050748797225 48.745523370840516,
-3.9910057957512173 48.745762437542936,
-3.9901158997460566 48.74515390188169,
-3.991851437957331 48.74306933218229,
-3.9887834656682424 48.7393151550319,
-3.993896752679784 48.73699956468981,
-3.9941353727403235 48.74093375678278,
-3.9980896480292487 48.74147327911715,
-4.000544025794788 48.740416709111294,
-4.00364608658179 48.741630638707136,
-4.005077806945022 48.740394228657095,
-4.007054944589484 48.740956236995665,
-4.008248044892178 48.74050663082754,
-4.008657107853101 48.740888796326814,
-4.007157210329716 48.74122599876611,
-4.00933887945464 48.7431367698581,
-4.009234652034756 48.74313664396521,
-4.006666897046123 48.7528720020309,
    ))', 4326)
);
