BEGIN;

CREATE TEMP TABLE tmp_links_import (
    name TEXT NOT NULL,
    url TEXT NOT NULL,
    category_name TEXT NOT NULL
);

INSERT INTO tmp_links_import (name, url, category_name) VALUES
('Guacamole','https://rdp-dash.bmcnetworks.dk/guacamole/#/','Interne systemer (Admin)'),
('MailChimp','https://login.mailchimp.com','Interne systemer (Admin)'),
('Plesk','https://isp.bmcnetworks.dk:8443/login_up.php','Interne systemer (Admin)'),
('Speedtest (admin)','http://speedtest.bmcnetworks.dk/results/stats.php','Interne systemer (Admin)'),
('Uisp','https://uisp.bmcnetworks.dk','Interne systemer (Admin)'),
('s3 Admin','http://172.16.30.13:9001','Interne systemer (Admin)'),
('Mailarkiv admin','https://arkiv.bmcmailarkiv.dk/','Interne systemer (Admin)'),
('Ducky Mail admin','https://mailadmin.bmcdenmark.com','BMC Mail server'),
('Webmail','https://mail.bmcdenmark.com','BMC Mail server'),
('BMC Anydesk','https://get.anydesk.com/0RRDdvHP/BMCsupport.exe','Public links'),
('Ninite Std software','https://ninite.com/.net4.8-.net5-.net6-.net7-.netx5-.netx6-.netx7-adoptjava8-adoptjavax11-adoptjavax17-adoptjavax8-firefox-vlc/ninite.exe','Public links'),
('Norva24 Nextcloud','https://norva24tv.acdu.dk/login','Norva24'),
('SFTP Nextcloud liste','https://bmcdenmark.sharepoint.com','Norva24'),
('Maskinsikkerhed Nextcloud','https://ms.docs.bmcnetworks.dk/login?redirect_url=/apps/dashboard/','Maskinsikkerhed'),
('Android Kiosk','https://downloads.pronestor.com','PFA'),
('Anydesk PFA','https://my.anydesk.com','PFA'),
('Clickshare barco','http://xms.cloud.barco.com','PFA'),
('Clickshare guide','https://theunion.dk','PFA'),
('Meraki PFA','https://n717.meraki.com','PFA'),
('The Union Planner','https://the-union.pronestor.com','PFA'),
('care.oniadea','https://care.oniadea.com','PFA'),
('BMC Nextcloud','https://nc.bmcnetworks.dk','Interne systemer'),
('BMC Sharepoint','https://bmcdenmark.sharepoint.com','Interne systemer'),
('2fAuth','https://2f.bmcnetworks.dk/','Interne systemer'),
('Seafile','https://docs.bmcnetworks.dk','Interne systemer'),
('Vaultwarden','https://bw.bmcnetworks.dk/#/','Interne systemer'),
('BMC mail arkiv','https://bmcnetworks.bmcmailarkiv.dk','Interne systemer'),
('Uptime Kuma','https://kuma.bmcnetworks.dk/dashboard','Interne systemer'),
('uISP OLD','https://unms-pri.bmcnetworks.dk','Interne systemer'),
('Smokeping','https://smokeping.bmcnetworks.dk','Interne systemer'),
('Teknik WIKI','https://wiki.bmcnetworks.dk','Interne systemer'),
('Unifi','https://unifi.bmcnetworks.dk:8443','Interne systemer'),
('Unifi old','https://unifi-sdn.bmcnetworks.dk:8443/','Interne systemer'),
('BMC Office install','http://software.bmcnetworks.dk','Externe systemer'),
('BMC Speakonline','https://phone-wizard.com','Externe systemer'),
('Cloudfactory Portal','http://portal.cloudfactory.dk','Externe systemer'),
('Eset MSP','https://msp.eset.com','Externe systemer'),
('Minside Telefoni','https://minside.bmcnetworks.dk','Externe systemer'),
('My Globalconnect','https://my.globalconnect.dk','Externe systemer'),
('SentinelOne','https://euce1-teamblue.sentinelone.net','Externe systemer'),
('Simply CRM portal','https://tickets.simply-crm.com','Externe systemer'),
('Globalconnect','https://nn.globalconnect.dk','Externe systemer'),
('Simply CRM','https://bmcnetworks.simply-crm.dk','Externe systemer'),
('Portal admin','https://mit.bmcnetworks.dk','Externe systemer'),
('Jira','https://bmcdenmark.atlassian.net','Externe systemer'),
('Avast hub','http://businesshub.avast.com','Externe systemer'),
('Booking mødelokale','https://3048.torvekoekken.dk','Externe systemer'),
('CP SMS','https://www.cpsms.dk','Externe systemer'),
('Curanet','https://reseller.curanet.dk','Externe systemer'),
('Mit GC','https://nn.globalconnect.dk','Externe systemer'),
('Shipmondo','https://app.shipmondo.com','Externe systemer'),
('e-conomic','https://secure.e-conomic.com','Externe systemer'),
('Provision Yealink','https://dm.yealink.com','Externe systemer'),
('Carl-Ras','https://www.carl-ras.dk','Grosister'),
('Deltaco','https://www.deltaco.dk','Grosister'),
('Serverschmiede','https://www.serverschmiede.com','Grosister'),
('DCS','http://dcs.dk','Grosister'),
('Also','https://www.also.com','Grosister'),
('EET','https://www.eetgroup.com','Grosister'),
('Farnell','https://dk.farnell.com','Grosister'),
('Lemvigh-Müller','https://www.lemu.dk','Grosister'),
('Lan-Com','https://lan-com.dk','Grosister'),
('Clerk','https://my.clerk.io','ITvarer.dk'),
('OnPay Manager','https://manage.onpay.io','ITvarer.dk'),
('Stedger','https://dashboard.stedger.com','ITvarer.dk'),
('Webshop admin','https://itvarer.bmcnetworks.dk','ITvarer.dk'),
('3 Erhverv','https://www.3.dk','Tele sites'),
('ICH','http://ich01.supertel.dk','Tele sites'),
('Mastedatabasen','https://www.mastedatabasen.dk','Tele sites'),
('BMCnas','https://172.16.20.28/cgi-bin/','Hardware'),
('HP Officejet','http://172.16.20.187','Hardware'),
('TrueNAS','https://172.16.30.9','Hardware'),
('Flame search tips','https://github.com/pawelmalak/flame/wiki/Search-bar','Diverse'),
('Mentech','http://mentech.dk','Diverse'),
('BMCnet.dk','http://bmcnet.dk','bmcnet.dk'),
('bmcnet admin','https://reseller.curanet.dk','bmcnet.dk'),
('Power DNS','http://172.16.20.25','Old links'),
('SugarCRM','http://sugar.intranet.bmc','Old links'),
('Teknik intra','http://teknik.intranet.bmc','Old links');

