import 'dotenv/config';
import db from './src/database/models/index.js';
import { v4 as uuidv4 } from 'uuid';

async function restoreQuestionnaire() {
    console.log('🌱 Restoring Full Questionnaire (24 questions)...');
    try {
        await db.sequelize.authenticate();

        // 1. Deactivate old versions
        await db.Questionnaire.update({ isActive: false }, { where: { isActive: true } });

        // 2. Create the Questionnaire
        const questionnaire = await db.Questionnaire.create({
            id: uuidv4(),
            version: 'V2.0-Restored',
            isActive: true
        });

        const questions = [
            { text: "Email", type: "email", section: "Basic Information", weight: 0, order: 1 },
            { text: "Name", type: "text", section: "Basic Information", weight: 0, order: 2 },
            { text: "Current Location", type: "text", section: "Basic Information", weight: 0, order: 3 },
            { text: "Location Applied For", type: "text", section: "Basic Information", weight: 0, order: 4 },
            {
                text: "State (Applied for)",
                type: "select",
                section: "Basic Information",
                weight: 0,
                order: 5,
                options: ["Andaman & Nicobar", "Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar", "Chandigarh", "Chhattisgarh", "Delhi & NCR", "Goa", "Gujarat", "Himachal Pradesh", "Haryana", "Jammu & Kashmir", "Jharkhand", "Karnataka", "Kerala", "Ladakh", "Madhya Pradesh", "Maharashtra", "Mizoram", "Meghalaya", "Manipur", "Nagaland", "Odisha", "Puducherry", "Punjab", "Rajasthan", "Sikkim", "Tamilnadu", "Telangana", "Tripura", "Uttar Pradesh", "Uttarakhand", "West Bengal"].map(s => ({ text: s, score: 0 }))
            },
            { text: "Contact Number", type: "text", section: "Basic Information", weight: 0, order: 6 },
            { text: "Age", type: "number", section: "Basic Information", weight: 0, order: 7 },
            {
                text: "Educational Qualification",
                type: "radio",
                section: "Profile & Background",
                weight: 5,
                order: 8,
                options: [
                    { text: "Under Graduate", score: 2 },
                    { text: "Graduate", score: 3 },
                    { text: "Post Graduate", score: 5 }
                ]
            },
            {
                text: "What is your Personal Networth",
                type: "select",
                section: "Financials",
                weight: 15,
                order: 9,
                options: [
                    { text: "Less than 2 Crores", score: 3 },
                    { text: "Between 2 - 5 Crores", score: 6 },
                    { text: "Between 5 - 10 Crores", score: 9 },
                    { text: "Between 10 - 15 Crores", score: 12 },
                    { text: "Greater than 15 Crores", score: 15 }
                ]
            },
            {
                text: "Are you a native of the Proposed Location?",
                type: "radio",
                section: "Location",
                weight: 10,
                order: 10,
                options: [
                    { text: "Native", score: 10 },
                    { text: "Willing to Relocate", score: 5 },
                    { text: "Will manage Remotely", score: 0 }
                ]
            },
            { text: "Proposed Location Photos (If any)", type: "file", section: "Location", weight: 0, order: 11 },
            {
                text: "Why do you want to partner with Royal Enfield?",
                type: "radio",
                section: "Strategy",
                weight: 5,
                order: 12,
                options: [
                    { text: "Absence of Royal Enfield in the particular location and presence of opportunity", score: 2 },
                    { text: "Passionate about the brand", score: 2 },
                    { text: "Experience in the automobile business and would like to expand with Royal Enfield", score: 5 }
                ]
            },
            {
                text: "Who will be the partners in proposed company?",
                type: "radio",
                section: "Business Structure",
                weight: 5,
                order: 13,
                options: [
                    { text: "Immediate Family", score: 5 },
                    { text: "Extended Family", score: 3 },
                    { text: "Friends", score: 2 },
                    { text: "Proprietorship", score: 5 }
                ]
            },
            {
                text: "Who will be managing the Royal Enfield dealership",
                type: "radio",
                section: "Business Structure",
                weight: 10,
                order: 14,
                options: [
                    { text: "I will be managing full time", score: 10 },
                    { text: "I will be managing with my partners", score: 7 },
                    { text: "I will hire a manager full time and oversee the operations", score: 5 }
                ]
            },
            {
                text: "Proposed Firm Type",
                type: "radio",
                section: "Business Structure",
                weight: 5,
                order: 15,
                options: [
                    { text: "Proprietorship", score: 3 },
                    { text: "Partnership", score: 3 },
                    { text: "Limited Liability partnership", score: 3 },
                    { text: "Private Limited Company", score: 5 }
                ]
            },
            {
                text: "What are you currently doing?",
                type: "radio",
                section: "Experience",
                weight: 10,
                order: 16,
                options: [
                    { text: "Running automobile dealership", score: 10 },
                    { text: "Running another business not in automobile field", score: 5 },
                    { text: "Presently working, willing to resign and handle business", score: 5 },
                    { text: "Currently not working and looking for business opportunities", score: 2 }
                ]
            },
            {
                text: "Do you own a property in proposed location?",
                type: "radio",
                section: "Location",
                weight: 10,
                order: 17,
                options: [
                    { text: "Yes", score: 10 },
                    { text: "No, will rent a location in the desired location", score: 5 }
                ]
            },
            {
                text: "How are you planning to invest in the Royal Enfield business",
                type: "radio",
                section: "Financials",
                weight: 10,
                order: 18,
                options: [
                    { text: "I will be investing my own funds", score: 10 },
                    { text: "I will invest partially and get the rest from the bank", score: 7 },
                    { text: "I will be requiring complete funds from the bank", score: 3 }
                ]
            },
            {
                text: "What are your plans of expansion with RE?",
                type: "radio",
                section: "Strategy",
                weight: 5,
                order: 19,
                options: [
                    { text: "Willing to expand with the help of partners", score: 2 },
                    { text: "Willing to expand by myself", score: 5 },
                    { text: "No plans for expansion", score: 0 }
                ]
            },
            {
                text: "Will you be expanding to any other automobile OEM in the future?",
                type: "radio",
                section: "Strategy",
                weight: 4,
                order: 20,
                options: [
                    { text: "Yes", score: 0 },
                    { text: "No", score: 4 }
                ]
            },
            {
                text: "Do you own a Royal Enfield ?",
                type: "radio",
                section: "Brand Loyalty",
                weight: 3,
                order: 21,
                options: [
                    { text: "Yes, it is registered in my name", score: 3 },
                    { text: "Yes, it is registered to my immediate family member", score: 2 },
                    { text: "Not at the moment but owned it earlier", score: 1 },
                    { text: "No", score: 0 }
                ]
            },
            {
                text: "Do you go for long leisure rides",
                type: "radio",
                section: "Brand Loyalty",
                weight: 3,
                order: 22,
                options: [
                    { text: "Yes, with the Royal Enfield riders", score: 3 },
                    { text: "Yes, with other brands", score: 2 },
                    { text: "No", score: 0 }
                ]
            },
            { text: "What special initiatives do you plan to implement if selected as business partner for Royal Enfield ?", type: "textarea", section: "Strategy", weight: 0, order: 23 },
            { text: "Please elaborate your present business/employment.", type: "textarea", section: "Experience", weight: 0, order: 24 }
        ];

        for (const q of questions) {
            const question = await db.QuestionnaireQuestion.create({
                id: uuidv4(),
                questionnaireId: questionnaire.id,
                sectionName: q.section,
                questionText: q.text,
                inputType: q.type,
                weight: q.weight,
                order: q.order,
                isMandatory: true
            });

            if (q.options) {
                const optionsWithId = q.options.map((opt, idx) => ({
                    id: uuidv4(),
                    questionId: question.id,
                    optionText: opt.text,
                    score: opt.score,
                    order: idx + 1
                }));
                const OptionModel = db.QuestionnaireOption;
                await OptionModel.bulkCreate(optionsWithId);
            }
        }

        console.log('✅ Full Questionnaire restored successfully!');
        process.exit(0);
    } catch (error) {
        console.error('❌ Restoration failed:', error);
        process.exit(1);
    }
}

restoreQuestionnaire();
