import { Router } from "express";
import { ILog, MLog } from "../../mongo/logs.js";
import dayjs from "dayjs";
import { msToTime } from "../../util/time.js";
import { PipelineStage } from "mongoose";
import { MBlacklist } from "../../mongo/blacklist.js";
import { SteamUtil } from "../../util/SteamUtil.js";
import { GitUtil } from "../../util/git.js";
import { removeProperties } from "../../util/functions.js";
import { SuccessResponseBuilder } from "../responseBuilder.js";

const generateSearch = (regex: RegExp) => [
    {
        stationName: { $regex: regex },
    },
    {
        userUsername: { $regex: regex },
    },
    {
        stationShort: { $regex: regex },
    },
    {
        userSteamId: { $regex: regex },
    },
    {
        server: { $regex: regex },
    },
];

export class StationsRoute
{
    static load()
    {
        const app = Router();

        app.get("/", async (req, res) =>
        {
            const s = req.query.q?.toString().split(",").map(x => new RegExp(x, "i"));

            const filter: PipelineStage[] = [];


            s && filter.push({
                $match: {
                    $and: [
                        ...s.map(x => ({ $or: generateSearch(x) })),
                    ],
                },
            });

            const records = await MLog.aggregate(filter)
                .sort({ leftDate: -1 })
                .limit(30);
            res.json(
                new SuccessResponseBuilder<{ records: Omit<ILog, "_id" | "__v">[] }>()
                    .setCode(200)
                    .setData({ records: records.map(x => removeProperties<Omit<ILog, "_id" | "__v">>(x, [ "_id", "__v" ])) })
                    .toJSON(),
            );
        });

        return app;
    }
}