'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const permissions = [
      // Master Data Permissions
      { permission_code: 'MASTER.COMPANY.ALL', permission_name: 'Company - All', module: 'MASTER', resource: 'company', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'MASTER.PLANT.ALL', permission_name: 'Plant - All', module: 'MASTER', resource: 'plant', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'MASTER.MATERIAL.ALL', permission_name: 'Material - All', module: 'MASTER', resource: 'material', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'MASTER.BUSINESS_PARTNER.ALL', permission_name: 'Business Partner - All', module: 'MASTER', resource: 'business_partner', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'MASTER.BOM.ALL', permission_name: 'BOM - All', module: 'MASTER', resource: 'bom', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'MASTER.ROUTING.ALL', permission_name: 'Routing - All', module: 'MASTER', resource: 'routing', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Purchase Permissions
      { permission_code: 'PURCHASE.PURCHASE_ORDER.CREATE', permission_name: 'Purchase Order - Create', module: 'PURCHASE', resource: 'purchase_order', action: 'CREATE', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'PURCHASE.PURCHASE_ORDER.READ', permission_name: 'Purchase Order - Read', module: 'PURCHASE', resource: 'purchase_order', action: 'READ', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'PURCHASE.PURCHASE_ORDER.APPROVE', permission_name: 'Purchase Order - Approve', module: 'PURCHASE', resource: 'purchase_order', action: 'APPROVE', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'PURCHASE.GRN.ALL', permission_name: 'GRN - All', module: 'PURCHASE', resource: 'grn', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Sales Permissions
      { permission_code: 'SALES.SALES_ORDER.CREATE', permission_name: 'Sales Order - Create', module: 'SALES', resource: 'sales_order', action: 'CREATE', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'SALES.SALES_ORDER.READ', permission_name: 'Sales Order - Read', module: 'SALES', resource: 'sales_order', action: 'READ', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'SALES.SALES_ORDER.APPROVE', permission_name: 'Sales Order - Approve', module: 'SALES', resource: 'sales_order', action: 'APPROVE', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Production Permissions
      { permission_code: 'PRODUCTION.PRODUCTION_ORDER.ALL', permission_name: 'Production Order - All', module: 'PRODUCTION', resource: 'production_order', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Finance Permissions
      { permission_code: 'FINANCE.GL_ACCOUNT.ALL', permission_name: 'GL Account - All', module: 'FINANCE', resource: 'gl_account', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'FINANCE.ACCOUNTING_DOCUMENT.ALL', permission_name: 'Accounting Document - All', module: 'FINANCE', resource: 'accounting_document', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // HR Permissions
      { permission_code: 'HR.EMPLOYEE.ALL', permission_name: 'Employee - All', module: 'HR', resource: 'employee', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      
      // Admin Permissions
      { permission_code: 'ADMIN.USER.ALL', permission_name: 'User - All', module: 'ADMIN', resource: 'user', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() },
      { permission_code: 'ADMIN.ROLE.ALL', permission_name: 'Role - All', module: 'ADMIN', resource: 'role', action: 'ALL', is_system_permission: true, status: 'ACTIVE', created_at: new Date(), updated_at: new Date() }
    ];

    const dialect = queryInterface.sequelize.getDialect ? queryInterface.sequelize.getDialect() : 'mysql';
    if (dialect === 'mysql' || dialect === 'mariadb') {
      for (const p of permissions) {
        await queryInterface.sequelize.query(
          `INSERT IGNORE INTO permission (permission_code, permission_name, module, resource, action, is_system_permission, status, created_at, updated_at)
           VALUES (:permission_code, :permission_name, :module, :resource, :action, :is_system_permission, :status, NOW(), NOW())`,
          { replacements: { permission_code: p.permission_code, permission_name: p.permission_name, module: p.module, resource: p.resource, action: p.action, is_system_permission: p.is_system_permission ? 1 : 0, status: p.status } }
        );
      }
    } else {
      await queryInterface.bulkInsert('permission', permissions, { ignoreDuplicates: true });
    }
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('permission', null, {});
  }
};

