import db from '../database/models/index.js';

const seedInterviewTemplates = async () => {
    try {
        console.log('--- Seeding Missing Interview Templates ---');

        const templates = [
            {
                templateCode: 'INTERVIEW_SCHEDULED_APPLICANT',
                description: 'Notification sent to the applicant when an interview is scheduled',
                subject: 'Interview Scheduled: {{applicationId}}',
                body: `
                    <html><body>
                        <h2>Dear {{applicantName}},</h2>
                        <p>Your <strong>{{type}}</strong> for Royal Enfield Dealership Application ({{applicationId}}) has been scheduled.</p>
                        <p><strong>Scheduled Time:</strong> {{scheduledAt}}</p>
                        <p><strong>Meeting Link/Location:</strong> {{meetLink}}</p>
                        <p>Please ensure you are available at the scheduled time.</p>
                        <div style="margin: 20px 0;">
                            <a href="{{meetLink}}" style="background: #c8102e; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin-right: 10px;">Join Meeting</a>
                            <a href="{{appLink}}" style="background: #f4f4f4; color: #333; padding: 10px 20px; text-decoration: none; border-radius: 5px; border: 1px solid #ccc;">View Application</a>
                        </div>
                        <p>Best Regards,<br>Royal Enfield Onboarding Team</p>
                    </body></html>
                `,
                placeholders: ['applicantName', 'applicationId', 'type', 'scheduledAt', 'meetLink', 'appLink']
            },
            {
                templateCode: 'INTERVIEW_SCHEDULED_PANELIST',
                description: 'Notification sent to the panelist (employee) when assigned an interview',
                subject: 'New Interview Assignment: {{applicationId}}',
                body: `
                    <html><body>
                        <h2>Hi {{panelistName}},</h2>
                        <p>You have been assigned as a panelist for <strong>{{type}}</strong> with <strong>{{applicantName}}</strong>.</p>
                        <p><strong>Application ID:</strong> {{applicationId}}</p>
                        <p><strong>Scheduled Time:</strong> {{scheduledAt}}</p>
                        <p><strong>Meeting Link/Location:</strong> {{meetLink}}</p>
                        <div style="margin: 20px 0;">
                            <a href="{{meetLink}}" style="background: #c8102e; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin-right: 10px;">Join Meeting</a>
                            <a href="{{appLink}}" style="background: #f4f4f4; color: #333; padding: 10px 20px; text-decoration: none; border-radius: 5px; border: 1px solid #ccc;">View Application</a>
                        </div>
                        <p>Please review the applicant's profile before the session.</p>
                        <p>Regards,<br>System Administrator</p>
                    </body></html>
                `,
                placeholders: ['panelistName', 'applicantName', 'applicationId', 'type', 'scheduledAt', 'meetLink', 'appLink']
            },
            {
                templateCode: 'INTERVIEW_RESCHEDULED_APPLICANT',
                description: 'Notification sent to the applicant when an interview is rescheduled',
                subject: 'Interview Rescheduled: {{applicationId}}',
                body: `
                    <html><body>
                        <h2>Dear {{applicantName}},</h2>
                        <p>Your <strong>{{type}}</strong> for Royal Enfield Dealership Application ({{applicationId}}) has been <strong>rescheduled</strong>.</p>
                        <p><strong>New Scheduled Time:</strong> {{scheduledAt}}</p>
                        <p><strong>Meeting Link/Location:</strong> {{meetLink}}</p>
                        <p>Please ensure you are available at the new scheduled time.</p>
                        <div style="margin: 20px 0;">
                            <a href="{{meetLink}}" style="background: #c8102e; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin-right: 10px;">Join Meeting</a>
                            <a href="{{appLink}}" style="background: #f4f4f4; color: #333; padding: 10px 20px; text-decoration: none; border-radius: 5px; border: 1px solid #ccc;">View Application</a>
                        </div>
                        <p>Best Regards,<br>Royal Enfield Onboarding Team</p>
                    </body></html>
                `,
                placeholders: ['applicantName', 'applicationId', 'type', 'scheduledAt', 'meetLink', 'appLink']
            },
            {
                templateCode: 'INTERVIEW_RESCHEDULED_PANELIST',
                description: 'Notification sent to the panelist when an interview is rescheduled',
                subject: 'Interview Rescheduled: {{applicationId}}',
                body: `
                    <html><body>
                        <h2>Hi {{panelistName}},</h2>
                        <p>The <strong>{{type}}</strong> for <strong>{{applicantName}}</strong> ({{applicationId}}) has been <strong>rescheduled</strong>.</p>
                        <p><strong>New Scheduled Time:</strong> {{scheduledAt}}</p>
                        <p><strong>Meeting Link/Location:</strong> {{meetLink}}</p>
                        <div style="margin: 20px 0;">
                            <a href="{{meetLink}}" style="background: #c8102e; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px; margin-right: 10px;">Join Meeting</a>
                            <a href="{{appLink}}" style="background: #f4f4f4; color: #333; padding: 10px 20px; text-decoration: none; border-radius: 5px; border: 1px solid #ccc;">View Application</a>
                        </div>
                        <p>Please update your calendar accordingly.</p>
                        <p>Regards,<br>System Administrator</p>
                    </body></html>
                `,
                placeholders: ['panelistName', 'applicantName', 'applicationId', 'type', 'scheduledAt', 'meetLink', 'appLink']
            },
            {
                templateCode: 'INTERVIEW_CANCELLED_APPLICANT',
                description: 'Notification sent to the applicant when an interview is cancelled',
                subject: 'Interview Cancelled: {{applicationId}}',
                body: `
                    <html><body>
                        <h2>Dear {{applicantName}},</h2>
                        <p>We inform you that your <strong>{{type}}</strong> for Royal Enfield Dealership Application ({{applicationId}}) has been <strong>cancelled</strong>.</p>
                        <p>Our team will reach out to you if a new session is required.</p>
                        <p>Best Regards,<br>Royal Enfield Onboarding Team</p>
                    </body></html>
                `,
                placeholders: ['applicantName', 'applicationId', 'type']
            },
            {
                templateCode: 'INTERVIEW_CANCELLED_PANELIST',
                description: 'Notification sent to the panelist when an interview is cancelled',
                subject: 'Interview Cancelled: {{applicationId}}',
                body: `
                    <html><body>
                        <h2>Hi {{panelistName}},</h2>
                        <p>The <strong>{{type}}</strong> for <strong>{{applicantName}}</strong> ({{applicationId}}) has been <strong>cancelled</strong>.</p>
                        <p>You no longer need to attend this session.</p>
                        <p>Regards,<br>System Administrator</p>
                    </body></html>
                `,
                placeholders: ['panelistName', 'applicantName', 'applicationId', 'type']
            },
            {
                templateCode: 'WORKFLOW_ACTION_REQUIRED',
                description: 'Notification for stakeholders when their action is required in a workflow',
                subject: 'Action Required: {{requestId}} — {{targetStage}}',
                body: `
                    <html><body>
                        <h2>Hi {{recipientName}},</h2>
                        <p>Application <strong>{{requestId}}</strong> (Applicant: {{dealerName}}) has reached the <strong>{{targetStage}}</strong> stage and requires your action/review.</p>
                        <div style="margin: 20px 0;">
                            <a href="{{link}}" style="background: #c8102e; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px;">View Application</a>
                        </div>
                        <p>Regards,<br>Royal Enfield Workflow System</p>
                    </body></html>
                `,
                placeholders: ['recipientName', 'requestId', 'dealerName', 'targetStage', 'link', 'ctaLabel']
            },
            {
                templateCode: 'WORKFLOW_STATUS_UPDATE_DEALER',
                description: 'Milestone update notification for the dealer/applicant',
                subject: 'Update on your Request: {{requestId}}',
                body: `
                    <html><body>
                        <h2>Dear {{dealerName}},</h2>
                        <p>Your request <strong>{{requestId}}</strong> has been updated to: <strong>{{targetStage}}</strong>.</p>
                        <p>You can track the live progress here: <a href="{{link}}">View Tracking</a></p>
                        <p>Best Regards,<br>Royal Enfield Team</p>
                    </body></html>
                `,
                placeholders: ['requestId', 'dealerName', 'targetStage', 'link']
            }

        ];

        for (const t of templates) {
            await db.EmailTemplate.upsert({
                ...t,
                isActive: true
            });
            console.log(`Successfully seeded/updated: ${t.templateCode}`);
        }

        console.log('--- Interview Templates Seeded Successfully ---');
        process.exit(0);
    } catch (error) {
        console.error('Error seeding interview templates:', error);
        process.exit(1);
    }
};

seedInterviewTemplates();
