-- Vector Zulu KoreID Adapter Schema
-- Extends the existing vz_oracle database on Charlie

-- KoreID sequence table (per spec)
CREATE TABLE IF NOT EXISTS kore_sequence (
    mmyy        CHAR(4)     NOT NULL,
    ct          CHAR(2)     NOT NULL,
    t           CHAR(1)     NOT NULL,
    net         CHAR(5)     NOT NULL,
    ccc         CHAR(3)     NOT NULL,
    parent_seq  INT         NOT NULL DEFAULT 0,
    current_seq INT         NOT NULL DEFAULT 0,
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (mmyy, ct, t, net, ccc)
);

-- KoreID GUID registry (per spec)
CREATE TABLE IF NOT EXISTS kore_guid_registry (
    guid        UUID        PRIMARY KEY,
    koreid      TEXT        NOT NULL UNIQUE,
    event_type  TEXT,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Outbound events posted to KoreChain
CREATE TABLE IF NOT EXISTS kore_outbound_events (
    id              BIGSERIAL   PRIMARY KEY,
    koreid          TEXT        NOT NULL UNIQUE,
    guid            UUID,
    event_type      TEXT,
    currency        TEXT,
    token_symbol    TEXT,
    tx_hash         TEXT,
    amount          NUMERIC(28,8),
    status          TEXT        NOT NULL DEFAULT 'PENDING',
    response_koreid TEXT,
    posted_at       TIMESTAMPTZ,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_kore_outbound_status ON kore_outbound_events(status);
CREATE INDEX IF NOT EXISTS idx_kore_outbound_tx ON kore_outbound_events(tx_hash);
CREATE INDEX IF NOT EXISTS idx_kore_outbound_posted ON kore_outbound_events(posted_at DESC);

-- Lookup tables (populate with values from Bryan's spec)
CREATE TABLE IF NOT EXISTS kore_id_type (
    code    CHAR(2) PRIMARY KEY,
    name    TEXT    NOT NULL
);

CREATE TABLE IF NOT EXISTS kore_treasury (
    code    CHAR(1) PRIMARY KEY,
    name    TEXT    NOT NULL
);

CREATE TABLE IF NOT EXISTS kore_network (
    code    CHAR(5) PRIMARY KEY,
    name    TEXT    NOT NULL
);

CREATE TABLE IF NOT EXISTS kore_network_current (
    code    CHAR(8) PRIMARY KEY,
    name    TEXT    NOT NULL
);

CREATE TABLE IF NOT EXISTS kore_currency (
    code    CHAR(3) PRIMARY KEY,
    name    TEXT    NOT NULL
);

-- Seed placeholder values (replace with Bryan's actual codes)
INSERT INTO kore_id_type (code, name) VALUES
    ('a1', 'MINT'),
    ('a2', 'BURN'),
    ('a3', 'TRANSFER'),
    ('a4', 'RESERVE_UPDATE'),
    ('a5', 'SWAP'),
    ('a6', 'ONRAMP'),
    ('a7', 'OFFRAMP'),
    ('b1', 'EXTERNAL_RESPONSE'),
    ('b2', 'VZ_CHILD')
ON CONFLICT DO NOTHING;

INSERT INTO kore_treasury (code, name) VALUES
    ('1', 'VECTOR_ZULU'),
    ('2', 'SOVEREIGN_GLOBAL'),
    ('3', 'KORE_COLLECTIVE'),
    ('4', 'MERIDIAN')
ON CONFLICT DO NOTHING;

INSERT INTO kore_network (code, name) VALUES
    ('f4e50', 'KORECHAIN'),
    ('0a1b0', 'ETHEREUM'),
    ('03e80', 'AMITIS'),
    ('00154', 'TRON')
ON CONFLICT DO NOTHING;

INSERT INTO kore_network_current (code, name) VALUES
    ('0a1b03e8', 'ETHEREUM_MAINNET'),
    ('03e80015', 'AMITIS_MAINNET'),
    ('f4e5b231', 'KORECHAIN_MAINNET'),
    ('001540ab', 'TRON_MAINNET')
ON CONFLICT DO NOTHING;

INSERT INTO kore_currency (code, name) VALUES
    ('001', 'USD'),
    ('002', 'GBP'),
    ('003', 'EUR'),
    ('004', 'CHF'),
    ('005', 'JPY'),
    ('006', 'SGD'),
    ('010', 'BTC'),
    ('011', 'ETH'),
    ('012', 'USDT'),
    ('013', 'USDC'),
    ('020', 'GPSC'),
    ('030', 'AMTS')
ON CONFLICT DO NOTHING;
