"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const core_1 = require("@nestjs/core");
const common_1 = require("@nestjs/common");
const path_1 = require("path");
const fs_1 = require("fs");
const app_module_1 = require("../app.module");
const numberplate_service_1 = require("../appapi/numberplate/numberplate.service");
const DEFAULT_PLATE_URL = "https://xplate.io/en/numbers/images/license-plate/dubai-classic/NOC/99899/0/0/1200";
async function bootstrap() {
    const logger = new common_1.Logger("GenerateCarPreviews");
    const plateImageUrl = process.argv.find((a) => a.startsWith("--plateImageUrl="))?.split("=")[1] ||
        DEFAULT_PLATE_URL;
    const publicBaseUrl = process.argv.find((a) => a.startsWith("--publicBaseUrl="))?.split("=")[1] ||
        "";
    const drawBox = process.argv.includes("--drawBox");
    logger.log(`Plate image: ${plateImageUrl}`);
    logger.log(`Public base URL: ${publicBaseUrl || "(none)"}`);
    logger.log(`Draw debug box: ${drawBox}`);
    const app = await core_1.NestFactory.createApplicationContext(app_module_1.AppModule, {
        logger: ["error", "warn"],
    });
    try {
        const numberPlateService = app.get(numberplate_service_1.NumberPlateService);
        const cars = numberPlateService.getCarAssets();
        logger.log(`Generating previews for ${cars.length} cars: ${cars.map((c) => c.slug).join(", ")}`);
        const allResults = await numberPlateService.generateAllCarPreviews({
            plateImageUrl,
            publicBaseUrl: publicBaseUrl || undefined,
            drawBox: drawBox ? "true" : undefined,
            offsetY: "0",
        });
        const outputDir = (0, path_1.join)(process.cwd(), "uploads", "number-plate-previews");
        await fs_1.promises.mkdir(outputDir, { recursive: true });
        const summary = [];
        for (const car of cars) {
            const result = allResults[car.slug];
            summary.push({
                car: car.slug,
                label: car.label,
                front: result?.front?.previewPath || "ERROR",
                back: result?.back?.previewPath || "ERROR",
            });
        }
        logger.log("\n=== Generated Previews ===");
        for (const row of summary) {
            logger.log(`[${row.label}]`);
            logger.log(`  front: ${row.front}`);
            logger.log(`  back:  ${row.back}`);
        }
        const reportPath = (0, path_1.join)(process.cwd(), "uploads", "number-plate-previews", "preview-report.json");
        await fs_1.promises.writeFile(reportPath, JSON.stringify({ plateImageUrl, previews: summary }, null, 2), "utf8");
        logger.log(`\nReport written to ${reportPath}`);
    }
    finally {
        await app.close();
    }
}
bootstrap().catch((err) => {
    console.error(err);
    process.exit(1);
});
//# sourceMappingURL=generate-car-previews.js.map