<?php

/**
 * Migration 0042 — per-owner config store (multi-tenancy foundation)
 *
 * instinctor_owner_config holds tenant-scoped overrides of the whitelisted
 * per-business settings (store_type, chat_*, branding, SEO/social, etc.).
 * It parallels the flat global instinctor_config table: a lookup that misses
 * here falls through to the global value, so an owner only stores the keys
 * they actually override. See Instinctor\Core\Config::TENANT_KEYS for the
 * whitelist and Instinctor\Core\Tenant for the current-tenant resolver.
 *
 * owner_id is the instinctor_users id (role 'owner') that owns the tenant's
 * pages (pages.owner_id). The vendor site itself is owner_id IS NULL and never
 * has rows here — it keeps using the global instinctor_config.
 *
 * On WordPress single-tenant installs this table is created but unused (there
 * is one implicit owner; Tenant::currentOwnerId() resolves null → global).
 *
 * @since 2.1.2226
 */

if (!defined('ABSPATH')) exit;

return [
    'description' => 'Per-owner config store (tenant-scoped settings overrides)',
    'up' => [
        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_owner_config (
            id          int unsigned NOT NULL AUTO_INCREMENT,
            owner_id    int unsigned NOT NULL,
            config_key  varchar(191) NOT NULL,
            config_val  longtext     NOT NULL,
            updated_at  datetime     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY owner_key (owner_id, config_key),
            KEY owner_id (owner_id)
        ) {charset}",
    ],
];
