CREATE TABLE IF NOT EXISTS qipai_third_party_configs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NULL,
  provider VARCHAR(32) NOT NULL,
  mode VARCHAR(16) NOT NULL DEFAULT 'MANUAL',
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  credential_ref VARCHAR(255) NOT NULL DEFAULT '',
  settings JSON NOT NULL,
  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_third_party_config_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_third_party_config_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_third_party_config_scope (tenant_id, provider, store_id),
  KEY idx_qipai_third_party_config_resolution (tenant_id, provider, enabled, store_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_third_party_mappings (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  provider VARCHAR(32) NOT NULL,
  resource_type VARCHAR(16) NOT NULL,
  external_ref VARCHAR(128) NOT NULL,
  local_resource_id BIGINT UNSIGNED NOT NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_third_party_mapping_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  UNIQUE KEY uq_qipai_third_party_mapping_external
    (tenant_id, provider, resource_type, external_ref),
  KEY idx_qipai_third_party_mapping_local
    (tenant_id, provider, resource_type, local_resource_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_group_vouchers (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  provider VARCHAR(32) NOT NULL,
  voucher_hash CHAR(64) NOT NULL,
  voucher_masked VARCHAR(64) NOT NULL,
  external_product_id VARCHAR(128) NOT NULL DEFAULT '',
  status VARCHAR(32) NOT NULL DEFAULT 'RECEIVED',
  amount_cents INT UNSIGNED NOT NULL DEFAULT 0,
  expires_at DATETIME(3) NULL,
  redeemed_at DATETIME(3) NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_group_voucher_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  UNIQUE KEY uq_qipai_group_voucher_hash (tenant_id, provider, voucher_hash),
  KEY idx_qipai_group_voucher_status (tenant_id, provider, status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_group_redemptions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  voucher_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  actor_id BIGINT UNSIGNED NULL,
  redemption_mode VARCHAR(16) NOT NULL,
  client_request_id VARCHAR(128) NOT NULL,
  status VARCHAR(32) NOT NULL,
  failure_code VARCHAR(64) NOT NULL DEFAULT '',
  provider_response JSON NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  completed_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_group_redemption_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_group_redemption_voucher
    FOREIGN KEY (voucher_id) REFERENCES qipai_group_vouchers(id),
  CONSTRAINT fk_qipai_group_redemption_order
    FOREIGN KEY (order_id) REFERENCES qipai_orders(id),
  CONSTRAINT fk_qipai_group_redemption_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_group_redemption_request (tenant_id, client_request_id),
  UNIQUE KEY uq_qipai_group_redemption_voucher (tenant_id, voucher_id),
  KEY idx_qipai_group_redemption_status (tenant_id, status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_direct_bookings (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  provider VARCHAR(32) NOT NULL,
  event_id VARCHAR(128) NOT NULL,
  external_booking_no VARCHAR(128) NOT NULL,
  external_store_ref VARCHAR(128) NOT NULL,
  external_room_ref VARCHAR(128) NOT NULL,
  store_id BIGINT UNSIGNED NULL,
  room_id BIGINT UNSIGNED NULL,
  customer_ref_hash CHAR(64) NOT NULL DEFAULT '',
  starts_at DATETIME(3) NOT NULL,
  ends_at DATETIME(3) NOT NULL,
  amount_cents INT UNSIGNED NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'PENDING_MAPPING',
  order_id BIGINT UNSIGNED NULL,
  claimed_user_id BIGINT UNSIGNED NULL,
  payload JSON NOT NULL,
  failure_code VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  claimed_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_direct_booking_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_direct_booking_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_direct_booking_room
    FOREIGN KEY (room_id) REFERENCES qipai_rooms(id),
  CONSTRAINT fk_qipai_direct_booking_order
    FOREIGN KEY (order_id) REFERENCES qipai_orders(id),
  UNIQUE KEY uq_qipai_direct_booking_event (tenant_id, provider, event_id),
  UNIQUE KEY uq_qipai_direct_booking_no
    (tenant_id, provider, external_booking_no),
  KEY idx_qipai_direct_booking_status (tenant_id, provider, status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026062217', 'm05c_third_party');
