import { createRequire } from 'module';
import PocketBase from 'pocketbase';

const require = createRequire(import.meta.url);

let runtimeConfig = {};
try {
    runtimeConfig = require('../pocket-base/bai_api_pb_hooks/bai_api_shared/config/runtime.js');
} catch (_error) {
    runtimeConfig = {};
}

const PB_URL = (process.env.PB_URL || 'https://bai-api.blv-oa.com/pb').replace(/\/+$/, '');
const AUTH_TOKEN = process.env.POCKETBASE_AUTH_TOKEN || runtimeConfig.POCKETBASE_AUTH_TOKEN || '';
const SOFT_DELETE_RULE = 'is_delete = 0';

if (!AUTH_TOKEN) {
    console.error('❌ 缺少 POCKETBASE_AUTH_TOKEN，无法执行建表。');
    process.exit(1);
}

const pb = new PocketBase(PB_URL);

const collections = [
    {
        name: 'tbl_product_list',
        type: 'base',
        // Empty rules in PocketBase mean public read access.
        listRule: SOFT_DELETE_RULE,
        viewRule: SOFT_DELETE_RULE,
        createRule: '(@request.auth.users_idtype = "ManagePlatform" || @request.auth.usergroups_id = "ROLE-1774666070666-9dDrTB")',
        updateRule: '(@request.auth.users_idtype = "ManagePlatform" || @request.auth.usergroups_id = "ROLE-1774666070666-9dDrTB")',
        deleteRule: '(@request.auth.users_idtype = "ManagePlatform" || @request.auth.usergroups_id = "ROLE-1774666070666-9dDrTB")',
        fields: [
            { name: 'prod_list_id', type: 'text', required: true },
            { name: 'prod_list_name', type: 'text', required: true },
            { name: 'prod_list_modelnumber', type: 'text' },
            { name: 'prod_list_barcode', type: 'text' },
            { name: 'prod_list_icon', type: 'text' },
            { name: 'prod_list_description', type: 'text' },
            { name: 'prod_list_feature', type: 'text' },
            { name: 'prod_list_parameters', type: 'json' },
            { name: 'prod_list_function', type: 'json' },
            { name: 'prod_list_plantype', type: 'text' },
            { name: 'prod_list_category', type: 'text', required: true },
            { name: 'prod_list_sort', type: 'number' },
            { name: 'prod_list_comm_type', type: 'text' },
            { name: 'prod_list_series', type: 'text' },
            { name: 'prod_list_power_supply', type: 'text' },
            { name: 'prod_list_tags', type: 'text' },
            { name: 'prod_list_status', type: 'text' },
            { name: 'prod_list_basic_price', type: 'number' },
            { name: 'prod_list_vip_price', type: 'json' },
            { name: 'prod_list_remark', type: 'text' },
            { name: 'is_delete', type: 'number', default: 0, min: 0, max: 1, onlyInt: true },
        ],
        indexes: [
            'CREATE UNIQUE INDEX idx_tbl_product_list_prod_list_id ON tbl_product_list (prod_list_id)',
            'CREATE INDEX idx_tbl_product_list_prod_list_name ON tbl_product_list (prod_list_name)',
            'CREATE INDEX idx_tbl_product_list_prod_list_modelnumber ON tbl_product_list (prod_list_modelnumber)',
            'CREATE INDEX idx_tbl_product_list_prod_list_status ON tbl_product_list (prod_list_status)',
            'CREATE INDEX idx_tbl_product_list_prod_list_category ON tbl_product_list (prod_list_category)',
            'CREATE INDEX idx_tbl_product_list_prod_list_sort ON tbl_product_list (prod_list_sort)',
            'CREATE INDEX idx_tbl_product_list_category_sort ON tbl_product_list (prod_list_category, prod_list_sort)',
            'CREATE INDEX idx_tbl_product_list_prod_list_series ON tbl_product_list (prod_list_series)',
            'CREATE INDEX idx_tbl_product_list_prod_list_tags ON tbl_product_list (prod_list_tags)',
        ],
    },
];

function normalizeFieldPayload(field, existingField) {
    const payload = existingField
        ? Object.assign({}, existingField)
        : {
            name: field.name,
            type: field.type,
        };

    if (existingField && existingField.id) {
        payload.id = existingField.id;
    }

    payload.name = field.name;
    payload.type = field.type;

    if (typeof field.required !== 'undefined') {
        payload.required = field.required;
    }

    if (field.type === 'autodate') {
        payload.onCreate = typeof field.onCreate === 'boolean' ? field.onCreate : true;
        payload.onUpdate = typeof field.onUpdate === 'boolean' ? field.onUpdate : false;
    }

    return payload;
}

