-- M02-A multi-application and tenant configuration model.

CREATE TABLE IF NOT EXISTS qipai_platform_apps (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  appid VARCHAR(64) NOT NULL,
  name VARCHAR(128) NOT NULL,
  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),
  deleted_at DATETIME(3) NULL,
  UNIQUE KEY uq_qipai_platform_apps_appid (appid),
  KEY idx_qipai_platform_apps_status (status, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_tenant_apps (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  platform_app_id BIGINT UNSIGNED NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
  is_default TINYINT(1) NOT NULL DEFAULT 0,
  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),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_tenant_apps_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_tenant_apps_platform_app
    FOREIGN KEY (platform_app_id) REFERENCES qipai_platform_apps(id),
  UNIQUE KEY uq_qipai_tenant_apps_tenant_app (tenant_id, platform_app_id),
  KEY idx_qipai_tenant_apps_app_tenant (platform_app_id, tenant_id, status, deleted_at),
  KEY idx_qipai_tenant_apps_tenant_default (tenant_id, is_default, status, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_tenant_configs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  platform_app_id BIGINT UNSIGNED NOT NULL,
  brand_name VARCHAR(128) NOT NULL,
  logo_url VARCHAR(512) NOT NULL DEFAULT '',
  theme_color VARCHAR(16) NOT NULL DEFAULT '#1677ff',
  service_phone VARCHAR(32) NOT NULL DEFAULT '',
  franchise_phone VARCHAR(32) NOT NULL DEFAULT '',
  share_title VARCHAR(128) NOT NULL DEFAULT '',
  share_image_url VARCHAR(512) NOT NULL DEFAULT '',
  default_store_id BIGINT UNSIGNED NULL,
  extra_config JSON 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),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_tenant_configs_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_tenant_configs_platform_app
    FOREIGN KEY (platform_app_id) REFERENCES qipai_platform_apps(id),
  CONSTRAINT fk_qipai_tenant_configs_default_store
    FOREIGN KEY (default_store_id) REFERENCES qipai_stores(id),
  UNIQUE KEY uq_qipai_tenant_configs_tenant_app (tenant_id, platform_app_id),
  KEY idx_qipai_tenant_configs_app_tenant (platform_app_id, tenant_id, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061803', 'm02a_tenant_apps');
