CREATE TABLE IF NOT EXISTS qipai_wallet_scope_policies (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NULL,
  scope_type VARCHAR(16) NOT NULL DEFAULT 'TENANT',
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
    ON UPDATE CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_wallet_policy_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_wallet_policy_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_wallet_policy_store (tenant_id, store_id),
  KEY idx_qipai_wallet_policy_tenant (tenant_id, enabled)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_wallet_accounts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  scope_type VARCHAR(16) NOT NULL,
  store_id BIGINT UNSIGNED NULL,
  cash_balance_cents INT NOT NULL DEFAULT 0,
  gift_balance_cents INT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
    ON UPDATE CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_wallet_account_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_wallet_account_user
    FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  CONSTRAINT fk_qipai_wallet_account_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_wallet_account_scope
    (tenant_id, user_id, scope_type, store_id),
  KEY idx_qipai_wallet_account_user (tenant_id, user_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_wallet_ledger_entries (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  account_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NULL,
  business_type VARCHAR(64) NOT NULL,
  business_id VARCHAR(128) NOT NULL,
  entry_type VARCHAR(32) NOT NULL,
  cash_delta_cents INT NOT NULL DEFAULT 0,
  gift_delta_cents INT NOT NULL DEFAULT 0,
  cash_balance_after_cents INT NOT NULL,
  gift_balance_after_cents INT NOT NULL,
  operator_id BIGINT UNSIGNED NULL,
  trace_id VARCHAR(128) NOT NULL,
  note VARCHAR(512) NOT NULL DEFAULT '',
  metadata JSON NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_wallet_ledger_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_wallet_ledger_account
    FOREIGN KEY (account_id) REFERENCES qipai_wallet_accounts(id),
  CONSTRAINT fk_qipai_wallet_ledger_user
    FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  CONSTRAINT fk_qipai_wallet_ledger_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_wallet_ledger_operator
    FOREIGN KEY (operator_id) REFERENCES qipai_users(id),
  UNIQUE KEY uq_qipai_wallet_ledger_business
    (tenant_id, account_id, business_type, business_id),
  KEY idx_qipai_wallet_ledger_user (tenant_id, user_id, created_at),
  KEY idx_qipai_wallet_ledger_trace (tenant_id, trace_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026062421', 'm07a_wallet_ledger');