function buildCollectionPayload(collectionData, existingCollection) {
    if (!existingCollection) {
        return {
            name: collectionData.name,
            type: collectionData.type,
            listRule: Object.prototype.hasOwnProperty.call(collectionData, 'listRule') ? collectionData.listRule : null,
            viewRule: Object.prototype.hasOwnProperty.call(collectionData, 'viewRule') ? collectionData.viewRule : null,
            createRule: Object.prototype.hasOwnProperty.call(collectionData, 'createRule') ? collectionData.createRule : null,
            updateRule: Object.prototype.hasOwnProperty.call(collectionData, 'updateRule') ? collectionData.updateRule : null,
            deleteRule: Object.prototype.hasOwnProperty.call(collectionData, 'deleteRule') ? collectionData.deleteRule : null,
            fields: collectionData.fields.map((field) => normalizeFieldPayload(field, null)),
            indexes: collectionData.indexes,
        };
    }

    const targetFieldMap = new Map(collectionData.fields.map((field) => [field.name, field]));
    const fields = (existingCollection.fields || []).filter((existingField) => {
        return existingField && existingField.name !== 'id';
    }).map((existingField) => {
        const targetField = targetFieldMap.get(existingField.name);
        if (!targetField) {
            return existingField;
        }

        targetFieldMap.delete(existingField.name);
        return normalizeFieldPayload(targetField, existingField);
    });

    for (const field of targetFieldMap.values()) {
        fields.push(normalizeFieldPayload(field, null));
    }

    return {
        name: collectionData.name,
        type: collectionData.type,
        listRule: Object.prototype.hasOwnProperty.call(collectionData, 'listRule') ? collectionData.listRule : existingCollection.listRule,
        viewRule: Object.prototype.hasOwnProperty.call(collectionData, 'viewRule') ? collectionData.viewRule : existingCollection.viewRule,
        createRule: Object.prototype.hasOwnProperty.call(collectionData, 'createRule') ? collectionData.createRule : existingCollection.createRule,
        updateRule: Object.prototype.hasOwnProperty.call(collectionData, 'updateRule') ? collectionData.updateRule : existingCollection.updateRule,
        deleteRule: Object.prototype.hasOwnProperty.call(collectionData, 'deleteRule') ? collectionData.deleteRule : existingCollection.deleteRule,
        fields: fields,
        indexes: collectionData.indexes,
    };
}

function normalizeFieldList(fields) {
    return (fields || []).map((field) => ({
        name: field.name,
        type: field.type,
        required: !!field.required,
    }));
}

async function createOrUpdateCollection(collectionData) {
    console.log(`🔄 正在处理表: ${collectionData.name} ...`);

    try {
        const list = await pb.collections.getFullList({
            sort: '-created',
        });
        const existing = list.find((item) => item.name === collectionData.name);

        if (existing) {
            await pb.collections.update(existing.id, buildCollectionPayload(collectionData, existing));
            console.log(`♻️ ${collectionData.name} 已存在，已按最新结构更新。`);
            return;
        }

        await pb.collections.create(buildCollectionPayload(collectionData, null));
        console.log(`✅ ${collectionData.name} 创建完成。`);
    } catch (error) {
        console.error(`❌ 处理集合 ${collectionData.name} 失败:`, {
            status: error.status,
            message: error.message,
            response: error.response,
        });
        throw error;
    }
}

async function getCollectionByName(collectionName) {
    const list = await pb.collections.getFullList({
        sort: '-created',
    });
    return list.find((item) => item.name === collectionName) || null;
}

async function verifyCollections(targetCollections) {
    console.log('\n🔍 开始校验产品表结构与索引...');

    for (const target of targetCollections) {
        const remote = await getCollectionByName(target.name);
        if (!remote) {
            throw new Error(`${target.name} 不存在`);
        }
        const remoteFields = normalizeFieldList(remote.fields);
        const targetFields = normalizeFieldList(target.fields);
        const remoteFieldMap = new Map(remoteFields.map((field) => [field.name, field.type]));
        const remoteRequiredMap = new Map(remoteFields.map((field) => [field.name, field.required]));
        const missingFields = [];
        const mismatchedTypes = [];
        const mismatchedRequired = [];

        for (const field of targetFields) {
            if (!remoteFieldMap.has(field.name)) {
                missingFields.push(field.name);
                continue;
            }

            if (remoteFieldMap.get(field.name) !== field.type) {
                mismatchedTypes.push(`${field.name}:${remoteFieldMap.get(field.name)}!=${field.type}`);
            }

            if (remoteRequiredMap.get(field.name) !== !!field.required) {
                mismatchedRequired.push(`${field.name}:${remoteRequiredMap.get(field.name)}!=${!!field.required}`);
            }
        }

        const remoteIndexes = new Set(remote.indexes || []);
        const missingIndexes = target.indexes.filter((indexSql) => !remoteIndexes.has(indexSql));

        if (remote.type !== target.type) {
            throw new Error(`${target.name} 类型不匹配，期望 ${target.type}，实际 ${remote.type}`);
        }

        if (!missingFields.length && !mismatchedTypes.length && !mismatchedRequired.length && !missingIndexes.length) {
            console.log(`✅ ${target.name} 校验通过。`);
            continue;
        }

        console.log(`❌ ${target.name} 校验失败：`);
        if (missingFields.length) {
            console.log(`  - 缺失字段: ${missingFields.join(', ')}`);
        }
        if (mismatchedTypes.length) {
            console.log(`  - 字段类型不匹配: ${mismatchedTypes.join(', ')}`);
        }
        if (mismatchedRequired.length) {
            console.log(`  - 字段必填属性不匹配: ${mismatchedRequired.join(', ')}`);
        }
        if (missingIndexes.length) {
            console.log(`  - 缺失索引: ${missingIndexes.join(' | ')}`);
        }

        throw new Error(`${target.name} 结构与预期不一致`);
    }
}

async function init() {
    try {
        console.log(`🔄 正在连接 PocketBase: ${PB_URL}`);
        pb.authStore.save(AUTH_TOKEN, null);
        console.log('✅ 已使用 POCKETBASE_AUTH_TOKEN 载入认证状态。');

        for (const collectionData of collections) {
            await createOrUpdateCollection(collectionData);
        }

        await verifyCollections(collections);
        console.log('\n🎉 产品表结构初始化并校验完成！');
    } catch (error) {
        console.error('❌ 初始化失败:', error.response?.data || error.message);
        process.exitCode = 1;
    }
}

init();
