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_MakePoint(-4.01570310149135423, 48.74486669121691307)),
(2,'Salle des fêtes', ST_MakePoint(-4.01523914066345888, 48.74492186050463971)),
(3,'Crêperie', ST_MakePoint(-4.01485656953699177, 48.74436722362130325)),
(4,'Le port', ST_MakePoint(-4.01462263063635749, 48.74422250405495305)),
(5,'Marie', ST_MakePoint(-4.01292331831630555, 48.74446490379569497)),
(6,'La Poste', ST_MakePoint(-4.01297842611315758, 48.74533129595438652)),
(7, 'École primaire', ST_MakePoint(-4.01383673054336843, 48.74532080203461248)),
(8, 'Pharmacie', ST_MakePoint(-4.015872431573694, 48.74436631101314532)),
(9, 'Bibliothèque', ST_MakePoint(-4.01698025510527579, 48.74409891493710489)),
(10, 'Librairie', ST_MakePoint(-4.01666532624374639, 48.74359360390645435)),
(11, 'Poissonerie', ST_MakePoint(-4.01607549918972317, 48.74310777723044197)),
(12, 'Sémaphore', ST_MakePoint(-4.01266253446298737, 48.74636630923999547)),
(13, 'Boucherie', ST_MakePoint(-4.01149526816046631, 48.74529523306026846)),
(14, 'Bergerie', ST_MakePoint(-4.01131293956044654, 48.74748052979568769)),
(15, 'Pré Bufflon', ST_MakePoint(-4.00925737910312652, 48.74722639293891291)),
(16, 'Coiffeur', ST_MakePoint(-4.01695656484633279, 48.74578944178636419)),
(17, 'Dentiste', ST_MakePoint(-4.01820013254909014, 48.74479792341951878)),
(18, 'Barbier', ST_MakePoint(-4.01758793906357337, 48.74392848649364396)),
(19, 'Épicerie', ST_MakePoint(-4.01774795538932761, 48.74379635837768632)),
(20, 'Bijouterie', ST_MakePoint(-4.01806242225559451, 48.74378993547429673)),
(21, 'Écurie', ST_MakePoint(-4.02147447042353079, 48.74379889900189511)),
(22, 'Gendarmerie', ST_MakePoint(-4.01760408448711459, 48.74301074961061886)),
(23, 'Église', ST_MakePoint(-4.00974023695237225, 48.74437056754450026)),
(24, 'Collège', ST_MakePoint(-4.01000671584753565, 48.74422424537268483)),
(25, 'Hôtel', ST_MakePoint(-4.00708151936692136, 48.74137526128594544)),
(26, 'Cabinet médical', ST_MakePoint(-4.006265083289092, 48.74170845222292)),
(27, 'Pompier', ST_MakePoint(-4.006068894377577, 48.74217891432291)),
(28, 'Cimetière', ST_MakePoint(-4.00684574426586515, 48.74322789648684306)),
(29, 'Auberge de jeunesse', ST_MakePoint(-4.00338210183722332, 48.74299767351865142)),
(30, 'Déchetterie', ST_MakePoint(-4.003359497656666, 48.74446857837965)),
(31, 'Manoir', ST_MakePoint(-4.02409175802312014, 48.74299671587339589)),
(32, 'Vélociste', ST_MakePoint(-4.01912934818258982, 48.74268049223412191)),
(33, 'Gare maritime', ST_MakePoint(-4.01819217674779683, 48.74216778273091677)),
(34, '',ST_MakePoint (-4.01837996387577512, 48.74486077568911924)),
(35, '',ST_MakePoint (-4.01847183812000619, 48.74490975460548015)),
(36, '',ST_MakePoint (-4.01974946418712165, 48.74461924247759725)),
(37, '',ST_MakePoint (-4.01674202846954209, 48.74639773953580146)),
(38, 'Camping',ST_MakePoint (-4.02676345916091183, 48.74370038197837118)),
(39, 'Stade',ST_MakePoint (-4.03184643082272931, 48.7439405503143206)),
(40, 'Fort du Chenal',ST_MakePoint (-4.03504505063330576, 48.74215424186476753)),
(41, 'Menhir',ST_MakePoint (-4.0283457106078151, 48.74819693416072397)),
(42, 'Marre aux canards',ST_MakePoint (-4.02015407493021737, 48.74906065795587295)),
(43, '',ST_MakePoint (-4.01479791960592536, 48.74702453562415627)),
(44, '',ST_MakePoint (-4.01639547750786363, 48.74542363596422234)),
(45, '',ST_MakePoint (-4.00968237459288446, 48.74407907017207719)),
(46, '',ST_MakePoint (-4.00454687117624619, 48.74271289799514051)),
(47, '',ST_MakePoint (-4.00195445763924607, 48.74307169008736196)),
(48, 'Jardin botanique',ST_MakePoint (-3.99017657885386567, 48.74059175123832688)),
(49, '',ST_MakePoint (-4.00095090405674103, 48.74113512424731454)),
(50, '',ST_MakePoint (-4.01034636235266895, 48.75078911007821603)),
(51, 'Lande',ST_MakePoint (-4.00751796384676151, 48.74674513822930066)),
(52, '',ST_MakePoint(-4.00596435298959364, 48.74779732620555706)),
(53, '',ST_MakePoint(-3.99173705354423269, 48.74086265902959525)),
(54, '',ST_MakePoint(-4.00201087494934615, 48.74205886544121569)),
(55, '',ST_MakePoint(-4.01402240501941954, 48.74579808339848341)),
(56, '',ST_MakePoint(-4.01752262997271892, 48.74286683161378164)),
(57, '',ST_MakePoint(-4.01906847777902865, 48.74357188947293906)),
(58, '',ST_MakePoint(-4.0397601095149529, 48.74577305806607797)),
(59, '',ST_MakePoint(-4.01947006578626098, 48.74048898114416062)),
(60, '',ST_MakePoint(-4.02028886503836169, 48.74713526405361819)),
(61, '',ST_MakePoint(-4.01938373920015213, 48.75166818981730898)),
(62, '',ST_MakePoint(-4.01924319927781237, 48.74797778970009432)),
(63, '',ST_MakePoint(-4.01938373920015213, 48.75166818981730898)),
(64, '',ST_MakePoint(-4.02746179346384281, 48.74699628476702884)),
(65, '',ST_MakePoint(-4.02286843896043855, 48.74610257019231341)),
(66, '',ST_MakePoint(-4.03027566309002072, 48.74773403814539563)),
(67, '',ST_MakePoint(-4.0249746786814784, 48.74905338475223004));

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.04006335183021825 48.74527596273622976, -4.04018335215793201 48.74608699011085378, -4.03831415412299233 48.74688911543833569,
    -4.03751306925087672 48.74790642670545537, -4.0362470851437342 48.74808419558064543,
    -4.03551790379234276 48.74758836709975185, -4.0340586080325771 48.7467904270295449, -4.03277208026601119 48.74710004638985339,
    -4.03200871853877363 48.74747567812632809, -4.03185064789618419 48.74792315937866505, -4.03215683350077381 48.74899320175573791,
    -4.03205429237323543 48.74922608498921761, -4.03166691478030881 48.74923359733361394, -4.03114281568399679 48.74904578838667391,
    -4.0308807661358399 48.7487828546816786, -4.03004941206934308 48.7486833505726338, -4.02935404268136477 48.74888051593271143,
    -4.0289109984007716 48.74940133740160064, -4.02845758964104927 48.74938631330267924, -4.02691265753342709 48.75020229220042012,
    -4.02646823840077861 48.74919297470383128, -4.02523374081008978 48.74919297470383128, -4.02039451025458749 48.74023846408882576,
    -4.02114483756962748 48.73943626377466387, -4.02225674016294121 48.738947406438065, -4.02397754179544886 48.73962831357041381,
    -4.02538065697272529 48.73975052669476327, -4.0258571866555739 48.74032667027861976, -4.02586697924350378 48.74143763666283746,
    -4.02671325838233951 48.74213486863882139, -4.02861212622735732 48.7421740000776893, -4.02938354128939569 48.74270227152060642,
    -4.03012480508505977 48.74181692648819109, -4.03039185283764922 48.74200016883640529, -4.0305102449984167 48.7420880017881899,
    -4.03059664234290871 48.74219904469969578, -4.03077047712211556 48.74231913839474117, -4.03082014420188894 48.74250473717721377,
    -4.03105192390749778 48.74257570123644712, -4.0309774232878377 48.74267395899919819, -4.03107675744738447 48.74277767531815897,
    -4.03200451380450087 48.74306964535660569, -4.03283149491465931 48.74322373353422222, -4.03397398375216998 48.74287614025013937,
    -4.03418472124766669 48.74247717589914686, -4.03415212522576105 48.74214384376339382, -4.03399951905647836 48.74200450145770702,
    -4.03409930001331674 48.74174903956061655, -4.03396769950894019 48.74136087588372135, -4.03453195602962911 48.74114619996144881,
    -4.03524812776742436 48.74134656418410572, -4.03571804574113369 48.74237943963194652, -4.03759758965444249 48.7438040319799839,
    -4.0375798709083055 48.74410621576857494, -4.03824105738751626 48.74484528633763603, -4.04006335183021825 48.74527596273622976))', 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)
);