INSERT INTO link_categories (name, icon, sort_order)
SELECT DISTINCT category_name, 'bi-link-45deg', 100
FROM tmp_links_import
ON CONFLICT (name) DO NOTHING;

INSERT INTO links (name, type, url, environment, is_critical, is_favorite)
SELECT t.name, 'http', t.url, 'prod', FALSE, FALSE
FROM tmp_links_import t
WHERE NOT EXISTS (
    SELECT 1
    FROM links l
    WHERE l.deleted_at IS NULL
      AND l.name = t.name
      AND l.url = t.url
);

INSERT INTO link_category_map (link_id, category_id)
SELECT l.id, c.id
FROM tmp_links_import t
JOIN link_categories c ON c.name = t.category_name
JOIN LATERAL (
    SELECT id
    FROM links
    WHERE deleted_at IS NULL
      AND name = t.name
      AND url = t.url
    ORDER BY id ASC
    LIMIT 1
) l ON TRUE
ON CONFLICT DO NOTHING;

COMMIT;

SELECT
    (SELECT COUNT(*) FROM tmp_links_import) AS source_rows,
    (SELECT COUNT(*) FROM link_categories WHERE name IN (SELECT DISTINCT category_name FROM tmp_links_import)) AS matched_categories,
    (SELECT COUNT(*) FROM links WHERE deleted_at IS NULL AND (name, url) IN (SELECT name, url FROM tmp_links_import)) AS matched_links,
    (SELECT COUNT(*)
     FROM link_category_map lcm
     JOIN links l ON l.id = lcm.link_id
     JOIN link_categories c ON c.id = lcm.category_id
     WHERE l.deleted_at IS NULL
       AND (l.name, l.url) IN (SELECT name, url FROM tmp_links_import)
       AND c.name IN (SELECT DISTINCT category_name FROM tmp_links_import)
    ) AS matched_mappings;
