<?php
/**
 * 为已有渠道补齐缺失的默认配置（不删除已有数据）
 * 用法：php db/run_fill_missing_channel_config.php
 */
declare(strict_types=1);

define('BASE_PATH', dirname(__DIR__));
require_once BASE_PATH . '/vendor/autoload.php';

if (class_exists(\Dotenv\Dotenv::class) && is_file(BASE_PATH . '/.env')) {
    \Dotenv\Dotenv::createUnsafeMutable(BASE_PATH)->load();
}
\Webman\Config::load(BASE_PATH . '/config', ['route', 'plugin']);
\Webman\ThinkOrm\ThinkOrm::start(null);

use app\dice\service\DiceChannelConfigService;
use plugin\saiadmin\app\model\system\SystemDept;

echo "========== 补齐渠道缺失配置 ==========\n";

$service = new DiceChannelConfigService();
$service->ensureConfigCompositeKeys();

$summary = $service->syncAllChannelsFromDefault();
foreach ($summary as $deptId => $info) {
    $copied = implode(',', $info['copied'] ?? []) ?: '无';
    $merged = empty($info['merged']) ? '无' : json_encode($info['merged'], JSON_UNESCAPED_UNICODE);
    $skipped = implode(',', $info['skipped'] ?? []) ?: '无';
    echo "渠道 {$deptId}: 新增表 [{$copied}] 补齐行 {$merged} 跳过 [{$skipped}]\n";
}

echo "\n渠道数: " . SystemDept::count() . "\n完成。\n";
