import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join, resolve } from "node:path";

const __dirname = dirname(fileURLToPath(import.meta.url));
const source = readFileSync(join(__dirname, "InventoryLedgerView.vue"), "utf8");
const mainCss = readFileSync(resolve(__dirname, "../styles/main.css"), "utf8");

function functionBody(name) {
  const match = source.match(new RegExp(`async function ${name}\\(\\)[\\s\\S]*?\\n\\}`));
  assert.ok(match, `expected ${name} to exist`);
  return match[0];
}

describe("InventoryLedgerView warehouse action layout", () => {
  it("keeps stocktake inside the tool action group instead of a standalone rack", () => {
    assert.equal(source.includes("warehouse-stocktake-rack"), false);
    assert.equal(source.includes("stocktake-launch-button"), false);

    const toolActionsMatch = source.match(/<div class="warehouse-tool-actions">([\s\S]*?)<\/div>/);
    assert.ok(toolActionsMatch, "expected warehouse tool action group to exist");
    assert.match(toolActionsMatch[1], /<strong>盘库<\/strong>/);
    assert.match(toolActionsMatch[1], /@click="openStocktakeDialog"/);
  });

  it("does not render the separate warehouse header strip above the switchboard", () => {
    assert.equal(source.includes("inventory-main-header"), false);
  });

  it("makes the selected warehouse card visibly heavier than inactive cards", () => {
    assert.match(mainCss, /\.warehouse-tab-card-active\s*\{[\s\S]*border:\s*2px solid color-mix\(in srgb,\s*currentColor 72%,\s*white 28%\);/);
    assert.match(mainCss, /\.warehouse-tab-card-active\s*\{[\s\S]*0 0 0 5px color-mix\(in srgb,\s*currentColor 14%,\s*transparent\)/);
    assert.match(mainCss, /\.warehouse-tab-card-active::before\s*\{[\s\S]*content:\s*"当前";/);
  });

  it("shows document archive actions in warehouse ledger rows", () => {
    assert.match(source, /DocumentArchiveActions/);
    assert.match(source, /warehouse-operation/);
    assert.match(source, /previewWarehouseLedgerArchive/);
    assert.match(source, /downloadWarehouseLedgerArchive/);
    assert.match(source, /regenerateWarehouseLedgerArchive/);
  });

  it("supports exporting warehouse ledger excel with current filters", () => {
    assert.match(source, /导出Excel/);
    assert.match(source, /exportWarehouseLedgerExcel/);
    assert.match(source, /\/inventory\/transaction-ledger\/export/);
    assert.match(source, /warehouseLedgerExporting/);
  });

  it("shows document archive actions in inventory detail transaction rows", () => {
    assert.match(source, /key:\s*"archive_actions"/);
    assert.match(source, /<template #cell-archive_actions="\{ row \}">/);
    assert.match(source, /单据留档/);
  });

  it("uses the shared paper document shell for warehouse operation forms", () => {
    assert.match(source, /import WarehouseDocumentFormShell/);
    assert.match(source, /import WarehouseDocumentSection/);
    assert.match(source, /import WarehouseDocumentActionBar/);
    assert.match(source, /<WarehouseDocumentFormShell/);
    assert.match(source, /<WarehouseDocumentSection/);
    assert.match(source, /<WarehouseDocumentActionBar/);
  });

  it("surfaces generated PDF archive feedback after independent warehouse saves", () => {
    [
      ["submitRawReturnOutbound", "退货出库"],
      ["submitReturnInbound", "退货入库"],
      ["submitReturnScrapInbound", "退货废料入库"],
      ["submitReturnReworkOutbound", "返工出库"],
      ["submitSalesOut", "销售出库单"],
      ["submitSpecialAdjustment", "specialAdjustmentDirectionText.value"]
    ].forEach(([name, label]) => {
      const body = functionBody(name);
      assert.match(body, new RegExp(`rememberWarehouseArchiveResult\\(result, ${label === "specialAdjustmentDirectionText.value" ? label.replace(".", "\\.") : `"${label}"`}\\)`));
      assert.match(body, /formatArchiveFeedback\(result\)/);
    });
  });

  it("uses compact industrial layout for warehouse ledger drawer controls and archive column", () => {
    assert.match(source, /warehouse-ledger-filter-fields/);
    assert.match(source, /warehouse-ledger-filter-actions/);
    assert.match(source, /warehouse-ledger-export-button/);
    assert.match(source, /warehouse-ledger-table-wrap/);
    assert.match(source, /warehouse-ledger-table/);
    assert.match(source, /warehouse-ledger-archive-cell/);
    assert.equal(source.includes("warehouse-ledger-table-toolbar"), false);
    const filterActionsMatch = source.match(/<div class="warehouse-ledger-filter-actions">([\s\S]*?)<\/div>/);
    assert.ok(filterActionsMatch, "expected warehouse ledger filter actions to exist");
    assert.match(
      filterActionsMatch[1],
      /warehouse-ledger-export-button/,
      "export should stay with the query/reset filter actions"
    );
    const exportIndex = source.indexOf("warehouse-ledger-export-button");
    const filterActionsIndex = source.indexOf("warehouse-ledger-filter-actions");
    assert.ok(exportIndex > filterActionsIndex, "expected export button to sit inside filter action area");
    assert.match(mainCss, /\.warehouse-ledger-filter-actions/);
    assert.match(mainCss, /\.warehouse-ledger-export-button/);
    assert.doesNotMatch(mainCss, /body \.warehouse-ledger-table-toolbar/);
    assert.match(mainCss, /\.warehouse-ledger-table \.document-archive-actions/);
    assert.match(mainCss, /body \.warehouse-ledger-filters\s*\{[\s\S]*grid-template-columns: 1fr !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions\s*\{[\s\S]*justify-self: end !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*font-weight: 800 !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*box-sizing: border-box !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*align-items: center !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*justify-content: center !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*gap: 7px !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*min-width: 126px !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button::before\s*\{[\s\S]*margin: 0 !important;/);
    assert.match(mainCss, /body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*0 8px 16px rgba\(15, 23, 42, 0\.08\)/);
    assert.doesNotMatch(
      mainCss.match(/body \.warehouse-ledger-filter-actions \.warehouse-ledger-export-button\s*\{[\s\S]*?\n\}/)?.[0] || "",
      /0 10px 22px rgba\(22, 163, 74, 0\.14\)/,
      "warehouse ledger export button should not use the oversized green call-to-action shadow"
    );
    assert.match(mainCss, /body \.warehouse-ledger-summary\s*\{[\s\S]*repeat\(auto-fit, minmax\(158px, 1fr\)\)/);
    assert.match(mainCss, /body \.workflow-drawer\.form-drawer:has\(\.warehouse-ledger-drawer\)/);
    assert.match(mainCss, /@media \(max-width: 560px\)[\s\S]*body \.warehouse-ledger-summary\s*\{[\s\S]*grid-template-columns: 1fr !important;/);
    assert.match(mainCss, /Final warehouse ledger drawer UI override/);
    assert.ok(
      mainCss.lastIndexOf("Final warehouse ledger drawer UI override") > mainCss.lastIndexOf("Shared warehouse paper-form components"),
      "warehouse ledger drawer overrides must stay after shared paper document styles"
    );
  });

  it("uses the shared black hover tooltip for truncated warehouse ledger values", () => {
    assert.match(source, /ref="warehouseLedgerTableRef"/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(formatDateTime\(row\.biz_time\)\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(formatInventoryTxnTypeLabel\(row\.txn_type\)\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(formatWarehouseLedgerItemLabel\(row\)\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(formatWarehouseLedgerLotNo\(row\)\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(formatWarehouseLedgerSourceLabel\(row\)\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(row\.operator_name \|\| ['"]-['"]\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(formatWarehouseLedgerLogisticsLabel\(row\)\)/);
    assert.match(source, /warehouseLedgerTooltipAttrs\(row\.remark \|\| ['"]-['"]\)/);
    assert.match(source, /function warehouseLedgerTooltipAttrs\(value\)/);
    assert.match(source, /function bindWarehouseLedgerCellTooltip\(\)/);
    assert.match(source, /function showWarehouseLedgerCellTooltip\(cell, event\)/);
    assert.match(source, /table-cell-hover-tooltip/);
    assert.match(source, /data-table-tooltip/);
    assert.match(source, /data-table-full-value/);
    assert.match(source, /onBeforeUnmount\(clearWarehouseLedgerTooltipListeners\)/);
    assert.doesNotMatch(source, /<td :title="formatDateTime\(row\.biz_time\)"/);
    assert.doesNotMatch(source, /<td :title="formatWarehouseLedgerLotNo\(row\)"/);
  });
});
