import { NextFunction, Response } from "express";
import { CustomRequest } from "../types/custom.types";
import Channel from "../models/channel.model";
import { Op, Sequelize } from "sequelize";

export default class SearchController {
    static async search(request: CustomRequest, response: Response, next: NextFunction) {
        try {
            const data = await Channel.findAll({
                where: {
                    status: "VISIBLE",
                    [Op.and]: Sequelize.where(
                        Sequelize.fn("LOWER", Sequelize.col("name")),
                        {
                            [Op.like]: `%${String(request?.query?.search||"").toLowerCase()}%`,
                        }
                    ),
                },
            });
            response.status(200).json(data);
        } catch (error) {
            next(error);
        }
    }
}