import { z } from "zod";
import { literal, Op } from "sequelize";
import { Diagnose } from "@/models/diagnose";
import { Disease } from "@/models/disease";
import { Rule } from "@/models/rule";
import { FollowUpQuestion, Symptom } from "../models/symptom";
import { User } from "../models/user";

export const GenerateDefaultData = async (req, res, next) => {
    try {
        const def = {
            users: [
                {
                    id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    email: "admin@balitasehat.id",
                    username: "admin",
                    password:
                        "$2a$12$5dmmOWGJnGfl3OVd7SzZqed72cLcwNyhLplwQFFOlvAvyubjnlGsK",
                    role: "admin",
                },
                {
                    id: "dccfadd6-c37f-4ce3-88c8-e202413f83c0",
                    email: "johndoe@balitasehat.id",
                    username: "johndoe",
                    password:
                        "$2a$12$ni8YCtn8B2ko7AE75Lqm4uS9ZY2nEO2OXX3vhi1WFCDfp.78GOu3G",
                    role: "staff",
                },
            ],
            symptoms: [
                {
                    id: "b82fb6e4-7656-42e7-8e22-c4e9a94d5466",
                    index: 0,
                    name: "Tanda Bahaya Umum",
                    description:
                        "Gejala yang memerlukan perhatian medis segera",
                    category: "danger",
                    question:
                        "Apakah balita menunjukkan tanda bahaya umum seperti: tidak bisa minum/menyusu, muntah terus menerus, kejang, atau tidak sadar/sangat lemas?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        {
                            id: "f1c9e2ab-6c89-4f0b-9f4a-1b4c3b2a9154",
                            symptom_id: "b82fb6e4-7656-42e7-8e22-c4e9a94d5466",
                            question:
                                "Apakah balita mengalami kejang saat ini?",
                            type: "yes/no",
                            options: ["Ya", "Tidak"],
                        },
                        {
                            id: "c2e4ab98-8ad3-4a39-981e-134fc56b61b1",
                            symptom_id: "b82fb6e4-7656-42e7-8e22-c4e9a94d5466",
                            question:
                                "Apakah balita terlihat sangat lemas atau tidak sadar?",
                            type: "yes/no",
                            options: ["Ya", "Tidak"],
                        },
                        {
                            id: "6d8b75fa-d87d-46b1-98ef-b47df8d3c041",
                            symptom_id: "b82fb6e4-7656-42e7-8e22-c4e9a94d5466",
                            question: "Sejak kapan gejala tersebut muncul?",
                            type: "duration",
                            options: [
                                "< 6 jam",
                                "6-12 jam",
                                "12-24 jam",
                                "> 24 jam",
                            ],
                        },
                    ],
                },
                {
                    id: "cd3cdba4-a506-4e23-86c8-f29dca52d0fa",
                    index: 1,
                    name: "Batuk",
                    description: "Batuk kering atau berdahak",
                    category: "respiratory",
                    question: "Apakah balita mengalami batuk?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        {
                            id: "44dc4a46-58de-468b-b219-09aa2d1e85f4",
                            symptom_id: "cd3cdba4-a506-4e23-86c8-f29dca52d0fa",
                            question: "Berapa lama balita sudah batuk?",
                            type: "duration",
                            options: [
                                "< 3 hari",
                                "3-7 hari",
                                "1-2 minggu",
                                "> 2 minggu",
                            ],
                        },
                        {
                            id: "eacfad14-3805-4a06-a911-6f4dadf52949",
                            symptom_id: "cd3cdba4-a506-4e23-86c8-f29dca52d0fa",
                            question: "Jenis batuk apa yang dialami?",
                            type: "generic",
                            options: [
                                "Batuk kering",
                                "Batuk berdahak",
                                "Batuk menggonggong",
                                "Batuk berdarah",
                            ],
                        },
                    ],
                },
                {
                    id: "c52a1252-0efb-4bb2-af64-bbb3372895ee",
                    index: 2,
                    name: "Demam",
                    description: "Suhu tubuh di atas 37.5°C",
                    category: "general",
                    question: "Apakah balita mengalami demam?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "1a4e5c1b-f9d2-4c64-90f2-21a5f680d91b",
                    index: 3,
                    name: "Sesak Napas",
                    description: "Kesulitan bernapas atau napas cepat",
                    category: "respiratory",
                    question:
                        "Apakah balita mengalami sesak napas atau napas cepat?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        {
                            id: "7abfba99-8f69-4e18-a7e3-50d57db6bb2d",
                            symptom_id: "1a4e5c1b-f9d2-4c64-90f2-21a5f680d91b",
                            question:
                                "Apakah dada anak terlihat tertarik ke dalam saat bernapas?",
                            type: "yes/no",
                            options: ["Ya", "Tidak"],
                        },
                    ],
                },
                {
                    id: "a1b2c3d4-e5f6-7890-1234-5678f9g0h1i2",
                    index: 4,
                    name: "Diare",
                    description:
                        "Buang air besar encer lebih dari 3 kali sehari",
                    category: "digestive",
                    question: "Apakah balita mengalami diare?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        {
                            id: "j1k2l3m4-n5o6-7890-1234-5678p9q0r1s2",
                            symptom_id: "a1b2c3d4-e5f6-7890-1234-5678f9g0h1i2",
                            question: "Berapa lama balita sudah diare?",
                            type: "duration",
                            options: [
                                "< 1 hari",
                                "1-2 hari",
                                "3-5 hari",
                                "> 5 hari",
                            ],
                        },
                        {
                            id: "t3u4v5w6-x7y8-9012-3456-7890z1a2b3c4",
                            symptom_id: "a1b2c3d4-e5f6-7890-1234-5678f9g0h1i2",
                            question: "Apakah ada darah dalam tinja?",
                            type: "yes/no",
                            options: ["Ya", "Tidak"],
                        },
                    ],
                },
                {
                    id: "d5e6f7g8-h9i0-1234-5678-9012j3k4l5m6",
                    index: 5,
                    name: "Nyeri Perut",
                    description: "Rasa sakit atau tidak nyaman di area perut",
                    category: "digestive",
                    question: "Apakah balita mengeluh nyeri perut?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "n7o8p9q0-r1s2-3456-7890-1234t5u6v7w8",
                    index: 6,
                    name: "Nafsu Makan Berkurang",
                    description: "Penurunan keinginan untuk makan",
                    category: "general",
                    question: "Apakah balita mengalami penurunan nafsu makan?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "x9y0z1a2-b3c4-5678-9012-3456d7e8f9g0",
                    index: 7,
                    name: "Ruam Kulit",
                    description:
                        "Perubahan warna atau tekstur kulit yang tidak normal",
                    category: "skin",
                    question: "Apakah balita memiliki ruam kulit?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        {
                            id: "h1i2j3k4-l5m6-7890-1234-5678n9o0p1q2",
                            symptom_id: "x9y0z1a2-b3c4-5678-9012-3456d7e8f9g0",
                            question: "Di mana lokasi ruam kulit?",
                            type: "generic",
                            options: [
                                "Wajah",
                                "Tangan",
                                "Kaki",
                                "Seluruh tubuh",
                            ],
                        },
                    ],
                },
                {
                    id: "r3s4t5u6-v7w8-9012-3456-7890x1y2z3a4",
                    index: 8,
                    name: "Lesu dan Kurang Energi",
                    description: "Kurang aktif dan mudah lelah",
                    category: "general",
                    question: "Apakah balita terlihat lesu atau kurang energi?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "m1n2o3p4-q5r6-7890-1234-5678s9t0u1v2",
                    index: 9,
                    name: "Mual dan Muntah",
                    description:
                        "Rasa ingin muntah dan/atau mengeluarkan isi lambung",
                    category: "digestive",
                    question: "Apakah balita mengalami mual atau muntah?",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        {
                            id: "w3x4y5z6-a7b8-9012-3456-7890c1d2e3f4",
                            symptom_id: "m1n2o3p4-q5r6-7890-1234-5678s9t0u1v2",
                            question: "Seberapa sering muntah terjadi?",
                            type: "frequency",
                            options: [
                                "1-2 kali",
                                "3-5 kali",
                                "Lebih dari 5 kali",
                                "Terus menerus",
                            ],
                        },
                    ],
                },
            ],
            diseases: [
                {
                    id: "c1df88b0-b505-4bcc-ab2c-ccf39fc9e8cc",
                    name: "Kondisi Darurat",
                    description:
                        "Kondisi yang memerlukan penanganan medis segera",
                    severity: "severe",
                    treatment:
                        "SEGERA BAWA KE RUMAH SAKIT/UGD - JANGAN DITUNDA!",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "c8302689-547d-4792-b3b5-8839776606f7",
                    name: "Influenza (Flu)",
                    description:
                        "Infeksi virus yang menyerang sistem pernapasan",
                    severity: "mild",
                    treatment:
                        "Istirahat cukup, banyak minum air putih, kompres hangat, konsultasi dokter jika demam tinggi",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "f901abb3-2a9f-4a92-8c91-6aaac37f2bc3",
                    name: "Pneumonia",
                    description:
                        "Infeksi paru yang menyebabkan kesulitan bernapas",
                    severity: "severe",
                    treatment:
                        "Segera bawa ke fasilitas kesehatan. Berikan cairan dan pantau suhu tubuh.",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "b5c6d7e8-f9g0-1234-5678-9012h3i4j5k6",
                    name: "Gastroenteritis",
                    description: "Infeksi saluran pencernaan",
                    severity: "moderate",
                    treatment:
                        "Berikan oralit, jaga hidrasi, makanan lunak, konsultasi dokter jika gejala memburuk",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "l7m8n9o0-p1q2-3456-7890-1234r5s6t7u8",
                    name: "Alergi Makanan",
                    description: "Reaksi sistem imun terhadap makanan tertentu",
                    severity: "moderate",
                    treatment:
                        "Hindari makanan pemicu, berikan antihistamin jika diperlukan, konsultasi dokter",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "v9w0x1y2-z3a4-5678-9012-3456b7c8d9e0",
                    name: "Infeksi Virus",
                    description: "Infeksi umum yang disebabkan oleh virus",
                    severity: "mild",
                    treatment:
                        "Istirahat cukup, banyak minum, pantau gejala, konsultasi dokter jika demam tinggi atau gejala memburuk",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    id: "k5l6m7n8-o9p0-1234-5678-9012q3r4s5t6",
                    name: "Keracunan Makanan",
                    description:
                        "Gangguan pencernaan akibat makanan terkontaminasi",
                    severity: "moderate",
                    treatment:
                        "Jaga hidrasi, hindari makanan padat sementara, konsultasi dokter jika gejala berat",
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
            ],
            rules: [
                {
                    disease_id: "c1df88b0-b505-4bcc-ab2c-ccf39fc9e8cc",
                    confidence: 100,
                    symptoms_id: ["b82fb6e4-7656-42e7-8e22-c4e9a94d5466"],
                    follow_up_questions: [],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                },
                {
                    disease_id: "c8302689-547d-4792-b3b5-8839776606f7",
                    confidence: 75,
                    symptoms_id: [
                        "cd3cdba4-a506-4e23-86c8-f29dca52d0fa",
                        "c52a1252-0efb-4bb2-af64-bbb3372895ee",
                    ],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        "Jenis batuk apa yang dialami?: Batuk kering",
                        "Berapa lama balita sudah batuk?: < 3 hari",
                    ],
                },
                {
                    disease_id: "f901abb3-2a9f-4a92-8c91-6aaac37f2bc3",
                    confidence: 75,
                    symptoms_id: [
                        "1a4e5c1b-f9d2-4c64-90f2-21a5f680d91b",
                        "c52a1252-0efb-4bb2-af64-bbb3372895ee",
                    ],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        "Apakah dada anak terlihat tertarik ke dalam saat bernapas?: Ya",
                    ],
                },
                {
                    disease_id: "b5c6d7e8-f9g0-1234-5678-9012h3i4j5k6",
                    confidence: 80,
                    symptoms_id: [
                        "a1b2c3d4-e5f6-7890-1234-5678f9g0h1i2",
                        "d5e6f7g8-h9i0-1234-5678-9012j3k4l5m6",
                        "n7o8p9q0-r1s2-3456-7890-1234t5u6v7w8",
                        "m1n2o3p4-q5r6-7890-1234-5678s9t0u1v2",
                    ],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        "Berapa lama balita sudah diare?: 1-2 hari",
                        "Apakah ada darah dalam tinja?: Tidak",
                        "Seberapa sering muntah terjadi?: 3-5 kali",
                    ],
                },
                {
                    disease_id: "l7m8n9o0-p1q2-3456-7890-1234r5s6t7u8",
                    confidence: 70,
                    symptoms_id: [
                        "x9y0z1a2-b3c4-5678-9012-3456d7e8f9g0",
                        "d5e6f7g8-h9i0-1234-5678-9012j3k4l5m6",
                        "n7o8p9q0-r1s2-3456-7890-1234t5u6v7w8",
                    ],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: ["Di mana lokasi ruam kulit?: Wajah"],
                },
                {
                    disease_id: "v9w0x1y2-z3a4-5678-9012-3456b7c8d9e0",
                    confidence: 65,
                    symptoms_id: [
                        "c52a1252-0efb-4bb2-af64-bbb3372895ee",
                        "r3s4t5u6-v7w8-9012-3456-7890x1y2z3a4",
                        "n7o8p9q0-r1s2-3456-7890-1234t5u6v7w8",
                    ],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [],
                },
                {
                    disease_id: "k5l6m7n8-o9p0-1234-5678-9012q3r4s5t6",
                    confidence: 75,
                    symptoms_id: [
                        "m1n2o3p4-q5r6-7890-1234-5678s9t0u1v2",
                        "d5e6f7g8-h9i0-1234-5678-9012j3k4l5m6",
                        "r3s4t5u6-v7w8-9012-3456-7890x1y2z3a4",
                    ],
                    created_by_id: "a1a2fdf2-6206-4dcb-a7a1-e1cbf591c469",
                    follow_up_questions: [
                        "Seberapa sering muntah terjadi?: 3-5 kali",
                    ],
                },
            ],
        };

        const followUpQuestions = [];
        def.symptoms.map((item) => {
            if (item.follow_up_questions) {
                followUpQuestions.push(...item?.follow_up_questions);
            }
        });

        const users = await User.bulkCreate(def.users);
        const symptoms = await Symptom.bulkCreate(def.symptoms);
        const diseases = await Disease.bulkCreate(def.diseases);
        const rules = await Rule.bulkCreate(def.rules);
        const follow_up_questions =
            await FollowUpQuestion.bulkCreate(followUpQuestions);

        res.status(200).json({
            success: true,
            data: {
                users,
                symptoms,
                diseases,
                rules,
                follow_up_questions,
            },
        });
    } catch (err) {
        next(err);
    }
};
