CREATE DATABASE financial_data;

CREATE EXTENSION IF NOT EXISTS pgcrypto;

DO $$
BEGIN
	IF NOT EXISTS (
		SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'financial_user'
	) THEN
		CREATE ROLE financial_user LOGIN PASSWORD 'securepassword';
	END IF;
END
$$;

GRANT ALL PRIVILEGES ON DATABASE financial_data TO financial_user;

CREATE TABLE IF NOT EXISTS organizations(
id SERIAL PRIMARY KEY,
orgname VARCHAR(50) UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS accounts(
id SERIAL PRIMARY KEY,
accountnum VARCHAR UNIQUE NOT NULL,
accountname VARCHAR UNIQUE NOT NULL,
orgid INT REFERENCES organizations(id) ON DELETE RESTRICT
);

CREATE TABLE IF NOT EXISTS vendors(
id SERIAL PRIMARY KEY,
vendorname VARCHAR NOT NULL,
orgid INT REFERENCES organizations(id) ON DELETE RESTRICT
);

-- how can I normalize this further? going to end up with lots of nulls in the transactions table, but not sure if it's worth it to have separate tables for each transaction type (e.g. fund transactions, card transactions, etc.)
CREATE TABLE IF NOT EXISTS transactions(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
description VARCHAR NOT NULL,
amount REAL NOT NULL,
accountid INT REFERENCES accounts(id) ON DELETE RESTRICT,
orgid INT REFERENCES organizations(id) ON DELETE RESTRICT,
vendorid INT REFERENCES vendors(id) ON DELETE RESTRICT
);

CREATE TABLE IF NOT EXISTS snapshots(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
accountid INT REFERENCES accounts(id) ON DELETE RESTRICT,
balance REAL NOT NULL,
orgid INT REFERENCES organizations(id) ON DELETE RESTRICT
);

CREATE TABLE IF NOT EXISTS syncs(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
accountid INT REFERENCES accounts(id) ON DELETE RESTRICT,
orgid INT REFERENCES organizations(id) ON DELETE RESTRICT
);

CREATE TABLE IF NOT EXISTS rawtransactions(
id SERIAL PRIMARY KEY,
data JSONB NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now(),
source VARCHAR(100),
--accountid INT REFERENCES accounts(id) ON DELETE RESTRICT,
--orgid INT REFERENCES organizations(id) ON DELETE RESTRICT,
raw_sha256 CHAR(64) UNIQUE,
processed BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS rawsnapshots(
id SERIAL PRIMARY KEY,
data JSONB NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now(),
source VARCHAR(100),
--accountid INT REFERENCES accounts(id) ON DELETE RESTRICT,
--orgid INT REFERENCES organizations(id) ON DELETE RESTRICT,
raw_sha256 CHAR(64) UNIQUE,
processed BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE OR REPLACE FUNCTION set_raw_sha256()
RETURNS TRIGGER AS $$
BEGIN
	IF NEW.raw_sha256 IS NULL THEN
		NEW.raw_sha256 := encode(digest(convert_to(NEW.data::text, 'UTF8'), 'sha256'), 'hex');
	END IF;
	RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS rawtransactions_set_sha256 ON rawtransactions;
CREATE TRIGGER rawtransactions_set_sha256
BEFORE INSERT OR UPDATE OF data ON rawtransactions
FOR EACH ROW
EXECUTE FUNCTION set_raw_sha256();

DROP TRIGGER IF EXISTS rawsnapshots_set_sha256 ON rawsnapshots;
CREATE TRIGGER rawsnapshots_set_sha256
BEFORE INSERT OR UPDATE OF data ON rawsnapshots
FOR EACH ROW
EXECUTE FUNCTION set_raw_sha256();

CREATE TABLE IF NOT EXISTS currency(
id SERIAL PRIMARY KEY,
currencycode CHAR(3) UNIQUE NOT NULL,
currencyname VARCHAR(50) NOT NULL
);

CREATE TABLE IF NOT EXISTS funds(
id SERIAL PRIMARY KEY,
fundid VARCHAR(100) UNIQUE NOT NULL,
name VARCHAR(100) NOT NULL,
symbol VARCHAR(50),
currencyid INT REFERENCES currency(id) ON DELETE RESTRICT
);

CREATE TABLE IF NOT EXISTS fund_positions(
id SERIAL PRIMARY KEY,
datetime DATE NOT NULL,
accountid INT REFERENCES accounts(id) ON DELETE RESTRICT,
orgid INT REFERENCES organizations(id) ON DELETE RESTRICT,
fundid INT REFERENCES funds(id) ON DELETE RESTRICT,
value REAL NOT NULL,
shares REAL NOT NULL
);

--assets, liabilities, equity, income, expenses
CREATE TABLE IF NOT EXISTS accounttypes(
id SERIAL PRIMARY KEY,
typename VARCHAR(100) NOT NULL,
includeinnetworth BOOLEAN NOT NULL
);

CREATE INDEX IF NOT EXISTS rawtransactions_data_idx ON rawtransactions USING GIN (data);
CREATE INDEX IF NOT EXISTS rawtransactions_received_at_idx ON rawtransactions (received_at);
--CREATE INDEX IF NOT EXISTS rawtransactions_account_org_idx ON rawtransactions (orgid, accountid);

CREATE INDEX IF NOT EXISTS accounts_orgid_idx ON accounts (orgid);
CREATE INDEX IF NOT EXISTS vendors_orgid_idx ON vendors (orgid);

CREATE INDEX IF NOT EXISTS transactions_accountid_idx ON transactions (accountid);
CREATE INDEX IF NOT EXISTS transactions_orgid_idx ON transactions (orgid);
CREATE INDEX IF NOT EXISTS transactions_vendorid_idx ON transactions (vendorid);
CREATE INDEX IF NOT EXISTS transactions_account_datetime_idx ON transactions (accountid, datetime);
CREATE INDEX IF NOT EXISTS transactions_org_datetime_idx ON transactions (orgid, datetime);

CREATE INDEX IF NOT EXISTS snapshots_accountid_idx ON snapshots (accountid);
CREATE INDEX IF NOT EXISTS snapshots_orgid_idx ON snapshots (orgid);
CREATE INDEX IF NOT EXISTS snapshots_account_datetime_idx ON snapshots (accountid, datetime);
CREATE INDEX IF NOT EXISTS snapshots_org_datetime_idx ON snapshots (orgid, datetime);

CREATE INDEX IF NOT EXISTS syncs_accountid_idx ON syncs (accountid);
CREATE INDEX IF NOT EXISTS syncs_orgid_idx ON syncs (orgid);
CREATE INDEX IF NOT EXISTS syncs_account_datetime_idx ON syncs (accountid, datetime);
CREATE INDEX IF NOT EXISTS syncs_org_datetime_idx ON syncs (orgid, datetime);

--CREATE INDEX IF NOT EXISTS rawtransactions_accountid_idx ON rawtransactions (accountid);
--CREATE INDEX IF NOT EXISTS rawtransactions_orgid_idx ON rawtransactions (orgid);

CREATE INDEX IF NOT EXISTS funds_currencyid_idx ON funds (currencyid);

CREATE INDEX IF NOT EXISTS fund_positions_accountid_idx ON fund_positions (accountid);
CREATE INDEX IF NOT EXISTS fund_positions_orgid_idx ON fund_positions (orgid);
CREATE INDEX IF NOT EXISTS fund_positions_fundid_idx ON fund_positions (fundid);
CREATE INDEX IF NOT EXISTS fund_positions_account_datetime_idx ON fund_positions (accountid, datetime);
CREATE INDEX IF NOT EXISTS fund_positions_org_datetime_idx ON fund_positions (orgid, datetime);

-- Seed data
INSERT INTO organizations (orgname)
VALUES
	('BNZ'),
	('Sharesies'),
	('Emoney')
ON CONFLICT (orgname) DO NOTHING;

INSERT INTO currency (currencycode, currencyname)
VALUES
	('NZD', 'New Zealand Dollar'),
	('USD', 'US Dollar')
ON CONFLICT (currencycode) DO NOTHING;

INSERT INTO accounttypes (typename, includeinnetworth)
SELECT 'assets', TRUE
WHERE NOT EXISTS (SELECT 1 FROM accounttypes WHERE typename = 'assets');
INSERT INTO accounttypes (typename, includeinnetworth)
SELECT 'liabilities', TRUE
WHERE NOT EXISTS (SELECT 1 FROM accounttypes WHERE typename = 'liabilities');
INSERT INTO accounttypes (typename, includeinnetworth)
SELECT 'equity', TRUE
WHERE NOT EXISTS (SELECT 1 FROM accounttypes WHERE typename = 'equity');
INSERT INTO accounttypes (typename, includeinnetworth)
SELECT 'income', FALSE
WHERE NOT EXISTS (SELECT 1 FROM accounttypes WHERE typename = 'income');
INSERT INTO accounttypes (typename, includeinnetworth)
SELECT 'expenses', FALSE
WHERE NOT EXISTS (SELECT 1 FROM accounttypes WHERE typename = 'expenses');

INSERT INTO accounts (accountnum, accountname, orgid)
VALUES
	('02-1244-0271514-00', 'Income Account', (SELECT id FROM organizations WHERE orgname = 'BNZ')),
	('02-1244-0271514-04', 'Rent', (SELECT id FROM organizations WHERE orgname = 'BNZ')),
	('12-3497-0007278-01', 'Jethro''s Investments', (SELECT id FROM organizations WHERE orgname = 'Sharesies')),
	('acc_cmoc72wzs000502kz7lnu345l', 'Jethro''s KiwiSaver', (SELECT id FROM organizations WHERE orgname = 'Sharesies')),
	('acc_cmoc72x21000a02kz9j3ve87q', 'Jethro''s Rainy day fund', (SELECT id FROM organizations WHERE orgname = 'Sharesies'))
ON CONFLICT DO NOTHING;

INSERT INTO vendors (vendorname, orgid)
SELECT 'PAK''nSAVE', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'PAK''nSAVE' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'PAK''nSAVE Fuel', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'PAK''nSAVE Fuel' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'BP', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'BP' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Mobil', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Mobil' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Caltex', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Caltex' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Woolworths', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Woolworths' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'New World', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'New World' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Super Liquor', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Super Liquor' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Lotto', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Lotto' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Auckland Transport', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Auckland Transport' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Finance Now', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Finance Now' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Gem Finance', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Gem Finance' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Sharesies', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Sharesies' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Feijoa', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Feijoa' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Spark', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Spark' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Asteron Life', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Asteron Life' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'AA Insurance', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'AA Insurance' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Mercury', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Mercury' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'YouTube', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'YouTube' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Netflix', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Netflix' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'The Warehouse', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'The Warehouse' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Pit Stop', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Pit Stop' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Lucky Star Bakery', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Lucky Star Bakery' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'Google', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'Google' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));
INSERT INTO vendors (vendorname, orgid)
SELECT 'IT Live Limited', (SELECT id FROM organizations WHERE orgname = 'BNZ')
WHERE NOT EXISTS (SELECT 1 FROM vendors WHERE vendorname = 'IT Live Limited' AND orgid = (SELECT id FROM organizations WHERE orgname = 'BNZ'));

INSERT INTO funds (fundid, name, symbol, currencyid)
VALUES
    ('91232ea0-548b-47a1-93a0-495cbf40fbd9', 'Pie Global Growth 2 Fund', 'SKS.FUND.PGG2', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('d2d85cc8-1884-4512-ab2e-1a7110498247', 'Smartshares Growth Fund', 'SKS.FUND.SLGF', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('b627459d-e626-4afa-9a6f-b980e47120da', 'Sharesies US500 Fund', 'SKS.FUND.SVOO', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('4da3e348-95da-4872-9ac9-00978ab263af', 'VanEck Semiconductor ETF', 'SKS.NASDAQ.SMH', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('fdcfd374-403f-412d-9a38-1a497c3ec36b', 'Smartshares Global Equities Responsible Fund', 'SKS.NZX.ESG', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('8cd3115a-831b-4a5a-9cdd-a5d523c6814f', 'Smart Asia Pacific ETF', 'APA', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('84fb0b94-e7dd-4a48-996d-fd261b781c11', 'Smart Emerging Markets ETF', 'EMF', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('68d32bbf-13e7-46e3-b735-1a7bb91ad481', 'Smart Europe ETF', 'EUF', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('77866efa-d81e-4f71-beaf-371bb210ac8c', 'Smart NZ Top 50 ETF', 'FNZ', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('67a17798-c341-4af3-a06e-5a621d342adb', 'Smart Australian Top 20 ETF', 'OZY', (SELECT id FROM currency WHERE currencycode = 'NZD')),
    ('af87fb44-ebf6-4239-ba08-ae1cc9a6461c', 'Smart US 500 ETF', 'USF', (SELECT id FROM currency WHERE currencycode = 'NZD'))
ON CONFLICT (fundid) DO NOTHING;

INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '91232ea0-548b-47a1-93a0-495cbf40fbd9'),
    2818.92,
    1812.93
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '91232ea0-548b-47a1-93a0-495cbf40fbd9')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = 'd2d85cc8-1884-4512-ab2e-1a7110498247'),
    2792.47,
    1940.03
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = 'd2d85cc8-1884-4512-ab2e-1a7110498247')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = 'b627459d-e626-4afa-9a6f-b980e47120da'),
    1665.11,
    1254.6
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = 'b627459d-e626-4afa-9a6f-b980e47120da')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '4da3e348-95da-4872-9ac9-00978ab263af'),
    304.41,
    153.12
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '4da3e348-95da-4872-9ac9-00978ab263af')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = 'fdcfd374-403f-412d-9a38-1a497c3ec36b'),
    465.52,
    283.3
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = 'fdcfd374-403f-412d-9a38-1a497c3ec36b')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s KiwiSaver')
      AND datetime = '2026-05-18'
);

INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '8cd3115a-831b-4a5a-9cdd-a5d523c6814f'),
    19.89,
    5.26
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '8cd3115a-831b-4a5a-9cdd-a5d523c6814f')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '84fb0b94-e7dd-4a48-996d-fd261b781c11'),
    14.6,
    7.33
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '84fb0b94-e7dd-4a48-996d-fd261b781c11')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '68d32bbf-13e7-46e3-b735-1a7bb91ad481'),
    19.28,
    6.6
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '68d32bbf-13e7-46e3-b735-1a7bb91ad481')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '77866efa-d81e-4f71-beaf-371bb210ac8c'),
    3.9,
    1.29
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '77866efa-d81e-4f71-beaf-371bb210ac8c')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = '67a17798-c341-4af3-a06e-5a621d342adb'),
    5.79,
    0.95
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = '67a17798-c341-4af3-a06e-5a621d342adb')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments')
      AND datetime = '2026-05-18'
);
INSERT INTO fund_positions (datetime, accountid, orgid, fundid, value, shares)
SELECT
    '2026-05-18',
    (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments'),
    (SELECT id FROM organizations WHERE orgname = 'Sharesies'),
    (SELECT id FROM funds WHERE fundid = 'af87fb44-ebf6-4239-ba08-ae1cc9a6461c'),
    35.09,
    1.58
WHERE NOT EXISTS (
    SELECT 1 FROM fund_positions
    WHERE fundid = (SELECT id FROM funds WHERE fundid = 'af87fb44-ebf6-4239-ba08-ae1cc9a6461c')
      AND accountid = (SELECT id FROM accounts WHERE accountname = 'Jethro''s Investments')
      AND datetime = '2026-05-18'
);