-- Payments for systems outside Aggie (for example the allBETs main app): each system has an API key, creates a Stripe Checkout
-- through Aggie, and is told the result by a signed callback. Keys and the callback secret are stored hashed / encrypted.
-- Idempotent: safe to re-run.
CREATE TABLE IF NOT EXISTS external_payment_client (
    id                        INT          NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name                      VARCHAR(80)  NOT NULL,
    key_id                    VARCHAR(16)  NOT NULL,
    key_hash                  CHAR(64)     NOT NULL,
    callback_url              VARCHAR(500) NULL,
    callback_secret_encrypted TEXT         NULL,
    allowed_return_hosts      VARCHAR(500) NOT NULL DEFAULT '',
    max_amount_cents          INT          NOT NULL DEFAULT 100000,
    rate_per_minute           INT          NOT NULL DEFAULT 60,
    live_allowed              TINYINT(1)   NOT NULL DEFAULT 0,
    active                    TINYINT(1)   NOT NULL DEFAULT 1,
    created_at                DATETIME(6)  NOT NULL,
    revoked_at                DATETIME(6)  NULL,
    last_used_at              DATETIME(6)  NULL,
    UNIQUE KEY uq_epc_key_id (key_id)
);

CREATE TABLE IF NOT EXISTS external_payment (
    id                       CHAR(32)      NOT NULL PRIMARY KEY,
    client_id                INT           NOT NULL,
    external_reference       VARCHAR(120)  NOT NULL,
    idempotency_key          VARCHAR(120)  NULL,
    amount_cents             BIGINT        NOT NULL,
    currency                 CHAR(3)       NOT NULL,
    description              VARCHAR(250)  NULL,
    customer_email           VARCHAR(190)  NULL,
    metadata_json            TEXT          NULL,
    status                   VARCHAR(24)   NOT NULL,
    livemode                 TINYINT(1)    NOT NULL,
    stripe_session_id        VARCHAR(120)  NULL,
    stripe_payment_intent_id VARCHAR(120)  NULL,
    checkout_url             VARCHAR(1000) NULL,
    success_url              VARCHAR(500)  NOT NULL,
    cancel_url               VARCHAR(500)  NOT NULL,
    refunded_cents           BIGINT        NOT NULL DEFAULT 0,
    paid_at                  DATETIME(6)   NULL,
    created_at               DATETIME(6)   NOT NULL,
    updated_at               DATETIME(6)   NOT NULL,
    UNIQUE KEY uq_ep_session (stripe_session_id),
    UNIQUE KEY uq_ep_idem (client_id, idempotency_key),
    KEY ix_ep_ref (client_id, external_reference),
    KEY ix_ep_pi (stripe_payment_intent_id),
    KEY ix_ep_created (created_at),
    CONSTRAINT fk_ep_client FOREIGN KEY (client_id) REFERENCES external_payment_client (id)
);

CREATE TABLE IF NOT EXISTS external_payment_event (
    id              CHAR(32)     NOT NULL PRIMARY KEY,
    payment_id      CHAR(32)     NOT NULL,
    client_id       INT          NOT NULL,
    event_type      VARCHAR(40)  NOT NULL,
    payload_json    TEXT         NOT NULL,
    status          VARCHAR(16)  NOT NULL,
    attempts        INT          NOT NULL DEFAULT 0,
    next_attempt_at DATETIME(6)  NULL,
    last_error      VARCHAR(300) NULL,
    created_at      DATETIME(6)  NOT NULL,
    delivered_at    DATETIME(6)  NULL,
    KEY ix_epe_due (status, next_attempt_at),
    KEY ix_epe_payment (payment_id)
);
