const assert = require("assert");
const fs = require("fs");
const path = require("path");

const repoRoot = path.join(__dirname, "..");

function read(relPath) {
  return fs.readFileSync(path.join(repoRoot, relPath), "utf8");
}

const versionPath = path.join(repoRoot, "VERSION");
assert(fs.existsSync(versionPath), "VERSION must exist");
const appVersion = fs.readFileSync(versionPath, "utf8").trim();
assert(/^\d+\.\d+\.\d+$/.test(appVersion), "VERSION must use x.y.z format");

const changelog = read("debian/changelog");
assert(
  new RegExp(`^labelnova \\(${appVersion.replace(/\./g, "\\.")}-`).test(changelog),
  `top changelog entry must start with ${appVersion}-`
);

const desktop = read("debian/cn.labelnova.app.desktop");
assert(desktop.includes("Exec=/opt/apps/cn.labelnova.app/files/bin/labelnova %U"));
assert(desktop.includes("Icon=cn.labelnova.app"));
assert(desktop.includes("Categories=Graphics;Office;Utility;"));
assert(desktop.includes("Keywords[zh_CN]=标签;条码;二维码;打印;打印机;"));

const info = read("debian/cn.labelnova.app.info.in");
assert(info.includes('"appid": "cn.labelnova.app"'));
assert(info.includes('"name": "LabelNova"'));
assert(info.includes('"version": "@VERSION@"'));
assert(info.includes('"arch": ["@ARCH@"]'));

const rules = read("debian/rules");
assert(rules.includes("APPID := cn.labelnova.app"));
assert(rules.includes("APPROOT := $(CURDIR)/debian/labelnova/opt/apps/$(APPID)"));
assert(rules.includes("APP_VERSION := $(shell cat VERSION)"));
assert(rules.includes("install -m 0755 $(BUILDDIR)/labelnova $(APPFILES)/bin/labelnova"));
assert(rules.includes("cp -a index.html $(APPFILES)/frontend/"));
assert(rules.includes("cp -a static $(APPFILES)/frontend/"));
assert(rules.includes("cp -a icons $(APPFILES)/frontend/"));
assert(rules.includes("install -m 0644 debian/$(APPID).desktop $(APPENTRIES)/applications/$(APPID).desktop"));
assert(rules.includes("install -m 0644 qt-shell/packaging/labelnova.svg $(APPENTRIES)/icons/hicolor/scalable/apps/$(APPID).svg"));
assert(rules.includes("debian/$(APPID).info.in > $(APPROOT)/info"));
assert(rules.includes("node tests/debian-store-layout.test.js"));
assert(rules.includes("node tests/label-templates.test.js"));
assert(rules.includes("node tests/object-placement.test.js"));
assert(rules.includes("node tests/desktop-runtime.test.js"));
assert(rules.includes("desktop-file-validate $(APPENTRIES)/applications/$(APPID).desktop"));
assert(rules.includes("authoritative deepin/UOS /opt/apps package staging path"));

const control = read("debian/control");
assert(control.includes("desktop-file-utils"));
assert(control.includes("nodejs"));
assert(control.includes("/opt/apps/cn.labelnova.app"));

const cmake = read("qt-shell/CMakeLists.txt");
assert(cmake.includes('file(READ "${CMAKE_CURRENT_LIST_DIR}/../VERSION" LABELNOVA_VERSION_RAW)'));
assert(cmake.includes("string(STRIP \"${LABELNOVA_VERSION_RAW}\" LABELNOVA_VERSION)"));
assert(cmake.includes('project(labelnova VERSION "${LABELNOVA_VERSION}" LANGUAGES CXX)'));
assert(cmake.includes('LABELNOVA_APP_VERSION="${PROJECT_VERSION}"'));
assert(!cmake.includes('DESTINATION share/applications'));
assert(!cmake.includes('DESTINATION share/icons/hicolor/scalable/apps'));
assert(cmake.includes('install(FILES "${FRONTEND_ROOT}/index.html" DESTINATION frontend)'));
assert(cmake.includes('install(DIRECTORY "${FRONTEND_ROOT}/static" DESTINATION frontend)'));
assert(cmake.includes('install(DIRECTORY "${FRONTEND_ROOT}/icons" DESTINATION frontend)'));
assert(cmake.includes("generic/local install layout"));
assert(cmake.includes("deepin/UOS store package layout is staged in debian/rules"));
assert(!cmake.includes('set(CPACK_GENERATOR "DEB")'));

const mainCpp = read("qt-shell/src/main.cpp");
assert(mainCpp.includes("QCoreApplication::setApplicationVersion(QStringLiteral(LABELNOVA_APP_VERSION));"));
assert(!mainCpp.includes('QCoreApplication::setApplicationVersion("1.0.0");'));

const buildDeb = read("qt-shell/build-deb.sh");
assert(buildDeb.includes("dpkg-buildpackage -b -d -us -uc"));
assert(!buildDeb.includes("cpack --config"));

assert.strictEqual(
  fs.existsSync(path.join(repoRoot, "debian/install")),
  false,
  "debian/install should be removed once debian/rules owns runtime staging"
);

console.log("debian store layout tests passed");
