module.exports = (sequelize, DataTypes) => {
  const CustomerComplaintRegister = sequelize.define('CustomerComplaintRegister', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    ccr_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      comment: 'Customer Complaint Register Number'
    },
    ccr_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      comment: 'Complaint Date'
    },
    doc_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      defaultValue: 'RI/QA/F/08',
      comment: 'Document Number'
    },
    rev_no: {
      type: DataTypes.STRING(20),
      allowNull: true,
      defaultValue: '0',
      comment: 'Revision Number'
    },
    rev_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Revision Date'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    customer_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Customer name (cached)'
    },
    mode_of_complaint: {
      type: DataTypes.ENUM('ORAL', 'PHONE', 'LETTER', 'EMAIL', 'FAX', 'VISIT'),
      allowNull: true,
      comment: 'Mode of complaint received'
    },
    part_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Part Name'
    },
    part_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Part Number'
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    complaint_description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Detailed complaint description'
    },
    car_ref_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Corrective Action Report Reference Number'
    },
    action_taken: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Action taken (briefly)'
    },
    closure_status: {
      type: DataTypes.ENUM('OPEN', 'IN_PROGRESS', 'RESOLVED', 'CLOSED', 'REJECTED'),
      allowNull: false,
      defaultValue: 'OPEN',
      comment: 'Complaint closure status'
    },
    closure_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Date when complaint was closed'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Additional remarks/sign'
    },
    assigned_to_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User assigned to handle complaint'
    },
    assigned_to_name: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Assigned person name'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT',
      comment: 'Document status'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'customer_complaint_register',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['plant_id'] },
      { unique: true, fields: ['ccr_no'] },
      { fields: ['ccr_date'] },
      { fields: ['customer_id'] },
      { fields: ['status'] },
      { fields: ['closure_status'] }
    ]
  });

  CustomerComplaintRegister.associate = function(models) {
    CustomerComplaintRegister.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    CustomerComplaintRegister.belongsTo(models.Plant, {
      foreignKey: 'plant_id',
      as: 'plant'
    });

    CustomerComplaintRegister.belongsTo(models.FiscalYear, {
      foreignKey: 'fiscal_year_id',
      as: 'fiscalYear'
    });

    CustomerComplaintRegister.belongsTo(models.BusinessPartner, {
      foreignKey: 'customer_id',
      as: 'customer'
    });

    CustomerComplaintRegister.belongsTo(models.Material, {
      foreignKey: 'material_id',
      as: 'material'
    });

    CustomerComplaintRegister.belongsTo(models.User, {
      foreignKey: 'assigned_to_user_id',
      as: 'assignedUser'
    });

    CustomerComplaintRegister.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    CustomerComplaintRegister.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });
  };

  return CustomerComplaintRegister;
};
