package com.ruoyi.info.collection.service;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CcdiPurchaseTransactionFeatureContractTest {

    @Test
    void shouldExposeSupplierListContractsAcrossPurchaseTransactionModels() throws Exception {
        assertHasField(
            "com.ruoyi.info.collection.domain.dto.CcdiPurchaseTransactionAddDTO",
            "supplierList"
        );
        assertHasField(
            "com.ruoyi.info.collection.domain.dto.CcdiPurchaseTransactionEditDTO",
            "supplierList"
        );
        assertHasField(
            "com.ruoyi.info.collection.domain.vo.CcdiPurchaseTransactionVO",
            "supplierList"
        );
        assertHasField(
            "com.ruoyi.info.collection.domain.vo.CcdiPurchaseTransactionVO",
            "supplierCount"
        );
        assertNotNull(Class.forName("com.ruoyi.info.collection.domain.CcdiPurchaseTransactionSupplier"));
        assertNotNull(Class.forName("com.ruoyi.info.collection.domain.dto.CcdiPurchaseTransactionSupplierDTO"));
        assertNotNull(Class.forName("com.ruoyi.info.collection.domain.vo.CcdiPurchaseTransactionSupplierVO"));
        assertNotNull(Class.forName("com.ruoyi.info.collection.domain.excel.CcdiPurchaseTransactionSupplierExcel"));
    }

    @Test
    void shouldDefineSupplierSubTableAndBiddingMigrationScripts() throws Exception {
        String initSql = Files.readString(repoPath("sql/ccdi_purchase_transaction.sql"));
        assertTrue(initSql.contains("CREATE TABLE `ccdi_purchase_transaction_supplier`"));
        assertTrue(initSql.contains("`is_bid_winner`"));
        assertTrue(initSql.contains("`sort_order`"));
        assertTrue(initSql.contains("utf8mb4_general_ci"));

        String menuSql = Files.readString(repoPath("sql/ccdi_purchase_transaction_menu.sql"));
        assertTrue(menuSql.contains("招投标信息维护"));

        Path migrationPath = repoPath("sql/migration/2026-04-22-bidding-info-maintenance-supplier-detail.sql");
        assertTrue(Files.exists(migrationPath), "应提供招投标供应商明细迁移脚本");

        String migrationSql = Files.readString(migrationPath);
        assertTrue(migrationSql.contains("ccdi_purchase_transaction_supplier"));
        assertTrue(migrationSql.contains("INSERT INTO ccdi_purchase_transaction_supplier"));
        assertTrue(migrationSql.contains("UPDATE sys_menu"));
        assertTrue(migrationSql.contains("招投标信息维护"));
    }

    @Test
    void shouldUseTwoSheetTemplateForBiddingImport() throws Exception {
        assertNotNull(Class.forName("com.ruoyi.info.collection.domain.excel.CcdiPurchaseTransactionSupplierExcel"));

        String controller = Files.readString(
            Path.of("src/main/java/com/ruoyi/info/collection/controller/CcdiPurchaseTransactionController.java")
        );
        assertTrue(controller.contains("招投标主信息"));
        assertTrue(controller.contains("供应商明细"));
    }

    private void assertHasField(String className, String fieldName) throws Exception {
        Class<?> clazz = Class.forName(className);
        Field field = clazz.getDeclaredField(fieldName);
        assertNotNull(field);
    }

    private Path repoPath(String relativePath) {
        return Path.of("..", relativePath);
    }
}
