-- Muse Account 模块初始 Schema（PostgreSQL 16）
-- 影响范围：权益、配额、用量、安全事件、New-API 绑定。

CREATE TABLE muse_member_entitlement (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    account_user_id BIGINT NOT NULL,
    plan_type VARCHAR(50) NOT NULL DEFAULT 'free',
    plan_started_at TIMESTAMP,
    plan_expires_at TIMESTAMP,
    ai_call_quota INT NOT NULL DEFAULT 0,
    ai_call_used INT NOT NULL DEFAULT 0,
    storage_quota_mb BIGINT NOT NULL DEFAULT 0,
    storage_used_mb BIGINT NOT NULL DEFAULT 0,
    kb_count_quota INT NOT NULL DEFAULT 0,
    work_count_quota INT NOT NULL DEFAULT 0,
    export_quota INT NOT NULL DEFAULT 0,
    features_json JSONB,
    revision INT NOT NULL DEFAULT 1,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_member_entitlement_user UNIQUE (tenant_id, account_user_id)
);

CREATE INDEX idx_muse_member_entitlement_user ON muse_member_entitlement(tenant_id, account_user_id);
CREATE TRIGGER trg_muse_member_entitlement_updated_at BEFORE UPDATE ON muse_member_entitlement FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_member_entitlement_audit_log (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    account_user_id BIGINT NOT NULL,
    change_type VARCHAR(30) NOT NULL,
    source_owner VARCHAR(50),
    source_id BIGINT,
    idempotency_key VARCHAR(200) NOT NULL,
    before_value_snapshot JSONB,
    delta_value_snapshot JSONB,
    after_value_snapshot JSONB,
    reason_code VARCHAR(50),
    reason_message VARCHAR(500),
    operator_user_id BIGINT,
    audit_event_id BIGINT,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_member_ent_audit_idem UNIQUE (tenant_id, idempotency_key)
);

CREATE INDEX idx_muse_member_ent_audit_user ON muse_member_entitlement_audit_log(tenant_id, account_user_id, create_time);
CREATE TRIGGER trg_muse_member_ent_audit_updated_at BEFORE UPDATE ON muse_member_entitlement_audit_log FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_member_quota (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    account_user_id BIGINT NOT NULL,
    quota_type VARCHAR(50) NOT NULL,
    total_amount BIGINT NOT NULL DEFAULT 0,
    used_amount BIGINT NOT NULL DEFAULT 0,
    reset_cycle VARCHAR(20),
    last_reset_at TIMESTAMP,
    next_reset_at TIMESTAMP,
    revision INT NOT NULL DEFAULT 1,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_member_quota_user_type UNIQUE (tenant_id, account_user_id, quota_type)
);

CREATE INDEX idx_muse_member_quota_user ON muse_member_quota(tenant_id, account_user_id);
CREATE TRIGGER trg_muse_member_quota_updated_at BEFORE UPDATE ON muse_member_quota FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_member_usage_record (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    account_user_id BIGINT NOT NULL,
    usage_type VARCHAR(50) NOT NULL,
    work_id BIGINT,
    task_id BIGINT,
    token_input INT NOT NULL DEFAULT 0,
    token_output INT NOT NULL DEFAULT 0,
    cost_reference VARCHAR(100),
    gateway_request_id VARCHAR(200),
    correlation_id VARCHAR(200),
    attribution_snapshot JSONB,
    status VARCHAR(20) NOT NULL DEFAULT 'completed',
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_muse_member_usage_user_time ON muse_member_usage_record(tenant_id, account_user_id, create_time);
CREATE INDEX idx_muse_member_usage_correlation ON muse_member_usage_record(tenant_id, correlation_id);
CREATE TRIGGER trg_muse_member_usage_record_updated_at BEFORE UPDATE ON muse_member_usage_record FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_member_security_event (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    account_user_id BIGINT NOT NULL,
    event_type VARCHAR(50) NOT NULL,
    severity VARCHAR(20) NOT NULL DEFAULT 'info',
    device_info JSONB,
    ip_address VARCHAR(64),
    acknowledged BOOLEAN NOT NULL DEFAULT FALSE,
    acknowledged_at TIMESTAMP,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_muse_member_security_user ON muse_member_security_event(tenant_id, account_user_id, create_time);
CREATE TRIGGER trg_muse_member_security_updated_at BEFORE UPDATE ON muse_member_security_event FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_member_new_api_binding (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    account_user_id BIGINT NOT NULL,
    newapi_user_id VARCHAR(100) NOT NULL,
    binding_status VARCHAR(30) NOT NULL DEFAULT 'active',
    command_id VARCHAR(120) NOT NULL,
    last_sync_at TIMESTAMP,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_member_newapi_user UNIQUE (tenant_id, account_user_id),
    CONSTRAINT uk_muse_member_newapi_command UNIQUE (tenant_id, command_id)
);

CREATE INDEX idx_muse_member_newapi_remote ON muse_member_new_api_binding(tenant_id, newapi_user_id);
CREATE TRIGGER trg_muse_member_newapi_updated_at BEFORE UPDATE ON muse_member_new_api_binding FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
