import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

const templates = [
  {
    code: 'APPROVAL_TASK_CREATED',
    name: '审批任务创建通知',
    description: '当有新的审批任务分配时发送',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】您有一个新的审批任务 - {{taskName}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #007bff; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">新审批任务</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{approverName}}</strong>，</p>
    <p>您有一个新的审批任务待处理：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #007bff;">
      <p style="margin: 10px 0;"><strong>任务名称：</strong>{{taskName}}</p>
      <p style="margin: 10px 0;"><strong>提交人：</strong>{{submitter}}</p>
      <p style="margin: 10px 0;"><strong>提交时间：</strong>{{submitTime}}</p>
      {{#if amount}}
      <p style="margin: 10px 0;"><strong>金额：</strong>{{formatCurrency amount}}</p>
      {{/if}}
    </div>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #007bff; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        立即处理
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请勿回复。<br>
      如有疑问，请联系系统管理员。
    </p>
  </div>
</div>
    `,
    variables: ['approverName', 'taskName', 'submitter', 'submitTime', 'amount', 'link'],
    example: {
      approverName: '张三',
      taskName: '报销审批',
      submitter: '李四',
      submitTime: '2024-11-15 10:30',
      amount: 500,
      link: 'https://example.com/approval/123',
    },
    priority: 'NORMAL',
    isActive: true,
  },
  {
    code: 'APPROVAL_TASK_APPROVED',
    name: '审批通过通知',
    description: '当审批任务被批准时通知提交人',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】您的审批已通过 - {{taskName}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #28a745; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">✓ 审批已通过</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{submitterName}}</strong>，</p>
    <p>您的审批申请已通过：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #28a745;">
      <p style="margin: 10px 0;"><strong>任务名称：</strong>{{taskName}}</p>
      <p style="margin: 10px 0;"><strong>审批人：</strong>{{approver}}</p>
      <p style="margin: 10px 0;"><strong>审批时间：</strong>{{approvalTime}}</p>
      {{#if comment}}
      <p style="margin: 10px 0;"><strong>审批意见：</strong>{{comment}}</p>
      {{/if}}
    </div>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #28a745; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        查看详情
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请勿回复。
    </p>
  </div>
</div>
    `,
    variables: ['submitterName', 'taskName', 'approver', 'approvalTime', 'comment', 'link'],
    example: {
      submitterName: '李四',
      taskName: '报销审批',
      approver: '张三',
      approvalTime: '2024-11-15 11:30',
      comment: '同意',
      link: 'https://example.com/approval/123',
    },
    priority: 'NORMAL',
    isActive: true,
  },
  {
    code: 'APPROVAL_TASK_REJECTED',
    name: '审批驳回通知',
    description: '当审批任务被驳回时通知提交人',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】您的审批被驳回 - {{taskName}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #dc3545; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">✗ 审批被驳回</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{submitterName}}</strong>，</p>
    <p>您的审批申请被驳回：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #dc3545;">
      <p style="margin: 10px 0;"><strong>任务名称：</strong>{{taskName}}</p>
      <p style="margin: 10px 0;"><strong>审批人：</strong>{{approver}}</p>
      <p style="margin: 10px 0;"><strong>驳回时间：</strong>{{rejectTime}}</p>
      <p style="margin: 10px 0;"><strong>驳回原因：</strong><span style="color: #dc3545;">{{reason}}</span></p>
    </div>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #dc3545; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        查看详情
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请勿回复。
    </p>
  </div>
</div>
    `,
    variables: ['submitterName', 'taskName', 'approver', 'rejectTime', 'reason', 'link'],
    example: {
      submitterName: '李四',
      taskName: '报销审批',
      approver: '张三',
      rejectTime: '2024-11-15 11:30',
      reason: '缺少发票',
      link: 'https://example.com/approval/123',
    },
    priority: 'NORMAL',
    isActive: true,
  },
  {
    code: 'SYSTEM_ALERT',
    name: '系统告警通知',
    description: '系统异常或重要事件告警',
    channel: 'EMAIL',
    subject: '【FF AI Workspace 告警】{{alertTitle}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #ffc107; color: #212529; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">⚠️ 系统告警</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <h3 style="color: #ffc107; margin-top: 0;">{{alertTitle}}</h3>
    <p>{{alertMessage}}</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; font-family: monospace; font-size: 12px;">
      <pre style="margin: 0; white-space: pre-wrap; word-wrap: break-word;">{{details}}</pre>
    </div>
    
    <p><strong>发生时间：</strong>{{timestamp}}</p>
    <p><strong>严重级别：</strong><span style="color: #dc3545;">{{severity}}</span></p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请立即处理。
    </p>
  </div>
</div>
    `,
    variables: ['alertTitle', 'alertMessage', 'details', 'timestamp', 'severity'],
    example: {
      alertTitle: '审计日志完整性检查失败',
      alertMessage: '发现 5 条审计日志记录的哈希链被破坏',
      details: 'Log IDs: abc123, def456, ghi789...',
      timestamp: '2024-11-15 02:00:00',
      severity: 'HIGH',
    },
    priority: 'HIGH',
    isActive: true,
  },
  // ========== 加班通知模板 ==========
  {
    code: 'OVERTIME_WEEKLY_REMINDER',
    name: '周加班提醒',
    description: '每周一提醒员工提交上周加班记录',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】请提交上周加班记录（{{weekYear}}-W{{weekNumber}}）',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #1890ff; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">⏰ 加班记录提交提醒</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{userName}}</strong>，</p>
    <p>请及时提交您的上周加班记录：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #1890ff;">
      <p style="margin: 10px 0;"><strong>周期：</strong>{{weekYear}} 年第 {{weekNumber}} 周</p>
      <p style="margin: 10px 0;"><strong>截止时间：</strong>{{deadlineDate}}</p>
      <p style="margin: 10px 0; color: #666;">请确保在截止时间前提交，以便主管及时审批。</p>
    </div>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #1890ff; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        立即提交
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请勿回复。
    </p>
  </div>
</div>
    `,
    variables: ['userName', 'weekYear', 'weekNumber', 'deadlineDate', 'link'],
    example: {
      userName: '张三',
      weekYear: 2024,
      weekNumber: 46,
      deadlineDate: '2024-11-18',
      link: 'https://example.com/overtime/submit',
    },
    priority: 'NORMAL',
    isActive: true,
  },
  {
    code: 'OVERTIME_APPROVAL_REQUIRED',
    name: '加班审批通知',
    description: '通知主管有新的加班审批',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】{{employeeName}} 提交了加班审批 - {{weekYear}}-W{{weekNumber}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #ff9800; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">📋 新的加班审批</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{approverName}}</strong>，</p>
    <p>您有一个新的加班审批待处理：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #ff9800;">
      <p style="margin: 10px 0;"><strong>员工：</strong>{{employeeName}}</p>
      <p style="margin: 10px 0;"><strong>部门：</strong>{{department}}</p>
      <p style="margin: 10px 0;"><strong>周期：</strong>{{weekYear}} 年第 {{weekNumber}} 周</p>
      <p style="margin: 10px 0;"><strong>总时长：</strong>{{totalHours}} 小时</p>
      <p style="margin: 10px 0;"><strong>天数：</strong>{{totalDays}} 天</p>
      {{#if hasAbnormal}}
      <p style="margin: 10px 0; color: #ff4d4f;"><strong>⚠️ 注意：</strong>存在异常加班记录</p>
      {{/if}}
    </div>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #ff9800; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        立即审批
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      请在 48 小时内完成审批 · 此邮件由 FF AI Workspace 系统自动发送
    </p>
  </div>
</div>
    `,
    variables: ['approverName', 'employeeName', 'department', 'weekYear', 'weekNumber', 'totalHours', 'totalDays', 'hasAbnormal', 'link'],
    example: {
      approverName: '李经理',
      employeeName: '张三',
      department: '研发部',
      weekYear: 2024,
      weekNumber: 46,
      totalHours: 40,
      totalDays: 5,
      hasAbnormal: false,
      link: 'https://example.com/overtime/approvals',
    },
    priority: 'NORMAL',
    isActive: true,
  },
  {
    code: 'OVERTIME_APPROVED',
    name: '加班审批通过通知',
    description: '通知员工加班审批已通过',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】您的加班已审批通过 - {{weekYear}}-W{{weekNumber}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #28a745; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">✓ 审批通过</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{employeeName}}</strong>，</p>
    <p>您的加班申请已通过审批：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #28a745;">
      <p style="margin: 10px 0;"><strong>周期：</strong>{{weekYear}} 年第 {{weekNumber}} 周</p>
      <p style="margin: 10px 0;"><strong>总时长：</strong>{{totalHours}} 小时</p>
      <p style="margin: 10px 0;"><strong>审批人：</strong>{{approverName}}</p>
      <p style="margin: 10px 0;"><strong>审批时间：</strong>{{approvalTime}}</p>
      {{#if comment}}
      <p style="margin: 10px 0;"><strong>审批意见：</strong>{{comment}}</p>
      {{/if}}
    </div>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #28a745; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        查看详情
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请勿回复。
    </p>
  </div>
</div>
    `,
    variables: ['employeeName', 'weekYear', 'weekNumber', 'totalHours', 'approverName', 'approvalTime', 'comment', 'link'],
    example: {
      employeeName: '张三',
      weekYear: 2024,
      weekNumber: 46,
      totalHours: 40,
      approverName: '李经理',
      approvalTime: '2024-11-15 15:30',
      comment: '同意',
      link: 'https://example.com/overtime/my-records',
    },
    priority: 'NORMAL',
    isActive: true,
  },
  {
    code: 'OVERTIME_REJECTED',
    name: '加班审批驳回通知',
    description: '通知员工加班审批被驳回',
    channel: 'EMAIL',
    subject: '【FF AI Workspace】您的加班被驳回 - {{weekYear}}-W{{weekNumber}}',
    template: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
  <div style="background-color: #dc3545; color: white; padding: 20px; border-radius: 5px 5px 0 0;">
    <h2 style="margin: 0;">✗ 审批驳回</h2>
  </div>
  
  <div style="background-color: #f8f9fa; padding: 20px; border: 1px solid #dee2e6; border-top: none;">
    <p>尊敬的 <strong>{{employeeName}}</strong>，</p>
    <p>您的加班申请被驳回：</p>
    
    <div style="background-color: white; padding: 15px; margin: 20px 0; border-radius: 5px; border-left: 4px solid #dc3545;">
      <p style="margin: 10px 0;"><strong>周期：</strong>{{weekYear}} 年第 {{weekNumber}} 周</p>
      <p style="margin: 10px 0;"><strong>审批人：</strong>{{approverName}}</p>
      <p style="margin: 10px 0;"><strong>驳回时间：</strong>{{rejectTime}}</p>
      <p style="margin: 10px 0;"><strong>驳回原因：</strong><span style="color: #dc3545;">{{reason}}</span></p>
    </div>
    
    <p>请修改后重新提交，或联系您的主管。</p>
    
    <p style="text-align: center; margin: 30px 0;">
      <a href="{{link}}" style="display: inline-block; padding: 12px 30px; background-color: #dc3545; color: white; text-decoration: none; border-radius: 5px; font-weight: bold;">
        查看详情
      </a>
    </p>
    
    <hr style="margin: 30px 0; border: none; border-top: 1px solid #dee2e6;" />
    <p style="font-size: 12px; color: #6c757d; text-align: center;">
      此邮件由 FF AI Workspace 系统自动发送，请勿回复。
    </p>
  </div>
</div>
    `,
    variables: ['employeeName', 'weekYear', 'weekNumber', 'approverName', 'rejectTime', 'reason', 'link'],
    example: {
      employeeName: '张三',
      weekYear: 2024,
      weekNumber: 46,
      approverName: '李经理',
      rejectTime: '2024-11-15 15:30',
      reason: '加班时长与实际工作内容不符，请核实后重新提交',
      link: 'https://example.com/overtime/my-records',
    },
    priority: 'NORMAL',
    isActive: true,
  },
];

async function main() {
  console.log('🚀 Initializing notification templates...\n');
  
  for (const template of templates) {
    try {
      const { code, ...data } = template;
      await prisma.notificationTemplate.upsert({
        where: { code },
        update: data as any,
        create: template as any,
      });
      console.log(`✓ Template "${template.name}" (${template.code}) initialized`);
    } catch (error) {
      console.error(`✗ Failed to initialize template ${template.code}:`, error);
    }
  }
  
  console.log('\n✅ All notification templates initialized successfully!');
  console.log(`\nTotal templates: ${templates.length}`);
}

main()
  .catch((error) => {
    console.error('Error initializing templates:', error);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

