'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('customer_complaint_register', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Company this complaint belongs to'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Plant where complaint is registered'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'fiscal_year',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
        comment: 'Fiscal year'
      },
      ccr_no: {
        type: Sequelize.STRING(50),
        allowNull: false,
        unique: true,
        comment: 'Customer Complaint Register Number'
      },
      ccr_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        comment: 'Complaint Date'
      },
      doc_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        defaultValue: 'RI/QA/F/08',
        comment: 'Document Number'
      },
      rev_no: {
        type: Sequelize.STRING(20),
        allowNull: true,
        defaultValue: '0',
        comment: 'Revision Number'
      },
      rev_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Revision Date'
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'business_partner',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Customer'
      },
      customer_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Customer name (cached)'
      },
      mode_of_complaint: {
        type: Sequelize.ENUM('ORAL', 'PHONE', 'LETTER', 'EMAIL', 'FAX', 'VISIT'),
        allowNull: true,
        comment: 'Mode of complaint received'
      },
      part_name: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: 'Part Name'
      },
      part_no: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Part Number'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Material reference'
      },
      complaint_description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Detailed complaint description'
      },
      car_ref_no: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Corrective Action Report Reference Number'
      },
      action_taken: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Action taken (briefly)'
      },
      closure_status: {
        type: Sequelize.ENUM('OPEN', 'IN_PROGRESS', 'RESOLVED', 'CLOSED', 'REJECTED'),
        allowNull: false,
        defaultValue: 'OPEN',
        comment: 'Complaint closure status'
      },
      closure_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Date when complaint was closed'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'Additional remarks/sign'
      },
      assigned_to_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'User assigned to handle complaint'
      },
      assigned_to_name: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: 'Assigned person name'
      },
      status: {
        type: Sequelize.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT',
        comment: 'Document status'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    }, {
      charset: 'utf8mb4',
      collate: 'utf8mb4_unicode_ci',
      comment: 'Customer Complaint Register (RI/QA/F/08)'
    });

    // Create indexes
    await queryInterface.addIndex('customer_complaint_register', ['company_id'], {
      name: 'idx_ccr_company_id'
    });
    await queryInterface.addIndex('customer_complaint_register', ['plant_id'], {
      name: 'idx_ccr_plant_id'
    });
    await queryInterface.addIndex('customer_complaint_register', ['ccr_no'], {
      name: 'idx_ccr_no',
      unique: true
    });
    await queryInterface.addIndex('customer_complaint_register', ['ccr_date'], {
      name: 'idx_ccr_date'
    });
    await queryInterface.addIndex('customer_complaint_register', ['customer_id'], {
      name: 'idx_ccr_customer_id'
    });
    await queryInterface.addIndex('customer_complaint_register', ['status'], {
      name: 'idx_ccr_status'
    });
    await queryInterface.addIndex('customer_complaint_register', ['closure_status'], {
      name: 'idx_ccr_closure_status'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('customer_complaint_register');
  }
};
