import moment from "moment";
import { v4 as uuidv4 } from "uuid";
import { Op } from "sequelize";
import { User } from "@/models/user";
import { Disease } from "@/models/disease";
import { z } from "zod";

export const CreateDisease = async (req, res, next) => {
    try {
        const validator = z.object({
            name: z
                .string({ required_error: "missing required field: name" })
                .min("name must be provided"),
            description: z.string({
                required_error: "missing required field: description",
            }),
            severity: z.enum(["mild", "moderate", "severe"], {
                required_error: "missing required field: severity",
            }),
            treatment: z
                .string({
                    required_error: "missing required field: treatment",
                })
                .min("treatment must be provided"),
        });

        const body = validator.parse(req.body);
        const disease = await Disease.create({
            ...body,
            id: uuidv4(),
            created_by_id: req.user.id,
        });

        res.status(200).json({
            success: true,
            data: disease,
        });
    } catch (err) {
        next(err);
    }
};

export const GetDiseases = async (req, res, next) => {
    try {
        const { page = 1, limit = 10, search = "" } = req.query;

        const q = {};
        const query = {
            [Op.or]: ["name"].map((field) => {
                return {
                    [field]: {
                        [Op.like]: `%${search}%`,
                    },
                };
            }),
        };

        const diseases = await Disease.findAll({
            where: query,
            include: [
                {
                    model: User,
                    as: "created_by",
                    attributes: ["id", "username", "email", "role"],
                },
            ],
            order: [["created_at", "DESC"]],
            limit: Number(limit),
            offset: (Number(page) > 1 ? Number(page) - 1 : 0) * Number(limit),
        });

        const data_size = await Disease.count(q);
        const page_size = Math.ceil(data_size / Number(limit));
        const newest = await Disease.count({
            where: {
                created_at: {
                    [Op.gte]: moment().subtract(7, "d").toISOString(),
                },
            },
        });

        res.status(200).json({
            success: true,
            properties: {
                all: data_size,
                new: newest,
            },
            pagination: {
                current_page: Number(page),
                limit: Number(limit),
                data_size,
                page_size,
                next: Number(page) < page_size ? Number(page) + 1 : 0,
                prev: Number(page) > 1 ? Number(page) - 1 : 0,
            },
            data: diseases,
        });
    } catch (err) {
        next(err);
    }
};
