PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS histories (
       id INTEGER PRIMARY KEY,       
       name_id INTEGER NOT NULL,
       compra REAL NOT NULL,
       venta REAL NOT NULL,
       parser TEXT NOT NULL,
       parsed INTEGER NOT NULL,
       FOREIGN KEY(name_id) REFERENCES institutions(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS institutions (
       id INTEGER PRIMARY KEY,
       name TEXT NOT NULL UNIQUE,
       short_name TEXT NOT NULL UNIQUE,
       created INTEGER NOT NULL
);

-- SAVE the last record with the last price for an inst
CREATE TABLE IF NOT EXISTS actual_price (
       id INTEGER PRIMARY KEY,
       inst_id INTEGER,
       hist_id INTEGER,
       FOREIGN KEY(inst_id) REFERENCES institutions(id) ON DELETE CASCADE,
       FOREIGN KEY(hist_id) REFERENCES histories(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS subscriptions (
       id INTEGER PRIMARY KEY,
       tgb_id INTEGER NOT NULL,
       inst_id INTEGER NOT NULL,
       created INTEGER NOT NULL,
       FOREIGN KEY(inst_id) REFERENCES institutions(id) ON DELETE CASCADE,
       UNIQUE(tgb_id, inst_id)
);

CREATE TRIGGER IF NOT EXISTS ai_history
       AFTER INSERT ON histories
       	     BEGIN
		INSERT OR REPLACE INTO actual_price(id, inst_id, hist_id)
		       VALUES ((SELECT ap.id from actual_price as ap WHERE ap.inst_id = new.name_id), new.name_id, new.id);
		       END;
		       

insert or ignore into institutions (name,short_name,created) values
       	 ('banco central dominicano','bcd','2024-02-29 23:33:23'),
       	 ('banco popular','bpd','2024-02-29 23:35:25'),
	 ('banco hipotecario dominicano','bhd','2024-02-29 23:38:21'),
	 ('banreservas','brd','2024-02-29 23:23:15'),
	 ('asociacion peravia de ahorros y prestamos','apeap','2024-02-29 23:23:27'),
	 ('asociacion popular de ahorros y prestamos','apap','2024-02-29 23:23:28'), 	   ('asociacion cibao de ahorros y prestamos','acap','2024-02-29 23:23:22'),		     ('asociacion la nacional de ahorros y prestamos','alnap','2024-02-29 23:23:26');
insert or ignore into institutions (name,short_name,created) values
	 ('banco promerica','bpr','2024-02-29 23:23:22'),
	 ('banco bdi','bbd','2024-02-29 23:23:23'),
	 ('banco caribe','bca','2024-02-29 23:23:23'),
	 ('banco santa cruz','bsc','2024-02-29 23:23:25'),
 	 ('banco vimenca','bvi','2024-03-22 16:14:42'),
	 ('scotiabank cambio online','scline','2024-03-22 16:17:09'),
	 ('scotiabank','scotiabank','2024-03-22 16:17:10');
insert or ignore into institutions (name,short_name,created) values
	 ('bonanza banco','bba','2024-02-29 23:23:26'),
	 ('banco atlantico','bat','2024-02-29 23:23:27'),
 	 ('banco lopez de haro','blh','2024-02-29 23:23:28'),
	 ('banco ademi','bad','2024-02-29 23:23:29'),
	 ('banco lafise','bla','2024-02-29 23:23:31'),
	 ('banesco','banesco','2024-03-22 16:11:13'),
 	 ('banco activo dominicana','bacd','2024-02-29 23:23:13');
insert or ignore into institutions (name,short_name,created) values
 	 ('girosol','girosol','2024-02-29 23:23:14'),
 	 ('moneycorps','moneycorps','2024-02-29 23:23:17'),
	 ('imbert y balbuena','imb','2024-02-29 23:23:18'),
	 ('rm','rm','2024-02-29 23:23:18'),
	 ('motor credito','mcr','2024-02-29 23:23:19'),
	 ('cambio extranjero','cex','2024-02-29 23:23:20'),
	 ('capla','capla','2024-02-29 23:23:20'),
 	 ('taveras','taveras','2024-02-29 23:23:24'),
	 ('gamelin','gamelin','2024-02-29 23:23:24'),
	 ('sct','sct','2024-02-29 23:23:25');
