'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Define all permissions in a structured way
    const permissionDefinitions = [
      // SALES MODULE
      { module: 'SALES', resource: 'QUOTATION', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW', 'PRINT', 'EXPORT'] },
      { module: 'SALES', resource: 'SALES_ORDER', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW', 'PRINT', 'EXPORT', 'CLOSE', 'REOPEN'] },
      { module: 'SALES', resource: 'DELIVERY', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'SALES', resource: 'SALES_INVOICE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT', 'EXPORT', 'CANCEL'] },
      
      // PPC MODULE
      { module: 'PPC', resource: 'PRODUCTION_PLAN', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT', 'EXPORT'] },
      { module: 'PPC', resource: 'PRODUCTION_SCHEDULE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW'] },
      { module: 'PPC', resource: 'WORK_ORDER', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT', 'CLOSE', 'REOPEN'] },
      { module: 'PPC', resource: 'MRP', operations: ['VIEW', 'EXPORT'] },
      
      // ENGINEERING MODULE
      { module: 'ENGINEERING', resource: 'MATERIAL', operations: ['CREATE', 'EDIT', 'DELETE', 'VIEW', 'EXPORT'] },
      { module: 'ENGINEERING', resource: 'BOM', operations: ['CREATE', 'EDIT', 'DELETE', 'APPROVE', 'VIEW', 'PRINT', 'EXPORT'] },
      { module: 'ENGINEERING', resource: 'ROUTING', operations: ['CREATE', 'EDIT', 'DELETE', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'ENGINEERING', resource: 'DRAWING', operations: ['UPLOAD', 'DELETE', 'VIEW', 'DOWNLOAD'] },
      { module: 'ENGINEERING', resource: 'DOCUMENT', operations: ['CREATE', 'EDIT', 'DELETE', 'VIEW'] },
      
      // PURCHASE MODULE
      { module: 'PURCHASE', resource: 'PURCHASE_REQUISITION', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW', 'PRINT'] },
      { module: 'PURCHASE', resource: 'RFQ', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'PURCHASE', resource: 'PURCHASE_ORDER', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW', 'PRINT', 'EXPORT', 'CLOSE', 'REOPEN'] },
      { module: 'PURCHASE', resource: 'VENDOR_INVOICE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      
      // INVENTORY MODULE
      { module: 'INVENTORY', resource: 'GOODS_RECEIPT', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'INVENTORY', resource: 'GOODS_ISSUE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'INVENTORY', resource: 'STOCK_TRANSFER', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'INVENTORY', resource: 'STOCK_ADJUSTMENT', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'INVENTORY', resource: 'STOCK_REPORT', operations: ['VIEW', 'EXPORT'] },
      { module: 'INVENTORY', resource: 'STOCK_VALUATION', operations: ['VIEW', 'EXPORT'] },
      
      // PRODUCTION MODULE
      { module: 'PRODUCTION', resource: 'PRODUCTION_ORDER', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW', 'PRINT', 'EXPORT', 'START', 'PAUSE', 'RESUME', 'CLOSE', 'REOPEN'] },
      { module: 'PRODUCTION', resource: 'MATERIAL_ISSUE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'PRODUCTION', resource: 'PRODUCTION_CONFIRMATION', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'PRODUCTION', resource: 'SCRAP_REPORT', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW'] },
      { module: 'PRODUCTION', resource: 'WIP_STATUS', operations: ['VIEW'] },
      { module: 'PRODUCTION', resource: 'WIP_REPORT', operations: ['EXPORT'] },
      { module: 'PRODUCTION', resource: 'PRODUCTION_EFFICIENCY', operations: ['VIEW'] },
      { module: 'PRODUCTION', resource: 'PRODUCTION_REPORT', operations: ['EXPORT'] },
      
      // QC MODULE
      { module: 'QC', resource: 'QC_INSPECTION', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW', 'PRINT', 'EXPORT'] },
      { module: 'QC', resource: 'INCOMING_INSPECTION', operations: ['CREATE', 'APPROVE', 'REJECT', 'VIEW'] },
      { module: 'QC', resource: 'IN_PROCESS_INSPECTION', operations: ['CREATE', 'APPROVE', 'REJECT', 'VIEW'] },
      { module: 'QC', resource: 'FINAL_INSPECTION', operations: ['CREATE', 'APPROVE', 'REJECT', 'VIEW'] },
      { module: 'QC', resource: 'QUALITY_REPORT', operations: ['CREATE', 'VIEW', 'EXPORT'] },
      { module: 'QC', resource: 'QUALITY_METRICS', operations: ['VIEW', 'EXPORT'] },
      
      // MAINTENANCE MODULE
      { module: 'MAINTENANCE', resource: 'MAINTENANCE_REQUEST', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'MAINTENANCE', resource: 'MAINTENANCE_SCHEDULE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'MAINTENANCE', resource: 'MAINTENANCE_WORK_ORDER', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT', 'CLOSE'] },
      { module: 'MAINTENANCE', resource: 'MACHINE_HISTORY', operations: ['CREATE', 'EDIT', 'VIEW', 'PRINT'] },
      { module: 'MAINTENANCE', resource: 'MAINTENANCE_REPORT', operations: ['VIEW', 'EXPORT'] },
      
      // DISPATCH MODULE
      { module: 'DISPATCH', resource: 'DISPATCH_PLAN', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW'] },
      { module: 'DISPATCH', resource: 'DELIVERY_CHALLAN', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'DISPATCH', resource: 'SHIPPING_DOCUMENT', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'DISPATCH', resource: 'GATE_PASS', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'DISPATCH', resource: 'DISPATCH_REPORT', operations: ['VIEW', 'EXPORT'] },
      
      // FINANCE MODULE
      { module: 'FINANCE', resource: 'PAYMENT_VOUCHER', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'FINANCE', resource: 'RECEIPT_VOUCHER', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'FINANCE', resource: 'JOURNAL_ENTRY', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT'] },
      { module: 'FINANCE', resource: 'TRIAL_BALANCE', operations: ['VIEW', 'EXPORT'] },
      { module: 'FINANCE', resource: 'PROFIT_LOSS', operations: ['VIEW', 'EXPORT'] },
      { module: 'FINANCE', resource: 'BALANCE_SHEET', operations: ['VIEW', 'EXPORT'] },
      { module: 'FINANCE', resource: 'ACCOUNTS_RECEIVABLE', operations: ['VIEW', 'EXPORT'] },
      { module: 'FINANCE', resource: 'ACCOUNTS_PAYABLE', operations: ['VIEW', 'EXPORT'] },
      { module: 'FINANCE', resource: 'CASH_FLOW', operations: ['VIEW', 'EXPORT'] },
      { module: 'FINANCE', resource: 'FINANCIAL_REPORTS', operations: ['VIEW', 'EXPORT'] },
      
      // HR MODULE
      { module: 'HR', resource: 'EMPLOYEE', operations: ['CREATE', 'EDIT', 'DELETE', 'VIEW', 'EXPORT'] },
      { module: 'HR', resource: 'ATTENDANCE', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'EXPORT'] },
      { module: 'HR', resource: 'LEAVE_APPLICATION', operations: ['CREATE', 'EDIT', 'APPROVE', 'REJECT', 'VIEW'] },
      { module: 'HR', resource: 'PAYROLL', operations: ['CREATE', 'EDIT', 'APPROVE', 'VIEW', 'PRINT', 'EXPORT'] },
      { module: 'HR', resource: 'PAYSLIP', operations: ['VIEW', 'PRINT'] },
      { module: 'HR', resource: 'MUSTER_ROLL', operations: ['CREATE', 'EDIT', 'VIEW', 'EXPORT'] },
      { module: 'HR', resource: 'HR_REPORTS', operations: ['VIEW', 'EXPORT'] },
      
      // MANAGEMENT MODULE
      { module: 'MANAGEMENT', resource: 'DASHBOARD', operations: ['VIEW'] },
      { module: 'MANAGEMENT', resource: 'ALL_REPORTS', operations: ['VIEW', 'EXPORT'] },
      { module: 'MANAGEMENT', resource: 'ANALYTICS', operations: ['VIEW', 'EXPORT'] },
      { module: 'MANAGEMENT', resource: 'KPI_DASHBOARD', operations: ['VIEW', 'EXPORT'] },
      { module: 'MANAGEMENT', resource: 'MANAGEMENT_REPORTS', operations: ['VIEW', 'EXPORT'] },
      { module: 'MANAGEMENT', resource: 'MIS_REPORTS', operations: ['VIEW', 'EXPORT'] },
      { module: 'MANAGEMENT', resource: 'SYSTEM_SETTINGS', operations: ['CONFIGURE'] },
      { module: 'MANAGEMENT', resource: 'AUDIT_TRAIL', operations: ['VIEW', 'EXPORT'] },
      { module: 'MANAGEMENT', resource: 'USER_ACTIVITY', operations: ['VIEW', 'EXPORT'] },
      
      // MASTER MODULE (simplified - using ALL operation)
      { module: 'MASTER', resource: 'COMPANY', operations: ['ALL'] },
      { module: 'MASTER', resource: 'PLANT', operations: ['ALL'] },
      { module: 'MASTER', resource: 'DEPARTMENT', operations: ['ALL'] },
      { module: 'MASTER', resource: 'MATERIAL', operations: ['ALL'] },
      { module: 'MASTER', resource: 'BUSINESS_PARTNER', operations: ['ALL'] },
      { module: 'MASTER', resource: 'MACHINE', operations: ['ALL'] },
      { module: 'MASTER', resource: 'UOM', operations: ['ALL'] },
      
      // ADMIN MODULE
      { module: 'ADMIN', resource: 'USER', operations: ['ALL'] },
      { module: 'ADMIN', resource: 'ROLE', operations: ['ALL'] },
      { module: 'ADMIN', resource: 'PERMISSION', operations: ['ALL'] }
    ];

    // Generate permissions array
    const permissions = [];
    permissionDefinitions.forEach(def => {
      def.operations.forEach(operation => {
        const permissionCode = `${def.module}_${def.resource}_${operation}`;
        const permissionName = `${def.resource.replace(/_/g, ' ')} - ${operation}`;
        
        permissions.push({
          permission_code: permissionCode,
          permission_name: permissionName,
          module: def.module,
          resource: def.resource,
          operation: operation,
          action: operation === 'ALL' ? 'ALL' : operation, // Keep action for backward compatibility
          description: `Permission to ${operation.toLowerCase()} ${def.resource.replace(/_/g, ' ').toLowerCase()} in ${def.module} module`,
          is_system_permission: true,
          status: 'ACTIVE',
          created_at: new Date(),
          updated_at: new Date()
        });
      });
    });

    // Insert permissions (ignore duplicates)
    await queryInterface.bulkInsert('permission', permissions, {
      ignoreDuplicates: true
    });

    console.log(`Inserted ${permissions.length} operation-wise permissions`);
  },

  async down(queryInterface, Sequelize) {
    // Remove all operation-wise permissions
    await queryInterface.bulkDelete('permission', {
      permission_code: {
        [Sequelize.Op.like]: '%_%_%'
      }
    }, {});
  }
};

