import path from 'node:path';

import { eventStream } from '@opengis/fastify-table/utils.js';

import mapnik from "./mapnik.js";

const { GetRasterStatus, CreateXML, RenderTile } = mapnik();

function sequence(arr, fn) {
    return arr.reduce(
        (promise, relpath, idx) => promise.then(() => fn({
            relpath, idx,
        })),
        Promise.resolve(),
    );
}

const unsafe = true;

export default async function checkRasterFile(arr, reply) {
    const callback = reply ? eventStream(reply) : () => { };

    let
        createdCount = 0,
        existsCount = 0,
        fileNotFoundCount = 0,
        xmlErrorCount = 0,
        renderErrorCount = 0;

    await sequence((arr || []), async ({
        relpath, idx,
    }) => {
        callback(`${idx + 1}/${arr.length}`);

        // check raster upload status, skip for directories
        const uploadStatus = !path.extname(relpath) ? {} : await GetRasterStatus({ path: relpath });

        if (uploadStatus.exists === false) {
            fileNotFoundCount += 1;
            callback(`file not found: ${relpath} `);
            return null;
        }

        // create at xml/:id with name only, always - by path
        const resp = await CreateXML({ path: relpath }, (msg) => {
            if (typeof msg === 'string' && msg === 'XML creation complete') {
                createdCount += 1;
            }
            if (typeof msg === 'string' && msg.startsWith('XML already exists')) {
                existsCount += 1;
            }
            callback(msg);
        }, unsafe).catch(err => ({ error: err }));

        if (resp.error?.message && resp.error.message.startsWith('5 NOT_FOUND')) {
            fileNotFoundCount += 1;
            callback(`directory not found: ${relpath} `);
            return null;
        }

        if (resp.error) {
            xmlErrorCount += 1;
            callback(`create xml error: ${resp.error.toString()} (${relpath})`);
            return null;
        }

        const data = await RenderTile({
            path: relpath,
            width: 256,
            height: 256,
            bbox: [3713463.7081504324, 6088362.176970857, 3713616.5822070027, 6088515.051027427], // /:id/18/155363/91245.png
            ttl: '0',
        }).catch(err => ({ err: err.toString() }));

        if (data.err) {
            renderErrorCount += 1;
            callback(`render error: ${data.err} (${relpath})`);
            return null;
        }

        callback(`render ok: ${relpath} `);
        return null;
    });

    callback(`${existsCount} - xml exists`);
    callback(`${createdCount} - xml created`);
    callback(`${fileNotFoundCount} - file not found`);
    callback(`${xmlErrorCount} - xml errors`);
    callback(`${renderErrorCount} - render errors`);

    callback('finish', 1);
}
