-- M8 计费与SaaS

CREATE TABLE tenant_balances (
    id              BIGINT          NOT NULL PRIMARY KEY,
    tenant_id       BIGINT          NOT NULL UNIQUE,
    balance         DECIMAL(18,4)   NOT NULL DEFAULT 0.0000,
    total_recharged DECIMAL(18,4)   NOT NULL DEFAULT 0.0000,
    total_spent     DECIMAL(18,4)   NOT NULL DEFAULT 0.0000,
    version         INT             NOT NULL DEFAULT 0,
    updated_at      DATETIME(3)     NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE billing_records (
    id              BIGINT          NOT NULL PRIMARY KEY,
    tenant_id       BIGINT          NOT NULL,
    project_id      BIGINT,
    operation       VARCHAR(64)     NOT NULL,
    credits         DECIMAL(18,4)   NOT NULL DEFAULT 0.0000,
    billing_mode    VARCHAR(16)     NOT NULL DEFAULT 'SOFT',
    ref_id          VARCHAR(128),
    created_at      DATETIME(3)     NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    INDEX idx_tenant_created (tenant_id, created_at),
    INDEX idx_project (project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
